Sub topic 9
Fluid typography with clamp()
This chapter assumes you know the CSS box model, the cascade, and the difference between rem, em, % and vw. It builds on the base styles and relative units from earlier chapters.
The problem with fixed and stepped type
A fixed font-size such as 18px is too large on a small phone or too small on a wide monitor. The usual fix is to change the size at each breakpoint. That works, but the text jumps from one size to the next, and you need more media queries.
Fluid typography lets the size change smoothly with the viewport width. You write one declaration and the browser computes the size for every screen.
How clamp() works
clamp(min, preferred, max) takes three values and returns the preferred value, limited to the range from min to max.
body {
font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
}- The minimum is
1rem. The text never gets smaller than this. - The preferred value is
0.9rem + 0.5vw. It grows as the viewport widens. - The maximum is
1.25rem. The text never gets larger than this.
Here 1vw is 1% of the viewport width. On a 400px-wide screen, 0.5vw is 2px. With a 16px root size, the preferred value is 14.4px + 2px = 16.4px, which is just above the minimum. On a 1600px screen, 0.5vw is 8px, so the preferred value is 22.4px. That is above the maximum, so the result is capped at 1.25rem, or 20px.
Why the preferred value mixes rem and vw
Do not use a bare vw value such as clamp(1rem, 2vw, 1.25rem). A size that depends only on vw ignores the user's own font-size setting. If a user raises their browser's default text size, the text will not respond, and zooming the page may not enlarge it either. This is an accessibility problem.
Adding a rem term, as in 0.9rem + 0.5vw, keeps part of the size tied to the user's setting. Use rem for the minimum and maximum too, not px.
Choosing the numbers
You need two points: the size at a small viewport and the size at a large one. A simple approach is to pick the sizes you want, then adjust the vw slope until the curve reaches them near your chosen widths.
For example, you want 1rem at about 320px and 1.25rem at about 1200px. The size must grow 0.25rem (4px) over 880px, so the slope is 4 / 880, about 0.45vw. Setting 0.5vw is a convenient rounding. Then check the result in developer tools at several widths.
Applying it to a type scale
You can use the same function for headings. Set each heading's range separately so the contrast between body text and headings stays comfortable.
:root {
--step-0: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
--step-1: clamp(1.25rem, 1rem + 1.25vw, 2rem);
--step-2: clamp(1.5rem, 1rem + 2.5vw, 3rem);
}
body { font-size: var(--step-0); }
h2 { font-size: var(--step-1); }
h1 { font-size: var(--step-2); }Custom properties keep the numbers in one place. Changing a step updates every element that uses it.
Common errors
- Invalid math. Inside
clamp(),+and-need spaces around them.0.9rem+0.5vwis invalid, and the declaration is dropped. - Unit mismatch in the limits. Mixing
pxlimits with arempreferred value works, but it defeats user scaling. - No fallback. Very old browsers ignore
clamp(). Put a plainfont-size: 1rem;line before it, and they use that. - Too much fluidity. Not everything should scale. Keep line height unitless, such as
line-height: 1.5, so it follows the font size.
Exercise
A stylesheet sets font-size: clamp(16px, 2vw, 20px). Name two problems with this declaration for users, and write a better one that goes from 1rem to 1.25rem.
Show answer
The pixel limits do not follow the user's default font-size setting, and the preferred value is pure vw, so it ignores that setting and may not respond to text zoom. A better version is font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);, which uses rem limits and a preferred value that includes a rem term.