Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Adding and Removing CSS Classes | Modifying and Styling the DOM
DOM Manipulation with JavaScript

bookAdding and Removing CSS Classes

When you want to change how an element looks on a web page, you can use JavaScript to modify its styles. While you have learned about changing inline styles directly, there is a more flexible and maintainable way: using CSS classes and the classList property. The classList property gives you a set of powerful methods for adding, removing, and toggling CSS classes on any DOM element. These methods include add, remove, and toggle.

Using classList.add("className"), you attach a CSS class to an element, instantly applying all the class's styles. With classList.remove("className"), you take the class off, removing its styles. The classList.toggle("className") method is especially useful. It adds the class if it isn't present, or removes it if it is. This makes it perfect for things like showing or hiding menus, highlighting items, or changing button states.

There are several benefits to using classList over setting inline styles:

  • You keep your JavaScript code cleaner and easier to read;
  • You separate style definitions (CSS) from behavior (JavaScript), making updates easier;
  • You can apply or remove multiple styles at once by toggling classes;
  • You can take advantage of CSS transitions and animations, which work best with classes.
index.html

index.html

script.js

script.js

copy

By using classList.toggle, the example above lets you add or remove the "highlight" class with each button click. This approach makes your code more modular and leverages the full power of CSS.

question mark

Which of the following statements best describes an advantage of using classList methods over directly setting inline styles with JavaScript?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 6.67

bookAdding and Removing CSS Classes

Scorri per mostrare il menu

When you want to change how an element looks on a web page, you can use JavaScript to modify its styles. While you have learned about changing inline styles directly, there is a more flexible and maintainable way: using CSS classes and the classList property. The classList property gives you a set of powerful methods for adding, removing, and toggling CSS classes on any DOM element. These methods include add, remove, and toggle.

Using classList.add("className"), you attach a CSS class to an element, instantly applying all the class's styles. With classList.remove("className"), you take the class off, removing its styles. The classList.toggle("className") method is especially useful. It adds the class if it isn't present, or removes it if it is. This makes it perfect for things like showing or hiding menus, highlighting items, or changing button states.

There are several benefits to using classList over setting inline styles:

  • You keep your JavaScript code cleaner and easier to read;
  • You separate style definitions (CSS) from behavior (JavaScript), making updates easier;
  • You can apply or remove multiple styles at once by toggling classes;
  • You can take advantage of CSS transitions and animations, which work best with classes.
index.html

index.html

script.js

script.js

copy

By using classList.toggle, the example above lets you add or remove the "highlight" class with each button click. This approach makes your code more modular and leverages the full power of CSS.

question mark

Which of the following statements best describes an advantage of using classList methods over directly setting inline styles with JavaScript?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt