liveBook · Manning

Variant class

As explained in CSS in Depth, Second Edition

A variant class is a CSS class that represents a named variation of a component's appearance or behavior.

A variant class represents a named variation of a reusable module’s appearance or behavior. It lets one module support multiple versions without duplicating the module’s core styles. section 9.5

Choose variant names that describe meaning rather than fragile visual details:

  • Prefer semantic names such as button--danger and button--success over color-based names such as button--red and button--blue. section 9.2.2
  • Use relative modifiers such as button--large cautiously; they remain useful when the variant keeps its relative meaning after a redesign. section 9.2.2
  • Avoid precise modifiers such as button--20px, which bind the class name to a detail likely to change. section 9.2.2

Semantic button variants

Treat button as the reusable module and add a semantic modifier for the intended role: button--danger or button--success. The modifier communicates why the button differs, while leaving room to change its colors later. section 9.2.2

How CSS in Depth, Second Edition covers it

How deep it goes
Explains variant purpose and naming trade-offs, but does not detail implementation mechanics.
Examples
Uses button--danger, button--success, button--large, and button--20px to contrast semantic, relative, and precise names.
What it assumes
Assumes familiarity with CSS classes and introduces variants through the broader idea of reusable modules.
What you can do after
Apply semantic variant classes when creating multiple versions of a reusable module.
How it connects
Connects variant classes to modules, modular CSS, encapsulation, and naming conventions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.