Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Modifying Element Styles with JavaScript | DOM Manipulation for Interactive Web Development
Advanced JavaScript Mastery

book
Modifying Element Styles with JavaScript

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.html

css

index.css

js

index.js

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<div id="box"></div>
<button id="change-style">Change Style</button>
</div>
<script src="index.js"></script>
</body>
</html>

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.html

css

index.css

js

index.js

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<div id="circle" class="default-style"></div>
<button id="toggle-style">Toggle Style</button>
</div>
<script src="index.js"></script>
</body>
</html>

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.html

css

index.css

js

index.js

copy
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div id="theme-container">
<p>This is a theme switcher example</p>
<button id="toggle-theme">Switch Theme</button>
</div>
<script src="index.js"></script>
</body>
</html>

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?

question mark

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

Select the correct answer

question mark

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

// HTML:
<div id="box"></div>

Select the correct answer

question mark

What does the following code do?

const button = document.getElementById('toggle-style');
button.classList.toggle('active');

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 12

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt