Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Modifying Element Styles | DOM Manipulation
Advanced JavaScript Mastery
course content

Course Content

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

book
Modifying Element Styles

In web development, we often need to modify the element styles dynamically. JavaScript provides two primary ways to change the appearance of elements: updating inline styles using the style property and managing classes with classList.

Changing Inline Styles Using the style Property

The style property in JavaScript allows you to directly modify the inline CSS of an HTML element. This method gives you full control over individual CSS properties but is limited to inline styles and only affects the specific element.

We can modify individual CSS properties using dot notation on the style object of an element. The property names are written in camelCase (e.g., backgroundColor instead of background-color).

html

index

css

index

js

index

copy

The style property is used to modify the inline styles of the #box element. Each CSS property is accessed as an individual JavaScript property (e.g., box.style.backgroundColor), allowing you to change specific aspects of the element's style dynamically.

Adding and Removing CSS Classes with classList

The classList property provides a more flexible and powerful way to manage the styles of an element by adding, removing, or toggling CSS classes. This method is more maintainable than directly modifying inline styles, as it allows you to take advantage of external CSS files and keep your styling separate from your JavaScript logic.

html

index

css

index

js

index

copy

classList.toggle() is used to add or remove the highlight class when the button is clicked. This method avoids cluttering the inline style attribute and relies on pre-defined CSS rules for maintainability.

Practical Example: Switching Themes

Let's take a practical example where the user can switch between a light and dark theme using classList.

html

index

css

index

js

index

copy

classList.replace() is used to swap the light-theme and dark-theme classes based on the current state.

1. Which property is used to modify inline styles of an element?
2. How would you change the background color of a `<div>` with an ID of `box` to coral using JavaScript?
3. What does the following code do?
Which property is used to modify inline styles of an element?

Which property is used to modify inline styles of an element?

Select the correct answer

How would you change the background color of a `<div>` with an ID of `box` to coral using JavaScript?

How would you change the background color of a <div> with an ID of box to coral using JavaScript?

Select the correct answer

What does the following code do?

What does the following code do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 12
We're sorry to hear that something went wrong. What happened?
some-alt