Sub topic 1
What design tokens are and why they help
A design token is a named value that records one design decision. Instead of writing #2563eb in forty places, you write it once, give it a name such as --color-accent, and refer to the name everywhere else. This chapter explains what tokens are, what problem they solve, and what they cost. Later chapters cover the CSS syntax in detail.
Prerequisites
This chapter assumes you can do the following:
- Read and write basic CSS: selectors, properties, and values such as colors, lengths, and font sizes.
- Explain what the cascade is and roughly how specificity decides which rule wins.
- Link a stylesheet to an HTML page.
You don't need JavaScript for this chapter. A later chapter on runtime access uses it.
What a design token is
A design token has three parts:
- A name that says what the value is for or what it is, such as
color-textorspace-4. - A value that the interface renders, such as
#111827or1rem. - A role in a shared system, meaning many components use it, and a team agrees on it.
The term comes from design systems work. Designers and developers needed a way to hand decisions to each other without screenshots and hex codes in chat messages. A token is the unit of that handoff. The design tool, the documentation, and the code all use the same name for the same decision.
Tokens are not tied to CSS. They can be stored as JSON, exported to Android or iOS, or written as Sass variables. This tutorial implements them with CSS custom properties, which are variables that the browser itself understands and resolves while the page is running.
The problem: hard-coded values
Here is a small stylesheet written without tokens.
.button {
background: #2563eb;
color: #ffffff;
padding: 12px 20px;
border-radius: 6px;
}
.link {
color: #2563eb;
}
.badge {
background: #2563eb1a;
color: #2563eb;
padding: 3px 10px;
border-radius: 6px;
}
.card {
padding: 16px;
border-radius: 6px;
border: 1px solid #e5e7eb;
}This code works, but it has problems that grow with the project.
- Repetition hides intent.
#2563ebappears four times. Nothing in the file says whether these are the same decision ("our brand blue") or four unrelated choices that happen to match. - Changes need search and replace. If the brand color changes, you search for the hex string. You will miss
rgb(37, 99, 235),#2563EBin uppercase, and any shade that was nudged by hand. - Near-duplicates creep in. One developer writes
12pxpadding, another writes0.75rem, a third writes13px. Each looks fine alone. Together they make the interface feel uneven. - Meaning is lost.
6pxon.cardand.buttoncould be a shared radius or a coincidence. Without a name, nobody knows whether changing one should change the other.
The same stylesheet with tokens
Now the same rules with named values.
:root {
--color-accent: #2563eb;
--color-accent-soft: #2563eb1a;
--color-on-accent: #ffffff;
--color-border: #e5e7eb;
--space-1: 0.25rem;
--space-2: 0.75rem;
--space-3: 1rem;
--space-4: 1.25rem;
--radius-medium: 0.375rem;
}
.button {
background: var(--color-accent);
color: var(--color-on-accent);
padding: var(--space-2) var(--space-4);
border-radius: var(--radius-medium);
}
.link {
color: var(--color-accent);
}
.badge {
background: var(--color-accent-soft);
color: var(--color-accent);
padding: var(--space-1) var(--space-2);
border-radius: var(--radius-medium);
}
.card {
padding: var(--space-3);
border-radius: var(--radius-medium);
border: 1px solid var(--color-border);
}The declarations in :root are the token definitions. The rest of the file only consumes them. Changing the brand color now means editing one line. The rule border-radius: var(--radius-medium) tells the reader that the button, badge, and card share a radius on purpose.
Two details are worth noting here. The syntax var(--name) reads the value of the custom property. Custom property names always start with two dashes and are case-sensitive, so --Color-Accent and --color-accent are different properties.
What kinds of values become tokens
Tokens usually cover the values that define the visual language of the interface. The table lists common categories.
| Category | Example token | Example value |
|---|---|---|
| Color | `--color-text` | `#111827` |
| Spacing | `--space-4` | `1rem` |
| Typography | `--font-size-300` | `1.125rem` |
| Font family | `--font-body` | `system-ui, sans-serif` |
| Radius | `--radius-medium` | `0.375rem` |
| Shadow | `--shadow-raised` | `0 1px 3px rgb(0 0 0 / 0.2)` |
| Motion | `--duration-fast` | `150ms` |
| Layering | `--z-modal` | `1000` |
Token values are not limited to single numbers or colors. A custom property can hold a shadow list, a font stack, or a duration. The browser stores whatever tokens you wrote and checks the value only when a property uses it. That flexibility is useful. It also causes some of the failure modes covered in the chapter on pitfalls.
How a token differs from an ordinary variable
Any CSS custom property can be used as a variable. A design token is a stricter idea. It is a variable that a team has decided to treat as a public part of its design language. That has practical consequences.
- Tokens are named by purpose or by scale position, not by one use.
--color-accentcan serve a button, a link, and a focus ring.--home-page-hero-button-blueserves one place and will not be reused. - Tokens are shared. If only one rule uses a value, it may not need a token yet.
- Tokens are stable. Other code depends on the name, so renaming a token is a breaking change, just like renaming a function in a library.
- Tokens are documented. The team can list them, preview them, and explain when to use each one.
CSS custom properties differ from Sass or Less variables in one important way. Sass variables are replaced at build time, so the compiled CSS contains plain values. Custom properties survive into the browser. They take part in the cascade and inheritance, and they can change when the page changes. This is what makes theming possible without rebuilding the stylesheet.
Why tokens help
One change, many results
When a decision lives in one place, changing it updates every consumer. A rebrand that touches the accent color, or an accessibility fix that raises text contrast, becomes an edit to a few token values.
Consistency by default
A limited set of names makes the consistent choice the easy one. A developer choosing padding picks from --space-1 to --space-4 instead of inventing a number. Reviewers can also spot a raw 13px in a pull request and ask why.
A shared vocabulary
Designers and developers use the same words. A design file that says "text on accent" maps to --color-on-accent in code. This reduces the number of questions that begin with "which gray is this?"
Themes without duplicated rules
Because custom properties cascade, you can redefine the same token names in a different scope, such as a dark section of the page, and every component inside picks up the new values. You will build this in the theming chapters. The point for now is that component rules stay unchanged.
Safer evolution
If components reference --color-accent rather than a literal, you can later swap the value, split the token into several, or point it at a different underlying color. Consumers keep working because they depend on the name.
Costs and failure modes
Tokens are not free. Knowing the costs helps you decide how far to take them.
- Indirection. A reader who sees
var(--space-3)must look up the value. Good names and browser developer tools reduce this cost. They don't remove it. - Too many tokens. A token for every one-off value turns the system into a second copy of the stylesheet. It adds names and no meaning.
- Bad names. A token called
--bluebreaks the day the accent becomes green. A name tied to a value instead of a role is hard to change without a rename across the codebase. - Runtime failures are quiet. If a token is misspelled or undefined, the browser doesn't raise an error. The declaration using it may silently do nothing. Debugging this is covered in a later chapter.
- Cascade surprises. Because tokens inherit, a token redefined in an ancestor changes everything beneath it. That's the feature, and also the source of many bugs.
Deciding what should be a token
Use these questions as a rule of thumb.
- Is the value a design decision, not a layout accident? A brand color or a spacing step qualifies. The exact width of one image column probably does not.
- Is it used in more than one place, or likely to be? Shared values earn names. A value used once can stay local until a second use appears.
- Would you want to change it everywhere at once? If yes, tokenize it. If different places should change independently, they need separate tokens or none.
- Does it need to vary by context? Values that change by theme, density, or screen size are strong candidates.
Start small. A first token set of colors, spacing steps, a radius, and a font stack is enough to gain most of the benefit. You can add more when a real need appears.
How the pieces connect
The following diagram shows how a design decision reaches the screen through a token.
Where this tutorial goes next
The next chapters build on this foundation in order. You will learn the syntax for declaring and using custom properties, then how they cascade and inherit, which decides where a token's value comes from. Then you will see fallback values, naming rules, and the layering of primitive, semantic, and component tokens. After that come theming, dark mode, scales built with calc(), typed tokens with @property, JavaScript access, debugging, and a pipeline for generating CSS from token source data.
Exercise
A stylesheet contains color: #2563eb in six rules, and border-radius: 6px in eight rules. The team wants to rebrand the blue and keep the radius flexible. Which values should become tokens, what would you name them, and why does the name --blue cause trouble?
Show answer
Both are good candidates because both are shared design decisions. Name the color by role, for example --color-accent, and the radius by scale or purpose, for example --radius-medium. --blue describes the current value, not the role. After a rebrand to green, the name would be false, or you would have to rename it in every file that uses it. A role-based name stays accurate when the value changes.
Exercise
Why can a custom property support theming when a Sass variable cannot, even though both give a value a name?
Show answer
A Sass variable is replaced with its plain value when the stylesheet is compiled, so the browser never sees the name. A custom property stays in the CSS and is resolved in the browser. It participates in the cascade and inheritance, so you can redefine the same name in another scope, such as a dark section or a data attribute, and every component inside that scope picks up the new value without any change to its own rules.
Exercise
A developer adds --home-hero-button-padding-left: 13px to :root and uses it in one rule. Is this a good token? What would you do instead?
Show answer
It is not a good token. It is named for one use, it is used once, and 13px is an arbitrary value that does not fit a spacing scale. Either keep the value local in the rule, or use an existing step such as var(--space-3). If the hero button really needs special spacing, define a component-level value near that component later, once a second use or a variant appears.