Sub topic 6
Primitive, semantic and component tokens
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know how they inherit down the DOM tree and how names are chosen. Here you will split a flat list of tokens into three tiers, each with its own job.
Why one flat list breaks down
Suppose you name a token --blue-500 and use it for links, buttons, focus rings and chart bars. Later the brand color changes, or a dark theme needs a lighter link color. Now you must find every use and decide whether it meant "the brand" or "a link". The name describes the value, but the components needed a role.
Tiers fix this by separating what a value is from what it is for.
The three tiers
| Tier | Answers the question | Example | Used by |
|---|---|---|---|
| Primitive | What raw values exist? | `--color-blue-500` | Semantic tokens only |
| Semantic | What role does a value play? | `--color-accent` | Components and layout |
| Component | What can this one component adjust? | `--card-padding` | The component and its variants |
Primitive and semantic tokens
Primitive tokens hold raw values and carry no meaning about where they are used. Semantic tokens point at a primitive and name a role.
:root {
/* Primitive tokens: raw values */
--color-blue-500: #2563eb;
--color-gray-50: #f9fafb;
--color-gray-900: #111827;
/* Semantic tokens: meaning, built from primitives */
--color-text: var(--color-gray-900);
--color-surface: var(--color-gray-50);
--color-accent: var(--color-blue-500);
}
.button {
background: var(--color-accent);
color: white;
}
.card {
background: var(--color-surface);
color: var(--color-text);
}The .button and .card rules never mention a hue. To rebrand, you change one line: the primitive that --color-accent points to. To theme, you point the semantic token at a different primitive. The component rules stay as they are.
Component tokens
A component token is a custom property declared on the component itself. It gives the component a small, deliberate set of adjustable values. Its default usually comes from the shared scale.
:root {
--space-2: 0.5rem;
--space-3: 0.75rem;
--space-4: 1rem;
}
.card {
/* Component token with a default taken from the shared scale */
--card-padding: var(--space-4);
padding: var(--card-padding);
}
.card--compact {
--card-padding: var(--space-2);
}
.badge {
/* Fallback used only when --badge-padding is not defined */
padding: var(--badge-padding, var(--space-3));
}The .card--compact variant overrides one value on the element. Because custom properties inherit, any child of that card that reads --card-padding sees the new value too. The .badge rule shows the other pattern: the second argument of var() is a fallback, so the badge works with no setup. A parent can still set --badge-padding to customize it.
Rules that keep the tiers useful
- Components read semantic tokens, not primitives. If a component uses
--color-blue-500directly, a theme cannot change it. - Themes override semantic tokens, not primitives. The primitive palette stays the same in every theme.
- Name semantic tokens by role (
--color-surface,--color-text), not by appearance (--color-light-gray). A role name stays true when the value changes. - Name component tokens with the component as a prefix (
--card-padding), so they are easy to search for and unlikely to collide.
Checking your work
Open the browser's dev tools and inspect an element. The Computed pane shows the resolved value. The Styles pane shows the var() chain you wrote. If a button is the wrong color, follow it from --button-bg to --color-accent to --color-blue-500 and look for the first link that is undefined or overridden.
Exercise
A .alert component sets background: var(--color-red-600) directly. A dark theme overrides only semantic tokens. Why does the alert look wrong in dark mode, and how do you fix it?
Show answer
--color-red-600 is a primitive, and the dark theme does not change primitives, so the alert keeps its light-mode color. Add a semantic token such as --color-danger: var(--color-red-600) and use it in .alert. The dark theme can then point --color-danger at a different primitive.