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 how they inherit, and you know the primitive and semantic token tiers from earlier chapters. Here you will derive whole scales from a few base tokens, so one edit rescales an interface.
Why derive a scale
A spacing scale written as a list of fixed values works, but the values are unrelated to each other. If you decide the whole interface should be a little tighter, you edit every line.
A derived scale stores the rule instead of the results. You keep one base value, and calc() produces each step from it. The base token becomes the single knob for the whole scale.
Derivation has a cost. Every step is a formula, so the numbers you get are the numbers the formula gives. Hand-picked values can follow a designer's intent more closely. Later sections cover when each approach fits.
A spacing scale from one base
The simplest derived scale is linear. Each step is a multiple of a base unit.
: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);
}With a root font size of 16px, these resolve to 4px, 8px, 16px and 32px. Change --space-base to 0.3rem and every step grows by the same proportion.
The step names (-1, -2, -4, -8) are multiples of the base, so the name tells you the math. This works for spacing because the relationship is a simple count of units.
The rules of calc() with tokens
calc() is strict about types. These rules cause most scale bugs:
| Operation | Rule | Valid example | Invalid example |
|---|---|---|---|
| `+` and `-` | Both sides must have compatible types, and spaces are required around the operator | `calc(var(--a) + 1rem)` | `calc(var(--a)+1rem)` |
| `*` | At least one side must be a plain number | `calc(1rem * 2)` | `calc(1rem * 2rem)` |
| `/` | The right side must be a plain number | `calc(1rem / 1.25)` | `calc(1rem / 1rem)` |
A custom property has no type of its own until it is substituted into a property. --ratio: 1.25 is just text, and it becomes a number only when calc() receives it. If the substituted text breaks one of the rules above, the declaration fails. The failure rules are in the debugging chapter, and the short version is that the property using the token falls back to its inherited or initial value.
Multiplier tokens
You can pull the multiplier out into its own token. This is useful when a component wants a step that is not in the scale.
:root {
--space-base: 0.25rem;
}
.callout {
--callout-gap: 3;
padding: calc(var(--space-base) * var(--callout-gap));
}This keeps the component tied to the base while allowing a one-off multiple. Use it sparingly. A step that several components need belongs in the scale.
Rescaling with a scope, and the inheritance trap
Deriving from a base suggests a neat trick: override --space-base in a container to make that region denser. This does not work the way most people expect.
:root {
--space-base: 0.25rem;
--space-2: calc(var(--space-base) * 2);
}
.compact {
--space-base: 0.1875rem;
}
.compact .item {
padding: var(--space-2); /* still 0.5rem, not 0.375rem */
}The reason is when var() is resolved. On :root, --space-2 is computed with --space-base already substituted, giving calc(0.25rem * 2). Descendants inherit that computed value. The new --space-base on .compact is never consulted, because --space-2 is not declared again there.
The fix is to declare the derived tokens on every element that may redefine the base. A selector list does this without duplicating the formulas.
:root {
--space-base: 0.25rem;
}
[data-density="compact"] {
--space-base: 0.1875rem;
}
:root,
[data-density="compact"] {
--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);
}Now each element that carries data-density="compact" recomputes the steps from its own base. Any element inside that region inherits the compact values.
A type scale from a ratio
Type scales usually follow a modular ratio. Each step is the previous one multiplied by a constant, such as 1.25 for a major third. CSS custom properties can express this with chained multiplication, because each step references its neighbor.
:root {
--text-ratio: 1.25;
--text-base: 1rem;
--text-sm: calc(var(--text-base) / var(--text-ratio));
--text-lg: calc(var(--text-base) * var(--text-ratio));
--text-xl: calc(var(--text-lg) * var(--text-ratio));
--text-2xl: calc(var(--text-xl) * var(--text-ratio));
}
h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }
p { font-size: var(--text-base); }
small { font-size: var(--text-sm); }The ratio must be a plain number so it can serve as the divisor and multiplier. With a 1rem base, the steps resolve as follows.
| Token | Formula | Result |
|---|---|---|
| `--text-sm` | 1rem / 1.25 | 0.8rem |
| `--text-base` | 1rem | 1rem |
| `--text-lg` | 1rem * 1.25 | 1.25rem |
| `--text-xl` | 1.25rem * 1.25 | 1.5625rem |
| `--text-2xl` | 1.5625rem * 1.25 | 1.953125rem |
Chaining is deliberate. Each step depends on the one before it, so the whole scale follows the ratio exactly. Because each token still carries a nested calc() expression, you may see long computed values in developer tools.
Changing the ratio responsively
The ratio and the steps are declared on the same element here, so a media query can change the ratio and every step follows. This avoids the inheritance trap from the previous section.
@media (min-width: 60rem) {
:root {
--text-ratio: 1.333;
}
}Small screens keep the tighter 1.25 ratio, and wide screens get a more dramatic one. Nothing else in the stylesheet changes.
A fluid base
The base itself does not have to be fixed. clamp() can grow it between two limits as the viewport widens, and the scale grows with it.
:root {
--text-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
}Because clamp() and calc() nest freely, each derived step scales fluidly with no further code. Check the lower and upper bounds against your readability requirements, and make sure the middle expression still lets users zoom text. A middle term made only of vw does not respond to the user's font-size setting, which is why this one mixes rem and vw.
Pairing line height
Line height behaves best as a unitless number, so it does not need calc() to follow the font size. A unitless value is multiplied by the element's own font size.
:root {
--leading-tight: 1.2;
--leading-body: 1.6;
}
h1 { font-size: var(--text-2xl); line-height: var(--leading-tight); }
p { font-size: var(--text-base); line-height: var(--leading-body); }Do not store a computed length such as 1.6rem as a line-height token. A length stays fixed when the font size changes, which breaks the scale.
Common mistakes with scale tokens
- Unitless zero in addition. A token defined as
0and then used incalc(var(--gap) + 1rem)is invalid, because a number and a length cannot be added. Define the token as0remor0px. - Missing spaces around
+and-.calc(var(--a)-1rem)is invalid. The spaces are required. - Negative values. Write
calc(var(--space-4) * -1). Writing-var(--space-4)is not valid CSS. - Overriding only the base. As shown above, derived tokens declared elsewhere keep their old values.
- Mixing
emandremin one scale.emdepends on the element where the value is used, so the same token can resolve to different sizes in different places. Keep shared scales inrem.
When to derive and when to list
| Situation | Better choice |
|---|---|
| A uniform grid of multiples, such as 4px units | Derive from a base |
| A type scale with one governing ratio | Derive from a ratio |
| Designers hand-tune each step, or steps break the pattern | List explicit values |
| Several themes need different relationships between steps | List per theme |
Mixing the two is common. Derive the spacing scale and list the type scale, or the other way around, depending on what the design system defines. Whichever you choose, keep the output names stable so components do not care how a value was produced.
Exercise
A page sets --space-base: 0.25rem and --space-2: calc(var(--space-base) * 2) on :root. A .compact section sets --space-base: 0.125rem, but items inside it using var(--space-2) still get 0.5rem. Why, and what is the smallest change that fixes it?
Show answer
--space-2 was resolved on :root with the old base already substituted, and descendants inherit that computed value. The new base on .compact is never used. Redeclare --space-2 (and the other derived steps) on .compact, for example by moving the step formulas into a rule with the selector list :root, .compact.
Exercise
With --text-ratio: 1.25 and --text-base: 1rem, what does --text-xl resolve to when it is defined as calc(var(--text-lg) * var(--text-ratio)) and --text-lg is calc(var(--text-base) * var(--text-ratio))? Then say what happens if --text-ratio is declared as 1.25rem.
Show answer
--text-xl is 1rem 1.25 1.25 = 1.5625rem. If the ratio is 1.25rem, the multiplication has two lengths, which calc() does not allow, so the declaration is invalid at computed-value time and the property using it does not get the intended size.