Sub topic 2
Declaring and using custom properties
This chapter assumes you know basic CSS selectors, properties and values, and how a stylesheet is linked to a page. You should also know roughly how the cascade picks a winning declaration. The cascade itself is covered in the next chapter.
Declaring a custom property
A custom property is a property whose name you choose. The name must start with two dashes. You declare it inside a rule, the same way you declare color or margin.
:root {
--color-accent: #2563eb;
--space-4: 1rem;
--font-body: system-ui, sans-serif;
}:root matches the <html> element. Declaring tokens there makes them available to every element in the document. Names are case-sensitive, so --Color-Accent and --color-accent are two different properties.
Using a value with var()
The var() function reads a custom property and substitutes its value.
.button {
background: var(--color-accent);
padding: var(--space-4);
font-family: var(--font-body);
}If you change --color-accent in one place, every rule that reads it changes too. This is the main reason custom properties work well for design tokens.
What a custom property can hold
Browsers do not check a custom property's value when you declare it. It can be a color, a length, a list, or even a fragment of a value. The browser stores it as text and checks it only after var() substitutes it into a real property.
:root {
--shadow-card: 0 1px 3px rgb(0 0 0 / 20%);
--border-default: 1px solid #d1d5db;
--columns: 3;
}
.card {
box-shadow: var(--shadow-card);
border: var(--border-default);
grid-template-columns: repeat(var(--columns), 1fr);
}You can also use var() as part of a larger value, or inside calc().
.panel {
margin: var(--space-4) auto;
padding-inline: calc(var(--space-4) * 2);
}Where var() works and where it does not
var() replaces values only. It cannot build the other parts of a rule.
| You want to substitute | Allowed? | Example |
|---|---|---|
| A whole value | Yes | `color: var(--color-text)` |
| One part of a value | Yes | `margin: var(--space-4) auto` |
| Inside `calc()` | Yes | `calc(var(--space-4) * 2)` |
| A property name | No | `var(--prop): red` |
| A selector | No | `var(--sel) { }` |
| A unit glued to a number | No | `var(--n)px` |
The last row catches many people. var(--n)px does not become 16px. Store the number and multiply it by a unit instead.
:root {
--gap: 16;
}
.list {
gap: calc(var(--gap) * 1px);
}How the browser resolves a token
The check in step D is the one that surprises people. A token such as --space-4: banana is accepted. The failure appears only when padding: var(--space-4) is evaluated. In that case the property behaves as if it were unset, and the browser reports no error.
Putting it together
This small stylesheet uses only what this chapter introduced.
:root {
--color-text: #111827;
--color-surface: #f9fafb;
--color-accent: #2563eb;
--space-4: 1rem;
}
body {
color: var(--color-text);
background: var(--color-surface);
}
a {
color: var(--color-accent);
padding: calc(var(--space-4) / 2);
}Exercise
You declare --size: 12 on :root and want a font size of 12 pixels. Why does font-size: var(--size)px fail, and how do you fix it?
Show answer
var() substitutes the text 12 and the trailing px is not part of the value, so the declaration is invalid. Use font-size: calc(var(--size) * 1px), or store the unit in the token with --size: 12px and write font-size: var(--size).