Sub topic 5
Building scales with calc() and other functions
This chapter assumes you can declare a custom property and read it with var(), and that you know how inheritance and the cascade work. Here you use custom properties as the inputs to math, so that one base value produces a whole set of consistent sizes and colors.
Why compose values
A design usually has a few base decisions: a spacing unit, a type size, a brand hue. Everything else is derived from them. If you write every derived value by hand, changing the base means editing dozens of rules. If you derive them with calc(), you change one property.
Custom properties work well here because var() is substituted before the browser evaluates the surrounding function. calc(var(--space) * 2) becomes calc(8px * 2) and then 16px.
A spacing scale with calc()
Store the base unit and multiply it.
:root {
--space: 8px;
--space-2: calc(var(--space) * 2);
--space-3: calc(var(--space) * 3);
--space-4: calc(var(--space) * 4);
}
.card {
padding: var(--space-3);
margin-block-end: var(--space-4);
gap: var(--space);
}Changing --space to 10px rescales every step. You can also override --space on a smaller scope, such as a compact sidebar, and every step used inside it shrinks with it. The derived properties are re-resolved where they are used, because --space-2 stores the unevaluated calc(var(--space) * 2) text, and the var() inside it is resolved on each element that inherits it.
Be careful with that last point. A custom property that is not registered inherits as a token stream, but var() references inside it are substituted at the place where the property is declared. So if --space-2 is declared on :root, an override of --space on .sidebar does not change --space-2 inside .sidebar. To get the rescaling, redeclare the derived properties in the scope too, or declare them on a selector that matches every element, such as *.
.sidebar {
--space: 4px;
--space-2: calc(var(--space) * 2);
--space-3: calc(var(--space) * 3);
}Rules for math inside calc()
calc() has type rules that custom properties do not relax.
- Addition and subtraction need operands of the same kind, such as length with length. Spaces are required around
+and-. - In multiplication, at least one side must be a plain number.
var(--space) * 2works.var(--space) * var(--space)does not, because both are lengths. - In division, the right side must be a number.
- Unitless numbers in custom properties are useful as multipliers:
--ratio: 1.25.
A type scale
A modular type scale multiplies a base size by a ratio repeatedly. Store the ratio as a number and chain steps.
:root {
--text-base: 1rem;
--ratio: 1.25;
--text-1: calc(var(--text-base) * var(--ratio));
--text-2: calc(var(--text-1) * var(--ratio));
--text-3: calc(var(--text-2) * var(--ratio));
}
h3 { font-size: var(--text-1); }
h2 { font-size: var(--text-2); }
h1 { font-size: var(--text-3); }Here --text-base is a length and --ratio is a number, so each multiplication is valid.
Other functions
calc() is not the only function that accepts var().
min(),max()andclamp()bound a value.clamp(1rem, 2.5vw, 2rem)gives fluid text with limits.rgb(),hsl()andcolor-mix()accept custom properties as components.
Storing color channels separately is a common pattern. With a hue in a property, you can derive a palette from one number.
:root {
--hue: 220;
--primary: hsl(var(--hue) 80% 45%);
--primary-light: hsl(var(--hue) 80% 85%);
--primary-dark: hsl(var(--hue) 80% 25%);
--title-size: clamp(1.5rem, 4vw, 3rem);
}
.banner {
background: var(--primary-light);
color: var(--primary-dark);
border: 2px solid var(--primary);
font-size: var(--title-size);
}Changing --hue to 10 turns the whole palette red, with the same lightness relationships.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| `calc(var(--space)*2)` with no spaces around `+` or `-` | Invalid for `+` and `-` only. `*` and `/` allow no spaces. | Always put spaces around `+` and `-`. |
| `calc(var(--a) * var(--b))` with two lengths | Declaration invalid at computed-value time | Make one operand unitless. |
| `--size: 10` then `width: var(--size)` | `10` is not a valid width | Use `calc(var(--size) * 1px)`. |
| Overriding a base property in a child scope | Derived values do not change | Redeclare the derived properties in that scope. |
Exercise
You declare --space: 8px and --space-2: calc(var(--space) * 2) on :root. You then set --space: 4px on .sidebar and use padding: var(--space-2) inside it. What padding do you get, and how do you fix it so it is 8px?
Show answer
The padding is 16px. --space-2 was resolved on :root using --space: 8px, and the computed result is inherited. To fix it, redeclare --space-2: calc(var(--space) * 2) inside .sidebar, so it resolves against 4px and gives 8px.
Exercise
Why does width: calc(var(--n) * var(--n)) fail when --n: 10px, and how can you change it so it works?
Show answer
Multiplying two lengths is not allowed in calc(), so at least one operand must be a number. Store --n: 10 as a number and write calc(var(--n) * var(--n) * 1px).