liveBook · Manning

CSS class

As explained in Web Components in Action

A CSS class is a named classification applied to elements so that matching CSS rules can style them.

A CSS class is a named classification applied to elements so matching CSS rules can style them. JavaScript can add or remove classes as an element’s state changes. section 5.3 section 7.4

Use classes in two common ways:

  • Add several classes to compose an effect, such as shake, shake-constant, and shake-horizontal; add shake-little for a shorter strum, then remove them when the timer ends. section 5.3
  • Use a class as a local selector, such as template.default-card, when choosing among templates inside a component. section 7.4

Animating an element with classes

strum(params) { this.classList.add('shake', 'shake-constant', 'shake-horizontal'); if (dur < 500) this.classList.add('shake-little'); this.timer = setTimeout(() => this.stopStrum(), dur); } stopStrum() { this.classList.remove('shake', 'shake-constant', 'shake-horizontal', 'shake-little'); }

How Web Components in Action covers it

How deep it goes
Demonstrates dynamic classList updates and class-based querySelector lookup, without descending into CSS rule definitions.
Examples
Uses CSShake animation classes and default-card or variation classes to identify templates.
What it assumes
Assumes familiarity with HTML elements, DOM querying, and Web Components while demonstrating the class operations needed here.
What you can do after
Apply it via DOM classList and querySelector APIs to animate elements and choose matching templates.
How it connects
Connects CSS classes with DOM elements, CSS selectors, Web Components, templates, and component state.

Also explained in

Compare how 5 books explain CSS class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.