Sub topic 9
Building spacing and type scales with calc()
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know the token tiers from earlier chapters. Here you derive a whole scale from one base token, so a single edit rescales every step.
Why derive a scale
A spacing scale written as a list of fixed values has no link between its steps. If you decide the base unit should grow from 4px to 5px, you must edit every line. A derived scale stores the relationship instead of the result. Each step is the base multiplied by a factor, so changing the base changes all of them.
CSS does this with calc(), which accepts var() references inside it.
A spacing scale from one base
:root {
--space-base: 0.25rem;
--space-1: var(--space-base);
--space-2: calc(var(--space-base) * 2);
--space-4: calc(var(--space-base) * 4);
--space-8: calc(var(--space-base) * 8);
}The step name is the multiplier, so --space-4 is four base units. With a base of 0.25rem, --space-4 is 1rem. Set --space-base to 0.3125rem and --space-4 becomes 1.25rem, with no other edit.
Using rem ties the scale to the user's root font size, so it also respects browser font settings.
.card {
padding: var(--space-4);
margin-block-end: var(--space-8);
}Rules for calc() with tokens
- In
calc(), multiplication needs at least one unitless number.var(--space-base) * 2is valid.var(--space-base) * var(--space-base)is not, because both sides have a length unit. - Division needs a unitless number on the right side.
- Addition and subtraction need operands of compatible types.
calc(1rem + 4)is invalid. - Spaces are required around
+and-.*and/do not need them, but spaces keep the code readable.
A type scale with a ratio
A type scale usually grows by a constant ratio. Store the ratio as a unitless number, then multiply.
:root {
--font-size-base: 1rem;
--type-ratio: 1.25;
--font-size-0: var(--font-size-base);
--font-size-1: calc(var(--font-size-0) * var(--type-ratio));
--font-size-2: calc(var(--font-size-1) * var(--type-ratio));
--font-size-3: calc(var(--font-size-2) * var(--type-ratio));
}
h3 { font-size: var(--font-size-1); }
h2 { font-size: var(--font-size-2); }
h1 { font-size: var(--font-size-3); }Each step builds on the previous one. With a ratio of 1.25, --font-size-3 resolves to about 1.953rem. Changing --type-ratio to 1.333 gives a larger, more dramatic scale everywhere.
Where the calculation happens
The diagram shows how one base value flows into every step.
Scoped rescaling
Because the scale is derived, you can change density in a scope by overriding only the base.
.compact {
--space-base: 0.1875rem;
}There is a catch. --space-4 is declared on :root with calc(var(--space-base) * 4). Custom properties that contain var() are resolved on the element where they are declared, then the resulting value is inherited. So .compact changes --space-base, but its children still inherit the already resolved --space-4 from :root. The override has no visible effect on the steps.
To make scoped rescaling work, redeclare the steps in the scope:
.compact {
--space-base: 0.1875rem;
--space-1: var(--space-base);
--space-2: calc(var(--space-base) * 2);
--space-4: calc(var(--space-base) * 4);
--space-8: calc(var(--space-base) * 8);
}Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| `calc(var(--space-base) * 2rem)` | Invalid, since both operands have units | Multiply by a unitless number |
| Ratio stored as `1.25rem` | Multiplying two lengths fails | Store the ratio as `1.25` |
| `calc(var(--space-base)*2)` written without spaces around `+` or `-` | Parse error for addition | Add spaces around `+` and `-` |
| Overriding the base in a child scope | Derived steps do not change | Redeclare the steps in that scope |
Exercise
A page defines --space-base: 0.25rem and --space-3: calc(var(--space-base) * 3) on :root. A .dense section sets only --space-base: 0.125rem. Elements inside .dense use padding: var(--space-3). What padding do they get, and how would you fix it so the padding follows the new base?
Show answer
They get 0.75rem, because --space-3 was resolved on :root using the base of 0.25rem and the resolved value is inherited. To fix it, redeclare --space-3: calc(var(--space-base) * 3) inside .dense, which gives 0.375rem, or write padding: calc(var(--space-base) * 3) at the point of use.