Sub topic 5
Naming tokens so they stay maintainable
This chapter assumes you can declare a custom property and read it with var(). It also assumes you know the cascade basics. Names are the part of a token system that is hardest to change later, so it is worth settling them early.
Why names matter
A token is a name attached to a value. Components depend on the name and never see the value, so the name is the public interface of your design system. If you change a value, nothing breaks. If you change a name, every var() that uses it breaks silently, because a missing custom property resolves to the guaranteed-invalid value and the declaration is dropped.
Good names do three jobs:
- They tell a developer which token to pick without opening the stylesheet.
- They stay true when the value changes.
- They are predictable, so a developer can guess a token's name before searching for it.
What a custom property name can contain
A custom property name starts with two hyphens. After that it can contain letters, digits, hyphens, underscores, non-ASCII characters, and escaped characters.
| Rule | Consequence |
|---|---|
| Names are case-sensitive | `--Color-Text` and `--color-text` are two different properties |
| A dot is not allowed unless escaped | A source name like `color.text.primary` must be converted to `--color-text-primary` |
| No spaces | Use hyphens to separate words |
| Must start with `--` | `-color-text` is an ordinary vendor-style identifier, not a custom property |
Pick one case style and keep it. Lowercase kebab-case is the common choice because it matches how CSS properties are written. The case-sensitivity rule is a frequent source of bugs. var(--Color-Text) will not find --color-text, and the browser will not warn you.
A structure for names
Name tokens as a sequence of segments, from the most general to the most specific. A common shape is:
--{namespace}-{category}-{item}-{variant}-{state}
Not every token needs every segment. The order matters more than the number of segments, because an ordered name sorts well. In an editor's autocomplete, typing --color-text- lists every text color together.
Here are some tokens built this way:
:root {
--color-text: #111827;
--color-text-muted: #6b7280;
--color-text-inverse: #ffffff;
--color-border: #d1d5db;
--color-border-focus: #2563eb;
--space-4: 1rem;
--radius-md: 0.375rem;
--font-size-lg: 1.125rem;
--shadow-card: 0 1px 3px rgb(0 0 0 / 0.15);
}Each name starts with a category (color, space, radius, font-size, shadow). Within a category, related tokens share a prefix (--color-text, --color-text-muted). That shared prefix is what makes the set easy to scan.
Keep the category list short
Choose a fixed list of categories and write it down. A typical list is color, space, font-family, font-size, font-weight, line-height, radius, border-width, shadow, z, duration, and easing. If one developer writes --spacing-4 and another writes --space-4, you now have two scales that look the same and drift apart.
Name by role, not by value
The most important rule is to describe what a token is for, not what it currently looks like. A name that contains its value becomes false when the value changes.
/* Fragile: the names describe the values */
:root {
--blue: #2563eb;
--light-gray: #f9fafb;
--space-16px: 16px;
}
.link {
color: var(--blue);
}
/* Durable: the names describe the roles */
:root {
--color-accent: #2563eb;
--color-surface: #f9fafb;
--space-4: 1rem;
}
.link {
color: var(--color-accent);
}Suppose the brand changes from blue to green. With --blue, you either leave a variable called blue holding a green value, or you rename it across the codebase. With --color-accent, you change one value.
Raw values still need names, and a value-based name is acceptable there. --color-blue-500 is a fine primitive name because it only claims to be a blue. What you should avoid is using that name directly in components. Primitive, semantic and component tokens are covered in the next chapter. For naming, remember this: the closer a token is to a component, the more its name should describe a role.
Roles describe purpose, not location
A role name should work in more than one place. --color-text-muted can be used for captions, placeholders and timestamps. --color-sidebar-caption-text is tied to one location, and the next developer who needs muted text on a card will either misuse it or invent a duplicate. Reserve location-specific names for component tokens, where the location is the point.
Naming scales
Some categories have a range of values: spacing, font sizes, radii, and color shades. There are two common ways to name the steps.
| Style | Example | Strength | Weakness |
|---|---|---|---|
| Numeric | `--space-1`, `--space-2`, `--space-4`, `--space-8` | Sorts correctly, easy to insert steps | The numbers do not say how big a step is |
| T-shirt sizes | `--font-size-sm`, `--font-size-md`, `--font-size-lg` | Reads naturally | Runs out of room: where does a size between `sm` and `md` go? |
Both work. Pick one per category and keep it. A few practical notes:
- For color shades, use a 50 to 900 numeric scale (
--color-gray-50through--color-gray-900). Lower numbers are lighter. This lets you add--color-gray-150later without renaming anything. - For spacing, number the steps by a multiple of a base unit. If
--space-4is1rembecause the base is0.25rem, the number tells you the size. This ties in with deriving scales usingcalc(), which a later chapter covers. - For t-shirt names, avoid stacking modifiers such as
--size-extra-extra-large. Usexs,sm,md,lg,xl,2xl. A name starting with a digit is fine inside a custom property, as in--font-size-2xl. - Do not bake units into names.
--space-16pxbecomes wrong the day you move torem.
Naming component tokens
Component tokens usually take the component name as their prefix. This keeps them from clashing with the shared scale and makes it clear who owns them.
.card {
--card-padding: var(--space-4);
--card-radius: var(--radius-md);
--card-bg: var(--color-surface);
padding: var(--card-padding);
border-radius: var(--card-radius);
background: var(--card-bg);
}
.card--compact {
--card-padding: var(--space-2);
}The names follow the same segment order as the global tokens: the owner first (card), then the property it controls (padding). A variant or state goes last: --button-bg-hover.
Keep state names consistent across components. Use hover, active, focus, disabled everywhere. Do not use --button-bg-hover in one place and --link-color-over in another.
Namespacing and collisions
Custom properties live in one global namespace per element's inherited scope. A third-party widget, a framework, or another team's stylesheet can define --color-primary or --spacing, and your values will silently overwrite theirs, or the reverse.
If your tokens will share a page with code you do not control, add a short prefix:
:root {
--ds-color-accent: #2563eb;
--ds-space-4: 1rem;
}
.button {
background: var(--ds-color-accent);
padding: var(--ds-space-4);
}The cost is longer names. If you ship a single application and own all the CSS, a prefix is optional. If you publish a component library or embed widgets, use one.
Renaming safely
Sooner or later a name will turn out to be wrong. Because a missing property fails silently, rename in two steps instead of one. First, add the new name and make the old name point to it.
:root {
/* New name */
--color-text-secondary: #6b7280;
/* Deprecated alias, remove after all usages are migrated */
--color-text-muted: var(--color-text-secondary);
}Existing var(--color-text-muted) calls keep working. Then search for the old name, migrate each usage, and delete the alias. A search such as grep -rn "\-\-color-text-muted" src/ finds both CSS usages and JavaScript strings that read the token by name.
Common naming mistakes
| Mistake | Example | Better |
|---|---|---|
| Value in the name | `--blue`, `--space-16px` | `--color-accent`, `--space-4` |
| Mixed case | `--Color-Text` | `--color-text` |
| Inconsistent category names | `--spacing-2` and `--space-4` | Pick one: `--space-*` |
| Location-specific shared token | `--sidebar-grey` | `--color-text-muted` |
| Vague names | `--color-1`, `--main` | `--color-surface`, `--color-text` |
| Unordered segments | `--hover-button-bg` | `--button-bg-hover` |
| Too many segments | `--ds-color-text-body-paragraph-muted-light` | `--ds-color-text-muted` |
A short naming checklist
- Does the name start with a category from your fixed list?
- Does the name say what the token is for, not what it looks like?
- Are segments ordered from general to specific?
- Is the case style the same as every other token?
- Does a similar token already exist under a different name?
The last question catches the most duplicates. Search for the value before you add a token.
Exercise
A stylesheet defines --light-blue: #dbeafe; and uses it for the background of error alerts. The design team now wants error alerts to be light red. Explain what is wrong with the name, and write two replacement tokens, one that names the raw color and one that names the role it plays in alerts.
Show answer
The name --light-blue describes a value, so after the change it would hold a red color and be misleading. Rename the raw color as a primitive that honestly describes it, and give the usage a role name: --color-red-100: #fee2e2; and --alert-error-bg: var(--color-red-100);. Components use --alert-error-bg, so later color changes only touch the token definition.
Exercise
A team has --color-text-muted used in 40 places. They decide --color-text-secondary is a better name. Describe how to rename it without breaking any page during the migration.
Show answer
Define --color-text-secondary with the real value. Redefine --color-text-muted as var(--color-text-secondary) so it acts as a deprecated alias. Migrate the 40 usages, including any JavaScript that reads the token by name. Then search the codebase again to confirm no usages remain, and delete the alias.