You can use the style property for this. For example, if you want to change the border:
document.elm.style.border = "3px solid #FF0000";
Similarly, for color:
document.getElementById("p2").style.color="blue";
The best thing is to define a class and do this:
document.getElementById("p2").className = "classname";
(Cross-browser artifacts must be considered accordingly.)
Answer from Ved on Stack OverflowW3Schools
w3schools.com › js › js_htmldom_css.asp
JavaScript DOM CSS
jQuery Selectors jQuery HTML jQuery ... Interview Prep JS Bootcamp JS Certificate JS Reference ... The HTML DOM allows JavaScript to change the style of HTML elements....
Top answer 1 of 7
129
You can use the style property for this. For example, if you want to change the border:
document.elm.style.border = "3px solid #FF0000";
Similarly, for color:
document.getElementById("p2").style.color="blue";
The best thing is to define a class and do this:
document.getElementById("p2").className = "classname";
(Cross-browser artifacts must be considered accordingly.)
2 of 7
30
// select element from DOM using *const*
const sample = document.getElementById("myid"); // using const
// or you can use *var*
var sample = document.getElementById("myid"); // using var
// change css style
sample.style.color = 'red'; // Changes color, adds style property.
// or (not recomended)
sample.style = "color: red"; // Replaces all style properties. NOT RECOMENDED
Videos
09:58
How to Change CSS Styles with JavaScript — Tutorial - YouTube
03:14
Jak Zmienić Style CSS za pomocą JavaScript? - YouTube
03:11
🔗 How to Link CSS & JavaScript to HTML (Step-by-Step Guide) ...
Chapter 28: Javascript Tutorial - How to Style HTML with Javascript?
12:17
A Deeper Look at JavaScript's Style Property - YouTube
08:22
How to Use JavaScript with CSS - YouTube
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › CSS_Object_Model › Using_dynamic_styling_information
Using dynamic styling information - Web APIs | MDN
October 17, 2025 - To get to the style objects from the document, you can use the Document.styleSheets property and access the individual objects by index (e.g., document.styleSheets[0] is the first stylesheet defined for the document, etc.). You can also access the sheet property on a specific <style> element to get its associated stylesheet object. In this example the background of the page is set to red using CSS. The JavaScript then accesses the property using the CSSOM and changes the background to cornflowerblue.
JavaScript Tutorial
javascripttutorial.net › home › javascript dom › javascript style
JavaScript Style: Setting Inline CSS Properties of an HTML Element
November 15, 2024 - The style property returns the read-only CSSStyleDeclaration object that contains a list of CSS properties. For example, to set the color of an element to red, you use the following code: element.style.color = 'red';Code language: JavaScript (javascript)
Udacity
udacity.com › blog › 2021 › 06 › javascript-css.html
Changing Element Styling with Javascript CSS | Udacity
June 28, 2021 - This article discusses how to modify HTML element styling with Javascript CSS, followed by the potential benefits and drawbacks of Javascript CSS compared to external CSS files. Finally, this article provides guidelines for choosing when to use Javascript CSS. Every HTML element in the Javascript DOM contains a Javascript object property called style.
Crestron
sdkcon78221.crestron.com › sdk › Crestron_HTML5UI › Content › Topics › Reference › Development › Change-CSS.htm
Change CSS Dynamically with JavaScript | Crestron® HTML5 User Interface Developer Microsite
This topic documents how to dynamically update the styling applied to your elements by manipulating the CSS at runtime using JavaScript. Using the DOM API, you can access the specific elements of your page. Through the DOM, access is provided to a style object that describes the style of an element.
Techaltum
tutorial.techaltum.com › javascript-css.html
JavaScript style Property | how to change css in javascript
December 28, 2025 - JavaScript style property is used to add, get and change css in javascript. style is the property of HTMLElement Node in DOM.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLElement › style
HTMLElement: style property - Web APIs | MDN
This object can be used to get and set the inline styles of an element. A live CSSStyleProperties object.
W3Schools
w3schools.com › js › js_jquery_css.asp
JavaScript / jQuery CSS Styles
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
W3Schools
w3schools.com › jsref › dom_obj_style.asp
HTML DOM Style object
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
W3C
w3.org › wiki › Dynamic_style_-_manipulating_CSS_with_JavaScript
Dynamic style - manipulating CSS with JavaScript - W3C Wiki
July 9, 2015 - In this article we will look at how to dynamically update the styling applied to your elements by manipulating your CSS at runtime using JavaScript. It uses the same kind of technique that we’ve already seen, but there are a few special considerations to keep in mind when working with the CSS DOM. The browser provides an interface to interact with style sheets — in your JavaScript code you can access a list of your style sheets by using document.styleSheets.