Sub topic 5
Naming tokens so they stay maintainable
This chapter assumes you can declare a custom property, read it with var(), and explain how it inherits down the DOM. It covers names only. The next chapter covers how tokens are layered into tiers.
Why names matter
A token's name is the interface other developers use. Once a name is spread across hundreds of rules, renaming it is expensive. A name that describes the wrong thing becomes misleading as soon as the value changes.
Consider --blue. When the brand color moves to green, you have two bad options. You can keep a variable named --blue that holds green, or you can rename it everywhere. A name should survive a change of value.
Name by role, not by value
Names that describe the value (--blue, --16px, --light-gray) break when the value changes. Names that describe the job stay correct.
:root {
/* Fragile: the name repeats the value */
--blue: #2563eb;
--light-gray: #f9fafb;
/* Durable: the name states the purpose */
--color-accent: #2563eb;
--color-surface: #f9fafb;
}
.button {
background: var(--color-accent);
}Value-based names still have a place for raw palette entries such as --color-blue-500. These belong to the primitive tier, and components should not use them directly.
Use one predictable structure
Pick a pattern and apply it everywhere. A common one runs from general to specific:
--{category}-{property or role}-{variant}-{state}
Each part is optional, but the order stays fixed.
| Token | Category | Role | Variant or state |
|---|---|---|---|
| `--color-text` | color | text | none |
| `--color-text-muted` | color | text | muted |
| `--color-accent-hover` | color | accent | hover |
| `--space-4` | space | step on a scale | 4 |
| `--font-size-lg` | font-size | size | lg |
Putting the category first has a practical benefit. Typing --color- in an editor lists every color token, and sorting the stylesheet groups related tokens together.
Formatting rules
Custom property names are case-sensitive. --Color-Accent and --color-accent are two different properties, and a typo in case silently produces an undefined variable.
- Use lowercase kebab-case for every token.
- Write numeric steps as plain numbers (
--space-4) or t-shirt sizes (--font-size-lg), and choose one style per scale. - Avoid abbreviations that need a glossary. Prefer
--color-backgroundto--clr-bgunless the whole team uses the short form. - Do not encode a token's tier or implementation in its name, such as
--var-color-1.
Prefix component tokens
Custom properties inherit, so a name used in one component can leak into its children. Prefix a component token with the component name to avoid collisions.
:root {
--space-2: 0.5rem;
--space-4: 1rem;
}
.card {
--card-padding: var(--space-4);
padding: var(--card-padding);
}
.card--compact {
--card-padding: var(--space-2);
}If you named this token --padding, a nested component that also set --padding would override it for its own subtree. A --card- prefix makes the owner clear and prevents that.
Check a new name
The diagram below shows how a name is built and checked.
Renaming safely
When a name must change, keep the old one as an alias for a while. Point it at the new token so existing rules keep working, then remove it once nothing uses it.
:root {
--color-accent: #2563eb;
/* Deprecated alias: remove after migrating usages */
--brand-color: var(--color-accent);
}Searching the codebase for --brand-color shows what is left to migrate. Because the alias resolves through var(), it follows any later change to --color-accent.
Exercise
A stylesheet defines --red: #dc2626; and uses it for error messages, and the design team now wants errors shown in orange. Which name would have avoided the problem, and why?
Show answer
A role-based name such as --color-error (or --color-danger). The name describes the purpose, so changing its value to orange leaves the name accurate and no rule has to be renamed. --red would now hold orange, which is misleading. --red can remain as a primitive that --color-error points to.