Naming and state
Name every class for what it is, follow one pattern, and show state through attributes. A selector then says which element it means and why.
- Recommended 8
- Optional 2
Recommended
Show state through attributes
CSS-15Express state with the attribute that carries it, such as
aria-expanded,aria-current,aria-invalid,disabled,hiddenordata-state. Never add a state class such asis-openoractive.Let the hidden attribute always hide
CSS-36Make the
hiddenattribute hide an element whatever its classes set. One rule in the utilities layer gives[hidden]the valuedisplay: none !important, excepthidden="until-found".Select elements by their own class
CSS-16Select elements in a component by their own class, never by tag name, position or ancestry. A selector has at most 2 compound parts and 0 type selectors in the components layer, which keeps it inside the
0,2,0cap.Prefix a component's custom properties with its name
CSS-17Name every custom property defined in a component file with the module prefix, such as
--order-card-gap. Set it from a global token, and let variants change only these local properties.Define every custom property a stylesheet reads
CSS-34Make every
var(--name)refer to a custom property that a stylesheet declares. A name that may be missing on purpose gets a fallback insidevar().Register animated custom properties with @property
CSS-39Register every custom property that a transition or animation changes with
@property, giving itssyntax,inheritsandinitial-value. A component registers only names with its own prefix.Keep script hooks apart from style hooks
CSS-18Let scripts find elements through
data-attributes or ids, never through styling classes. Stylesheets never select the attributes that scripts use as hooks.Nest only states and queries one level deep
CSS-33Nest at most one level inside a style rule. A nested rule is either
&followed by a pseudo-class or attribute, or an@mediaor@containerblock. Never nest a class inside another class.
Optional
Name classes for what the element is
CSS-13Name each class after the element's job in its module, such as
invoice-total. Never name it after a look, such asbig-redorleft-box.Use one naming pattern for every class
CSS-14Write every class name in lowercase words joined by single hyphens, starting with the module prefix. One regular expression describes every class in the project.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/html-css/naming-and-state">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.