Sub topic 1
What design tokens are and why they help
Prerequisites
This chapter assumes you can read basic CSS selectors, properties and values. It also assumes you know the cascade and specificity well enough to know that a later or more specific rule can win. You should be able to link a stylesheet to an HTML page. No JavaScript is needed here.
What a design token is
A design token is a named value that stores one design decision. Examples are a brand color, a spacing step, a font size or a corner radius. The name describes the role of the value, so you refer to --color-accent and not to #2563eb.
Tokens are a design idea, not a CSS feature. CSS custom properties are the browser's built-in way to implement them. A custom property is a property whose name starts with --, which you read elsewhere with var().
The problem with hard-coded values
Suppose a stylesheet repeats the same blue in many places:
.button { background: #2563eb; }
.link { color: #2563eb; }
.badge { border: 1px solid #2563eb; }This causes three problems:
- Changing the value means searching. A rebrand requires finding every copy of the hex code. A search can miss near-duplicates such as
#2564eb. - The value has no meaning. A reader cannot tell whether
#2563ebis the brand color or a coincidence. - Drift. Over time, people add slightly different blues, and the interface becomes inconsistent.
The same styles with tokens
Define the value once and give it a name:
:root {
--color-accent: #2563eb;
}
.button { background: var(--color-accent); }
.link { color: var(--color-accent); }
.badge { border: 1px solid var(--color-accent); }:root matches the html element. Custom properties inherit, so every element in the page can read the token. Changing the one line in :root updates all three rules.
Why tokens help
- One source of truth. Each decision lives in one place. Designers and developers can refer to the same names.
- Consistency. Components pick from a limited set of values instead of inventing new ones.
- Safe change. You can adjust a value without touching component rules.
- Theming. Because a token is an ordinary custom property, you can give it a different value inside a scope. Later chapters use this for dark mode and per-section themes.
- Runtime access. Unlike Sass variables, custom properties exist in the browser. They take part in the cascade and JavaScript can read and change them.
What tokens cover
Tokens are not limited to color. Common categories are:
| Category | Example token | Example value |
|---|---|---|
| Color | `--color-text` | `#111827` |
| Spacing | `--space-4` | `1rem` |
| Typography | `--font-size-lg` | `1.25rem` |
| Shape | `--radius-md` | `0.5rem` |
| Motion | `--duration-fast` | `150ms` |
A common mistake: tokens for everything
A token should capture a decision that is shared or likely to change. Turning every one-off number into a token produces a long list of names that nobody can navigate. If a value is used once and has no design meaning, a plain value is fine. Later chapters cover naming and the primitive, semantic and component tiers, which keep a larger set of tokens organized.
Check your understanding
Exercise
A stylesheet uses #2563eb in 40 rules. Name two problems this causes, and describe how a token fixes them.
Show answer
Changing the color means finding and editing all 40 places, and the hex code does not say what the color is for, so values can drift into near-duplicates. With a token such as --color-accent: #2563eb; defined once in :root and used as var(--color-accent), you edit one line to update every rule, and the name states the role of the value.
Exercise
Why can a CSS custom property support theming when a Sass variable cannot?
Show answer
A Sass variable is resolved at build time and no longer exists in the browser. A custom property remains in the page and follows the cascade, so it can be given a different value in a different scope, such as under a [data-theme="dark"] selector, and it can also be changed at runtime.