Design tokens — splitting CSS variables into two layers
Design tokens manage colour, spacing and type as named values. With CSS variables the most important decision is how many layers to use. A primitive token is the value itself (brand 500); a semantic token is a role (primary fill). Separating them lets theme switching and brand changes happen without interfering.
Two layers
:root {
/* primitive */
--brand-50: oklch(97% 0.02 250);
--brand-500: oklch(58% 0.16 250);
--brand-950: oklch(18% 0.04 250);
/* semantic */
--color-bg: var(--brand-50);
--color-surface: var(--brand-100);
--color-text: var(--brand-950);
--color-primary: var(--brand-500);
}
Components use only semantic tokens. A button reads var(--color-primary), so replacing the brand colour means changing primitives only. Dark mode changes semantic values and the component code stays untouched.
Naming
| Layer | Example | Rule |
|---|---|---|
| Primitive | --brand-500, --gray-100 | colour name plus step |
| Semantic | --color-bg, --color-text | role name |
| Component | --button-bg | only when needed, referencing a semantic token |
| Status | --color-danger, --color-success | role plus state |
Never put a value in a name. --color-blue becomes a lie when the brand turns violet; --color-primary stays true. The same applies to step numbers inside semantic tokens.
Theme switching
There are two mechanisms. light-dark() puts both values in one declaration; a data-theme attribute splits selectors. To follow the system while still letting users choose, combine them: default to light-dark() and let an explicit choice override it through the attribute.
Common mistakes
- Components reading primitives directly: theme switching breaks.
- Splitting semantic tokens too finely, producing names like --card-title-color.
- Defining the same value in several places, so one change leaves colours mismatched.
- Tokens without units:
--space-4: 16breaks inside calc(). - Shipping tokens without documentation: record the role and the contrast you verified.
Keep a token table: value, role, and the contrast pairs checked. It tells the next person which token to use on a new screen. The exported code here contains both layers plus hex fallbacks, which makes a usable starting point.
Documenting the tokens
Code alone does not carry the meaning of a token. A table of each token's value, role and verified contrast pairs is what lets a newcomer know which one to use. Keep the table in the same repository as the code and update it in the same commit; otherwise it ages faster than the values it describes.
Automated checks help as well. A small script that reads the pairs from the token table and calculates contrast in CI surfaces a regression the moment a colour changes. The matrix output here can be copied into a spreadsheet and used as the reference. The goal is a structure that does not depend on anyone remembering the rules.
This summarises token practice; an organisation's own rules take precedence.
Questions
How many semantic tokens should there be?
Background, surface, text, muted, border, primary, on-primary, success and danger — nine cover most interfaces.
Do CSS variables cost performance?
Negligibly. Unless thousands are stacked on one element, it is not a concern.
Do OKLCH tokens break older browsers?
If the definition is invalid the previous value stays. Declare a hex fallback and override it with @supports.
Get the primitive and semantic tokens from the export tab and paste them in.
Open the toolBasis date 28 November 2026. Written against the W3C specifications, the APCA documentation and published browser support tables; updated when they change.
Back to the guides