CSS-17Recommended
Prefix a component's custom properties with its name
Name 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.
How to do it
Global tokens hold the project's values, as Take every style value from design tokens describes. A component maps them to its own properties once at its root. Variants then change one local property instead of repeating whole declarations. A component never redefines a global token, so a change inside one module cannot leak into the rest of the page. Colour tokens follow Define every colour once.
Examples
CSS example
/* components/order-card.css */
.order-card {
--order-card-gap: var(--space-small);
gap: var(--order-card-gap);
}
.order-card[data-size="large"] { --order-card-gap: var(--space-large); }
/* Fails: redefines a global token inside a component */
.order-card { --space-small: 2px; }
HTML example
<article class="order-card" data-size="large">Wedding cake, three tiers</article>
How to check
Every custom property declared in a component file starts with -- followed by that file's prefix. Component files declare 0 global token names.
Sources
Related rules
- APP-1 Take every style value from design tokens in Web app interface
- WEB-31 Define every colour once in Website
- CSS-34 Define every custom property a stylesheet reads in HTML and CSS
- CSS-39 Register animated custom properties with @property in HTML and CSS
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/component-custom-properties">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.