Modular Scale Tokens and Typographic Rhythm
On the Type Scale and Fluid Type reading you built a modular scale by choosing a ratio, multiplying it out by hand, and writing each resulting size into a token. That works, but it bakes the arithmetic into fixed numbers: changing the ratio means recomputing and rewriting every step. This page shows how to encode the scale itself in CSS so that one ratio token drives every step through calc(). Change the ratio, and the whole scale regenerates. Along the way it covers computing clean values with pow() and round(), making the computed scale fluid, and the connection between a typed ratio and animation.
Computing the Scale with pow()
A modular scale is a base size multiplied by a ratio raised to a step number. Step one is the base times the ratio once, step two is the base times the ratio squared, and a step below the base is the base times the ratio raised to a negative power. That is exactly what the pow() math function expresses inside calc(): pow(base, exponent) raises the first argument to the power of the second.
Encode the scale with two inputs, a base and a ratio, then compute each step from them. Nothing else needs to change when you want a different scale.
:root {
--ratio: 1.25; /* major third */
--base: 1.8rem; /* step 0, body size */
--font-size-md: var(--base);
--font-size-lg: calc(var(--base) * pow(var(--ratio), 1));
--font-size-xl: calc(var(--base) * pow(var(--ratio), 2));
--font-size-2xl: calc(var(--base) * pow(var(--ratio), 3));
--font-size-3xl: calc(var(--base) * pow(var(--ratio), 4));
}
With the scale written this way, changing --ratio from 1.25 to 1.333 shifts every computed step at once, with no other edits. The ratio is now a single knob that controls the entire system. The pow() function reached Baseline across Chrome, Edge, Firefox, and Safari and is safe to use in current browsers; confirm at Can I Use: pow(). If you must support older browsers, the fallback is plain repeated multiplication: calc(var(--base) * var(--ratio) * var(--ratio)) for step two, and so on.
Steps Below the Base with Negative Exponents
Hand-typed scales tend to hide the steps below the base because computing them means dividing rather than multiplying. With pow() they fall out naturally: a negative exponent produces a value smaller than the base. This gives you small text, captions, and fine print that sit on the same ratio as the rest of the scale rather than being guessed.
:root {
--font-size-sm: calc(var(--base) * pow(var(--ratio), -1));
--font-size-xs: calc(var(--base) * pow(var(--ratio), -2));
}
Snapping to Clean Values with round()
Computing steps from a ratio produces precise but awkward numbers. A base of 18px times 1.25 cubed is 35.15625px. That sub-pixel precision is usually fine, since browsers render fractional pixels, but there are times you want the computed value snapped to a whole pixel or to a clean increment for crisp rendering or for alignment with a spacing grid. The round() math function does exactly that inside calc().
The function takes an optional rounding strategy, the value to round, and the interval to round to. Rounding a computed step to the nearest pixel keeps the scale ratio-driven while removing the fractional tail.
:root {
/* Round each computed step to the nearest whole pixel */
--font-size-xl: round(calc(var(--base) * pow(var(--ratio), 2)), 1px);
}
Use rounding deliberately rather than everywhere. Rounding every step can compound small errors and pull the scale slightly off its true ratio, so it is best reserved for the specific steps where pixel-crisp values matter. The round() function has the same modern support profile as pow(); see Can I Use: round().
Turning the Ratio Knob
The demo below computes a five-step scale from a single ratio using the technique above. Drag the slider to change the ratio and watch every step resize at once. The named stops mark the common musical ratios: a smaller ratio keeps the steps close together for dense, utilitarian interfaces, while a larger ratio opens dramatic contrast suited to editorial layouts.
Making the Whole Scale Fluid
The type-scale reading made a single value fluid with clamp(). The natural extension is to make every computed step fluid, so the entire scale breathes between a narrow and a wide viewport rather than just one heading. Because the steps are already expressed through tokens, you apply the fluid treatment to the base and let the ratio carry it through the scale, or you clamp each computed step individually for precise control over its range.
The simplest approach makes the base fluid and lets every pow()-computed step inherit that fluidity, since each step is a multiple of the base:
:root {
/* A fluid base means every computed step is fluid too */
--base: clamp(1.6rem, 1.4rem + 1vw, 1.8rem);
--ratio: 1.25;
--font-size-xl: calc(var(--base) * pow(var(--ratio), 2));
}
This is powerful because a single fluid declaration on the base propagates through the whole scale. The tradeoff is that every step scales by the same proportion, which may grow your largest display sizes faster than you want. When you need each step to have its own minimum, maximum, and rate, clamp the steps individually instead, accepting more verbosity for finer control. For generating those per-step clamp values, the Utopia calculator is the standard production tool, and it uses exactly the ratio-driven approach this page describes.
Animating the Ratio with a Typed Property
The ratio knob in the demo above changes instantly as you drag. If you wanted the scale to animate smoothly from one ratio to another, a plain --ratio custom property could not do it, because the browser cannot interpolate an untyped value. Registering the ratio as a typed number with @property, the technique covered on the @property choose topic, makes it animatable, so the entire scale can transition between two ratios as one smooth motion.
This is a niche effect rather than an everyday need, but it is a satisfying demonstration of how the pieces connect: a typed ratio token, a scale computed from it with pow(), and a transition that flows through every step at once. It is worth building once to see the whole system move together.
Connecting the Scale to Measure and Rhythm
A scale sets the sizes, but readable typography also depends on line length and vertical spacing. The Text Properties and Font-Relative Units choose topic covers these in depth: the ch unit for controlling measure, and the lh and rlh units for vertical rhythm. The connection worth noting here is that a token-driven scale feeds those tools directly. Your body size token sets the font size, a max-width in ch keeps the measure comfortable at that size, and margins expressed in lh tie the spacing between blocks to the same rhythm the scale establishes. The scale, the measure, and the rhythm are three parts of one system rather than independent decisions.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
-
How
pow()insidecalc()computes a scale step from a base and a ratio, and why that turns the ratio into a single knob for the whole scale -
How negative exponents produce the steps below the base, and when
round()is worth applying to a computed step - The live effect of changing one ratio token, demonstrated by showing several steps resize together
- How a fluid base propagates through every computed step, and the tradeoff against clamping steps individually
- A short before and after on your portfolio: take a page section with arbitrary, hand-picked font sizes and rebuild it on your computed scale tokens, showing how the hierarchy tightens once every size sits on one ratio
Start by putting your computed steps in a simple table on the page, like a spreadsheet of the scale, so teammates can see the ratio relationship between adjacent sizes before you apply it to real content.
Resources
-
MDN: pow() documents the exponential function and its use inside
calc() - MDN: round() covers the rounding strategies and interval argument
- MDN: calc() is the reference for the calculation context these functions run in
- Utopia type calculator generates fluid, ratio-driven scales and outputs ready-to-paste clamp values
- Modular Scale is a classic interactive explorer for comparing ratios and their resulting steps
- Can I Use: pow() and round() for current support data