Sub topic 9
Styles, tokens, and reusable components
Repetition only works if every repeated choice is the same choice. This chapter shows how to store those choices once and reuse them. It assumes you know basic typography terms, can read simple CSS, and have used a design tool such as Figma or Canva. The previous chapter covered repetition as a principle. Here you will see how design systems make it automatic.
The problem: repetition by hand drifts
When you set a button color by eye on every screen, small errors creep in. One blue is #0b5fff and another is #0a60ff. Spacing of 15px appears next to spacing of 16px. Each difference is tiny, but together they make an interface look unplanned.
The fix is to name each decision once and refer to the name. Three related ideas do this:
- A style is a saved set of properties, such as a text style "Heading 2" (size, weight, line height) or a color style "Accent".
- A token is a named design value, such as
--color-accentor--space-md. Tokens are the smallest unit of a design system. - A component is a reusable piece of interface, such as a button or card, built from tokens and styles.
Together, tokens, styles, and components make up a basic design system: a shared set of rules and parts that keep a product consistent.
Tokens in CSS
CSS custom properties are a direct way to write tokens. You define them once on :root, then reference them with var().
:root {
--color-text: #1a1a1a;
--color-accent: #0b5fff;
--space-sm: 8px;
--space-md: 16px;
--space-lg: 32px;
}
body {
color: var(--color-text);
font-family: system-ui, sans-serif;
}
.button {
background: var(--color-accent);
color: #ffffff;
padding: var(--space-sm) var(--space-md);
}
.link {
color: var(--color-accent);
}The button and the link share --color-accent. If your brand color changes, you edit one line and both update. The spacing tokens also give you a small set of allowed gaps (8, 16, 32), which keeps proximity decisions consistent: related items get --space-sm, separate groups get --space-lg.
Tokens and styles in design tools
Design tools use the same idea with different names. Figma calls them variables and styles. Canva has brand kits. Illustrator and InDesign have swatches, paragraph styles, and character styles. The workflow is the same in each:
- Pick a small set of values: a few colors, a type scale, and a spacing scale.
- Save each as a named style or variable.
- Apply the name, never a loose value.
- To change the design, edit the style, not each object.
Name by role, not by value
Good token names describe purpose. --color-accent survives a rebrand. --color-blue becomes a lie the day the accent turns green. Compare:
| Weak name | Better name | Why |
|---|---|---|
| `--blue` | `--color-accent` | The role stays valid if the hue changes |
| `--16px` | `--space-md` | The size can change without renaming |
| `--big-bold` | `--heading-2` | It says where the style is used |
Components: repeated structure
A component bundles tokens into a unit you can reuse. The card from the proximity chapter is a good example. Define it once, and every card gets the same internal gap and heading style.
.card {
display: grid;
gap: var(--space-sm);
padding: var(--space-md);
}
.card + .card {
margin-top: var(--space-lg);
}This snippet assumes the tokens from the earlier block are in the same stylesheet. The component applies proximity (small gap inside, large gap between cards) and repetition (every card behaves the same) at once.
How the pieces connect
Common errors
- Hard-coding values. Typing
#0b5fffinto ten rules defeats the token. Usevar(--color-accent). - Too many near-duplicates. Tokens like
--space-14,--space-15, and--space-16recreate the drift you wanted to remove. - Detaching components. In a design tool, overriding a component on one screen breaks the link to the original. Edit the main component instead, unless the exception is deliberate.
- Naming by appearance. Names tied to a color or size break when the design changes.
Exercise
A stylesheet uses #0b5fff in 12 places and 16px padding in 20 places. The brand color changes to a darker blue. Describe how you would restructure it, and name the tokens you would create.
Show answer
Define tokens on :root, for example --color-accent: #0b5fff; and --space-md: 16px;. Replace each hard-coded #0b5fff with var(--color-accent) and each 16px padding with var(--space-md). Then change the color by editing the single --color-accent line. The names describe roles, so they stay correct after the color changes.
Exercise
Why is --color-blue a weaker token name than --color-accent?
Show answer
--color-blue describes the current value. If the accent becomes green, the name is wrong and misleads readers. --color-accent describes the role, so it stays accurate when the value changes.