Sub topic 8
Design tokens with CSS custom properties
This chapter assumes you know CSS selectors, the cascade, and the box model. It also assumes you can read a BEM-style component, such as .card__title, from the earlier chapters.
What a design token is
A design token is a named value that stands for a design decision. Examples are a brand color, a spacing step, or a font size. Components refer to the name instead of the raw value. When the decision changes, you edit one line.
CSS custom properties are the native way to store tokens. A custom property is a name that starts with --. You read it with var().
Declaring and using tokens
Declare global tokens on :root. Every element inherits them from there.
:root {
--color-text: #1a1a1a;
--color-accent: #0b5fff;
--space-2: 0.5rem;
--space-4: 1rem;
}
.button {
color: var(--color-text);
padding: var(--space-2) var(--space-4);
border: 2px solid var(--color-accent);
}If you change --color-accent once, every rule that uses it updates. Nothing else needs editing.
Custom properties differ from Sass variables in one important way. They exist at runtime, so the browser resolves them while the page runs. They follow the cascade and inheritance, and JavaScript can change them.
Two tiers of tokens
Large codebases usually split tokens into two tiers.
| Tier | Describes | Example |
|---|---|---|
| Primitive | The raw value | `--blue-600: #0b5fff` |
| Semantic | The job the value does | `--color-accent: var(--blue-600)` |
Components should use semantic tokens. Primitives stay in the token file. This keeps a rebrand small, because you repoint the semantic token and leave the components alone.
:root {
--blue-600: #0b5fff;
--blue-300: #7aa5ff;
--color-accent: var(--blue-600);
}Theming by overriding tokens
Because custom properties inherit, you can redefine a token on any element. Its descendants see the new value.
[data-theme="dark"] {
--color-text: #f2f2f2;
--color-accent: var(--blue-300);
}A page with <body data-theme="dark"> switches every component that uses those tokens. No component rule changes. This also keeps specificity low, because themes change values instead of adding override selectors.
Component-level tokens
A component can expose its own tokens with a fallback. The second argument of var() is used when the property is not set.
.card {
padding: var(--card-padding, var(--space-4));
}
.card--compact {
--card-padding: var(--space-2);
}Here the modifier changes a token rather than redeclaring padding. The component keeps a single place where the value is applied.
Common errors
- Naming by value. A token called
--bluebreaks when the brand turns green. Name by role, such as--color-accent. - Using primitives in components. This makes theming and rebranding touch many files.
- Too many tokens. Create a token when a value is reused or likely to change. Do not tokenize every one-off number.
- Typos in
var(). A misspelled name is not an error. The declaration becomes invalid at computed-value time, and the property falls back to its inherited or initial value. Check the computed styles in DevTools. - Wrong scope. A token set on
.cardis not visible to elements outside it. Put shared tokens on:root.
Exercise
Exercise
A .alert rule uses color: var(--blue-600). Your team is rebranding and wants alerts to use the new accent color, and a dark theme needs a lighter shade. What change would you make?
Show answer
Add a semantic token such as --color-accent: var(--blue-600) on :root and change .alert to color: var(--color-accent). Then override --color-accent under [data-theme="dark"] to point at a lighter primitive such as --blue-300. The component then follows the theme and future rebrands without further edits.
Exercise
Why does .card--compact { --card-padding: var(--space-2); } work, even though .card is the rule that sets padding?
Show answer
Custom properties are resolved through the cascade on the element itself. The element has both classes, so --card-padding is set to --space-2 there. When .card reads var(--card-padding, var(--space-4)), it gets the modifier's value. The fallback applies only when the property is unset.