JavaScript Changing Styles (DOM Style Manipulation)
JavaScript allows you to change the appearance of HTML elements dynamically using the DOM. This is called style manipulation. It is commonly used to create interactive effects like highlighting elements, showing or hiding content, changing themes, and responding to user actions.
Changing styles with JavaScript is a core skill for building modern, interactive websites.
Why Change Styles Using JavaScript?
Using JavaScript to change styles helps you:
Accessing the style Property
Every HTML element has a style property that lets you apply inline CSS styles.
Example
CSS properties written with hyphens become camelCase in JavaScript.
Changing Multiple Styles
You can update multiple styles one by one:
Showing and Hiding Elements
JavaScript is often used to control visibility.
Other useful properties:
Changing Styles on User Events
Styles are commonly changed when users interact with elements.
Using classList (Best Practice)
Instead of adding inline styles, it’s better to toggle CSS classes.
JavaScript
CSS
Changing Styles for Multiple Elements
Inline Styles vs Class-Based Styles (Comparison)