JavaScript can access and modify the CSS of HTML elements through the DOM. Using properties like style and classList, you can dynamically change colors, sizes, layouts, and more in response to user actions, events, or conditions. This is the basis of interactive, responsive web pages.
element.style – Set styles directly on an element.classList – Add, remove, or toggle predefined CSS classes.background-color become backgroundColor in JavaScript.getComputedStyle() – Read an element’s final, calculated styles.Every DOM element exposes a style object that represents its inline styles. You can assign values to these properties to change how the element looks:
For example, element.style.color = "red" sets the text color, and element.style.backgroundColor = "yellow" sets the background color.
However, changing many inline styles can become hard to maintain. A better approach is to define reusable CSS classes in your stylesheet and then use element.classList.add(), element.classList.remove(), or element.classList.toggle() to apply or remove those classes.
When you need to read styles that may come from external stylesheets, inline styles, or browser defaults, use getComputedStyle(element) to get the final, computed values.
styleThis example selects a paragraph using getElementById() and applies several inline styles to it.
<p id="myPara">Hello World!</p>
<script>
const para = document.getElementById("myPara");
para.style.color = "red";
para.style.fontSize = "20px";
para.style.backgroundColor = "yellow";
</script>
classList to Add/Remove ClassesHere, a highlight class is toggled whenever the paragraph is clicked. The CSS class defines the styles in one place, and JavaScript only switches the class on or off.
<style>
.highlight {
background-color: yellow;
font-weight: bold;
}
</style>
<p id="text">Click me to highlight!</p>
<script>
const text = document.getElementById("text");
text.addEventListener("click", function () {
text.classList.toggle("highlight");
});
</script>
In this example, clicking the button applies a border, padding, and centered text to the paragraph.
<button onclick="changeStyle()">Change Style</button>
<p id="demo">Watch my style change!</p>
<script>
function changeStyle() {
const p = document.getElementById("demo");
p.style.border = "2px solid green";
p.style.padding = "10px";
p.style.textAlign = "center";
}
</script>
Click the button below to see JavaScript change the styles of this box in real time.
classList.toggle("highlight") example – Each click adds or removes the highlight class, so the paragraph switches between normal and highlighted styles.changeStyle() example – When the button is clicked, it adds a green border, padding, and centers the text in the paragraph.All of these patterns are commonly used in real projects for interactive UIs, such as opening modals, highlighting errors, showing/hiding menus, and toggling dark mode.
classList over many inline style changes to keep your code clean and reusable.backgroundColor, fontSize)..highlight) instead of repeating them in JavaScript.getComputedStyle(element) when you need to read the actual style applied to an element.<div> with a CSS class and use JavaScript to change its class when a button is clicked..dark-mode class on the <body>.style.display = "none" to hide an element and style.display = "block" (or "inline-block") to show it again.getComputedStyle() to log the current color and fontSize of an element to the console.click and mouseover) with style changes to create interactive hover effects.