The apps on your phone can switch to dark mode in a moment. Backgrounds, text and buttons all change together, and nobody repaints them one by one.
Here's how: every design value (a colour, a spacing, a radius, a font size, a shadow) is stored once, with a name. Each part of the page that needs the value uses the name, not the number. Those named values are called design tokens. Design tokens are like Mrs. Sri's paint notebook. On the first page are codes from the shop's catalogue: "Leaf Green 700", "Mist Grey 200". On the second page are the jobs: "room doors: Leaf Green 700", "walls: Mist Grey 200". The painter only reads the second page. If Mrs. Sri wants the doors a lighter green, she changes one line on the second page, and the catalogue stays as it is. With design tokens, the page's code plays the painter: the code reads only the names, so changing a value takes one line.
The problem: the same value in many places
Before tokens, Mrs. Sri's green #15803d was written in five places: the booking button's background, the active night button's background (the button for choosing the number of nights), its border, the colour of the "See the cancellation rules" link, and the focus ring. Changing the brand colour meant finding all five. You also had to hope nobody had typed #16803d by mistake. The 16px spacing and the 12px radius were spread across many CSS rules too.
A token stores each design decision (a value that has been chosen) once, with a name. Every other rule calls that name.
Design tokens in CSS: custom properties
In CSS, a token is written as a custom property, which you already know as a CSS variable. This code makes two tokens, then uses them on the booking button (.book) and on the box around the booking page (.mist):
:root {
--action: #15803d; /* declaration: the name starts with two hyphens */
--space-4: 1rem; /* 16px at the browser's default font size */
}
.book { background: var(--action); } /* var() reads the value */
.mist { padding: var(--space-4, 1rem); } /* the second value: a fallback if the token is missing */
:rootis the selector for the<html>element, the outermost element of the page. Custom properties are inherited by every element inside it, so a token written on:rootcan be read across the whole page.var(--action)reads the value of the token--action. When the browser calculates how an element looks, it replacesvar(--action)with the value, here#15803d. Token names are case-sensitive:--Actionis a different token.1remis 16px at the browser's default font size. So--space-4: 1remgives 16px of padding, and--radius-md: 0.5remgives an 8px radius.
In the exercise, the starter has a card .card with a heading and a paragraph. It also has a <style> tag (where CSS is written inside HTML) with an empty :root and an empty .card. Fill :root with --color-primary: #2563EB, --color-text: #111827, --space-4: 1rem and --radius-md: 0.5rem. Then write .card { padding: var(--space-4); border-radius: var(--radius-md); }. The result is 16px of padding and an 8px radius, and each number is written only once.
Three tiers: primitive, semantic, component
Tidy tokens are layered. Each tier may only point at the tier in the row before it in this table. Pointing means the value is taken from another token through var(): semantic points at primitive, component points at semantic.
| Tier | What it holds | Example | Read by |
|---|---|---|---|
| Primitive | every raw value, named by colour or size | --green-700: #15803d |
semantic tokens |
| Semantic | named by its job | --action: var(--green-700) |
component tokens, CSS rules |
| Component | owned by one component | --book-bg: var(--action) |
that component's rules |
The code below writes the three tiers for Mrs. Sri's booking page. The primitive tier holds the green colour scale 50 to 900 (called a ramp in the code comment) and this page's greys. At the very bottom, three component rules use the tokens: the .mist box, the .book booking button, and the active night button ([aria-pressed="true"]).
:root {
/* 1. primitive: the green ramp 50 to 900 (Tailwind v3's values) and this page's greys */
--green-50: #f0fdf4; --green-100: #dcfce7; --green-200: #bbf7d0; --green-300: #86efac; --green-400: #4ade80;
--green-500: #22c55e; --green-600: #16a34a; --green-700: #15803d; --green-800: #166534; --green-900: #14532d;
--white: #ffffff; --slate-100: #f1f5f9; --slate-200: #e2e8f0; --slate-400: #94a3b8; --slate-600: #475569;
--gray-700: #374151; --gray-800: #1f2937; --gray-900: #111827;
--space-3: 0.75rem; --space-4: 1rem; --radius-md: 0.5rem; --radius-lg: 0.75rem;
/* 2. semantic: named by job */
--surface-page: var(--slate-100);
--surface-card: var(--white);
--border: var(--slate-200);
--text: var(--gray-800);
--text-muted: var(--slate-600);
--action: var(--green-700);
--on-action: var(--white);
/* 3. component: owned by one component */
--card-bg: var(--surface-card);
--card-padding: var(--space-4);
--card-radius: var(--radius-lg);
--book-bg: var(--action);
--book-text: var(--on-action);
--book-radius: var(--radius-md);
}
.mist { padding: var(--card-padding); background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--card-radius); }
.book { color: var(--book-text); background: var(--book-bg); border-radius: var(--book-radius); }
.nights [aria-pressed="true"] { color: var(--on-action); background: var(--action); border-color: var(--action); }
- Primitives are the catalogue in Mrs. Sri's notebook. They never change when the theme (the page's set of colours, such as light or dark) changes:
--green-700is always#15803d. Semantic tokens are the notebook's second page, which notes which value does which job.--actionmeans "everything that can be pressed". - Component tokens give one component names of its own. Say the booking button one day needs a different colour from links. Only
--book-bgis pointed at another token, and--actionis not disturbed. - Only one thing needs watching: component rules never mention a primitive token.
Dark theme: swap only the semantic tokens
Mrs. Sri's booking page is getting a dark version. In it, the booking button must be #22c55e with #111827 text, and the card #1f2937. With the three tiers above, how many lines of the component rules (.mist, .book, the night buttons) have to change? Guess first.
The dark version only points seven semantic tokens at other primitives. The code sits inside a media query (a block of CSS that applies only in certain conditions). prefers-color-scheme: dark applies when the user's device is set to dark mode:
@media (prefers-color-scheme: dark) {
:root {
--surface-page: var(--gray-900);
--surface-card: var(--gray-800);
--border: var(--gray-700);
--text: var(--slate-100);
--text-muted: var(--slate-400);
--action: var(--green-500);
--on-action: var(--gray-900);
}
}
The .mist, .book and night-button rules aren't touched. So the answer to the guess: 0 component lines. If the page has a theme switch, use the selector :root[data-theme="dark"] (the <html> element with the attribute data-theme="dark") with the same contents, as the Lab does.
The result in Chromium, WebKit and Firefox, measured with getComputedStyle (a JavaScript command that reads how an element finally looks):
| Light | Dark | |
|---|---|---|
| Booking button | #15803d, white text 5.02:1 |
#22c55e, #111827 text 7.79:1 |
| Button against the card (at least 3:1, WCAG 1.4.11) | 5.02:1 on the #ffffff card |
6.44:1 on the #1f2937 card |
That is the advantage of semantic tokens over raw tokens (another name for primitives): easy theme switching, without changing components. Compare a team whose components use raw tokens directly, say .book { background: var(--green-700); }. That component is coupled to the raw token. The dark block only changes semantic tokens, so that button doesn't change. It stays #15803d on the #1f2937 card, with a contrast of only 2.93:1, failing the 3:1 minimum. To fix it, the team must edit every component that names --green-700, for every theme. The cure is one semantic layer: components read --action, and a theme changes --action in one place.
Beyond CSS: the DTCG format
Tokens are often also kept in a JSON file (a text format for data). That way Figma (the app where designers draw designs), the Android and iOS apps and the CSS all read the same source. The format is written by the Design Tokens Community Group (DTCG), a group at the W3C (the organisation that writes web standards). The document is Design Tokens Format Module 2025.10 (a Final Community Group Report, 28 October 2025). The document itself says it is not a W3C Standard and is not on the W3C Standards Track. Files end in .tokens or .tokens.json. Here are the green 700 token and the action token in that format:
{
"color": {
"green": {
"700": {
"$type": "color",
"$value": { "colorSpace": "srgb", "components": [0.0824, 0.502, 0.2392], "hex": "#15803d" }
}
}
},
"action": {
"$type": "color",
"$value": "{color.green.700}",
"$description": "Everything that can be pressed: the book button, the night choice, links."
}
}
$valueis the token's value and$typeits kind. In version 2025.10 a colour value is an object (a group of values inside{ }). It holds a colour space (srgb), three numbers from 0 to 1 for red, green and blue (21/255, 128/255, 61/255), andhexas a fallback."{color.green.700}"is an alias: its value is taken from the green 700 token, just likevar(--green-700)in CSS.- Tools such as Style Dictionary (since version 4) turn this file into CSS custom properties. Full support for version 2025.10 is still in progress, so check your tool's version.
⚠️ Tokens that fail silently
- A misspelt token name. You write
padding: var(--space4), but the token is--space-4. The padding becomes 0px, with no error message in the console (the message panel in DevTools), in the three browsers we measured. A declaration that uses a missing variable doesn't apply, so padding goes back to its initial value. - Gluing a unit after
var().--radius: 8thenborder-radius: var(--radius)pxalso gives 0px. Keep the unit inside the token (0.5rem), or usecalc(var(--radius) * 1px)(calc()is a CSS function for doing maths). - Components reading primitives.
var(--green-700)in.bookkeeps the dark theme from reaching the button. - A theme for only part of the page. Say the promo section gets
.promo { --action: #22c55e; }. Component tokens on:rootare already calculated at:root, so--book-bgalready holds#15803dbefore.promoinherits it. A button readingvar(--book-bg)inside.promostays#15803d. Only elements that read--actiondirectly change. For a per-section theme, its component tokens must be written again in that section too. - Colour names on the semantic tier.
--greenfor the button stops making sense the day the brand colour changes to blue. Semantic tokens are named by job.
TL;DR: A design token is a design value (colour, spacing, radius, font size, shadow) stored once with a name and used by its name. In CSS, a token is a custom property (CSS variable) --name: value on :root (the <html> element), read with var(--name) (with a fallback: var(--name, value)). Build three tiers: primitive holds raw values (--green-700: #15803d), semantic is named by job (--action: var(--green-700)), and component belongs to one component (--book-bg: var(--action)). Components never read primitives. The dark theme swaps only 7 semantic tokens and 0 component lines: that is the advantage of semantic tokens, easy theme switching. Components using raw tokens (primitives) directly are coupled to the raw token: on the dark card, their button stays #15803d, with a contrast of only 2.93:1. In the exercise, :root gets --color-primary: #2563EB, --color-text: #111827, --space-4: 1rem, --radius-md: 0.5rem, then .card uses var(--space-4) and var(--radius-md) (16px and 8px). Beyond CSS, tokens are kept in a JSON file (a text format for data) in the DTCG's Design Tokens Format Module 2025.10 ($value, $type, the alias "{color.green.700}"). A misspelt token name fails silently: the result is 0px, with no error.
⚡ One value, one name
Write:
:root {
--action: #15803d; /* declaration: the name starts with two hyphens */
--space-4: 1rem; /* 16px at the browser's default font size */
}
.book { background: var(--action); } /* var() reads the value */
Meaning: A design token is a design value (colour, spacing, radius, font size, shadow) stored with a name, as a custom property (CSS variable) that starts with --. It's written once on :root (the <html> element, so the whole page can read it) and used through var(). Here .book, the booking button, gets the background #15803d.
⚡ Three tiers
Meaning: Tokens are built in three tiers. Primitives hold the raw values (--green-700: #15803d). Semantic tokens are named by their job (--action: var(--green-700), for everything that can be pressed). Component tokens belong to one component (--book-bg: var(--action), the booking button's background). Component rules never mention a primitive.
⚡ Dark theme: swap only the semantic tokens
Write:
@media (prefers-color-scheme: dark) {
:root {
--surface-card: var(--gray-800);
--action: var(--green-500);
--on-action: var(--gray-900);
}
}
Guess first: with three tiers of tokens, how many lines of the component rules (the page box .mist and the button .book) change for a dark theme?
Zero. Only semantic tokens are pointed at other primitives: 3 in this code, 7 for the whole booking page. That's the advantage of semantic tokens: easy theme switching.
Meaning: @media (prefers-color-scheme: dark) applies when the user's device is set to dark mode. It only points semantic tokens (the card background, the action colour, the text colour on the action colour) at dark primitives. A component that uses a raw token (a primitive) directly is coupled to it. A .book button that uses var(--green-700) stays #15803d on the dark card, with a contrast of only 2.93:1.
⚡ A typo fails silently
Meaning: Say you write padding: var(--space4), but the token is --space-4. The padding becomes 0px, with no error message in the console (the message panel in DevTools). A unit glued after var() also gives 0px, so keep the unit inside the token (0.5rem).