Sub topic 14
Building a reusable fluid scale with custom properties
This chapter assumes you know basic CSS syntax, the units rem and vw, and how clamp() resolves to a single value. You will define type and spacing sizes once as custom properties and reuse them across a stylesheet.
Why define a scale
Writing clamp(1rem, 0.5rem + 2.5vw, 2rem) on every heading is error-prone. If you change one heading's range, nothing keeps the others consistent. A fluid scale solves this. You name each size once, and every rule refers to the name.
A custom property is a value you name with a -- prefix and read with var(). The browser substitutes the stored text wherever you use var(). Because the stored text is a clamp() expression, the final value is recalculated when the viewport or the font size changes.
Defining the scale
Put the scale on :root so every element can use it.
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.25rem);
--step-1: clamp(1.25rem, 1.1rem + 0.75vw, 1.75rem);
--space-m: clamp(1rem, 0.75rem + 1.25vw, 2rem);
}
body { font-size: var(--step-0); }
h2 { font-size: var(--step-1); margin-block: var(--space-m); }Each step has a minimum, a preferred value, and a maximum. The preferred value mixes a rem term with a vw term. The rem term keeps the size tied to the user's font-size setting, as the accessibility chapter explains.
How the pieces resolve
The browser handles a scale value in three stages.
- It finds
var(--step-1)and substitutes the storedclamp()text. - It resolves
remandvwinto pixel lengths for the current root font size and viewport width. - It clamps the preferred value between the minimum and the maximum.
Reusing the scale
You can use the same custom property for several purposes. Spacing tokens often work for padding, margins, and gaps alike.
:root {
--space-s: clamp(0.5rem, 0.4rem + 0.5vw, 1rem);
--space-m: clamp(1rem, 0.75rem + 1.25vw, 2rem);
}
.card {
padding: var(--space-m);
}
.cards {
display: grid;
gap: var(--space-s);
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}Changing --space-m in one place now changes every card.
Common errors
- Mixing
var()and a missing property. If--step-2is never defined,font-size: var(--step-2)is invalid at computed-value time. The property falls back to its inherited or initial value, and the browser shows no error. Givevar()a fallback, such asvar(--step-2, 1.5rem), when a name might be absent. - Defining the scale in the wrong place. Custom properties inherit. A scale defined on
.cardis not available outside it. Use:rootfor global tokens. - Using
vwalone in the preferred value. This ignores the user's font-size setting and zooms poorly. Keep aremterm in each preferred value. - Reading
eminside a root token. Units resolve where the property is used, not where it is defined, when the value is substituted as text. Anemterm in a:roottoken therefore depends on the font size of each element that uses it. Preferremin shared tokens so results stay predictable.
Exercise
You define --space-l: clamp(2rem, 1rem + 5vw, 4rem); inside .sidebar. A <footer> outside the sidebar uses padding: var(--space-l);. What happens, and how do you fix it?
Show answer
The footer cannot see --space-l, because custom properties inherit only to descendants. The padding declaration is invalid at computed-value time, so the footer gets the initial padding of 0. Move the definition to :root, or add a fallback such as var(--space-l, 2rem).