Manipulating Styles in JavaScript

Javascript 7 min min read Updated: Mar 09, 2026 Intermediate
Manipulating Styles in JavaScript
Intermediate Topic 5 of 15

JavaScript allows developers to dynamically modify the styles of HTML elements using the DOM. This process is known as manipulating styles. By changing CSS properties through JavaScript, developers can update the appearance of a webpage based on user actions or application logic.

Manipulating styles is commonly used to highlight elements, hide or show content, create animations, and improve user experience on interactive websites.

Why Manipulating Styles is Important

Modern web applications often require visual updates based on user interaction. For example, buttons may change color when clicked, menus may appear when hovered, or messages may be displayed dynamically.

Key Point: JavaScript can modify CSS styles in real time without refreshing the webpage.

Changing Styles Using the style Property

The style property allows JavaScript to directly change the CSS properties of an HTML element.

javascript let heading = document.getElementById("title"); heading.style.color = "blue"; heading.style.fontSize = "24px";
Output

The selected element text becomes blue and its font size increases.

Changing Background Color

JavaScript can also modify background colors and other visual styles.

javascript document.getElementById("box").style.backgroundColor = "yellow";
Output

The background color of the element changes to yellow.

Key Point: CSS properties written with hyphens (like background-color) are written in camelCase in JavaScript (backgroundColor).

Adding and Removing CSS Classes

Another common approach to style manipulation is using CSS classes. Instead of directly modifying styles, developers can add or remove classes using JavaScript.

javascript let element = document.getElementById("content"); element.classList.add("highlight");
Output

The CSS class "highlight" is applied to the element.

Removing a Class

JavaScript can also remove classes from elements.

javascript element.classList.remove("highlight");
Output

The highlight class is removed from the element.

Toggling a Class

The toggle() method adds a class if it does not exist and removes it if it already exists.

javascript element.classList.toggle("active");
Output

The class "active" is added or removed depending on its current state.

Key Point: Using CSS classes with JavaScript is often better than directly changing styles because it keeps styling and logic separate.

Conclusion

Manipulating styles with JavaScript allows developers to dynamically control the appearance of HTML elements. By using the style property or modifying CSS classes, developers can create interactive and visually responsive webpages.

These techniques are widely used in modern web development for features such as dynamic themes, animations, and user interface updates.

In the next tutorial, you will learn about Creating and Removing Elements in JavaScript, which explains how JavaScript can dynamically add or delete elements from a webpage.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators