Sub topic 9
Fluid type with clamp()
This chapter assumes you know the common length units (px, rem, %, vw) and the basics of the cascade. It also assumes you have seen min() and max() in the previous chapters.
The problem with fixed sizes
Breakpoint-based type jumps from one size to the next when the viewport crosses a threshold. Between breakpoints, the text stays the same size even as the space around it changes. clamp() replaces those jumps with a smooth change and a guaranteed range.
How clamp() resolves
clamp(MIN, PREFERRED, MAX) takes three values and returns one. It is equivalent to max(MIN, min(PREFERRED, MAX)). The browser works through three cases:
- If the preferred value is below the minimum, the result is the minimum.
- If the preferred value is above the maximum, the result is the maximum.
- Otherwise, the result is the preferred value.
h1 {
/* clamp(MIN, PREFERRED, MAX) is equivalent to max(MIN, min(PREFERRED, MAX)) */
font-size: clamp(1.5rem, 1rem + 2vw, 3rem);
}Assume a 16px root font size. The preferred value is 16px + 2vw. That equals the 24px minimum when the viewport is 400px wide, and the 48px maximum at 1600px. Between those widths the heading grows linearly. Outside them it stays fixed.
| Viewport width | Preferred value | Result |
|---|---|---|
| 320px | 22.4px | 24px (minimum) |
| 800px | 32px | 32px |
| 1920px | 54.4px | 48px (maximum) |
Choosing the preferred value
The preferred value is usually a rem term plus a vw term. The vw term sets how fast the size grows. The rem term sets the starting offset. To hit exact sizes at two viewport widths, solve for the line through those two points:
/* Scale from 1rem at a 20rem (320px) viewport to 2rem at a 60rem (960px) viewport.
slope = (2 - 1) / (60 - 20) = 0.025 -> 2.5vw
intercept = 1 - 0.025 * 20 = 0.5rem */
h2 {
font-size: clamp(1rem, 0.5rem + 2.5vw, 2rem);
}The slope is the size change divided by the width change. Multiply it by 100 to get the vw coefficient. The intercept is the minimum size minus the slope times the minimum width.
Reusing sizes with custom properties
Define each size once and reuse it. This keeps the type scale in one place.
: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); }Keep the rem term
A preferred value made only of vw ignores the user's font-size preference. Browser zoom changes the viewport width in CSS pixels, but a vw-only size may not grow enough when the user zooms in. Adding a rem term keeps part of the size tied to the user's settings.
/* Harder to zoom: the size depends only on the viewport */
.title-viewport-only {
font-size: clamp(1rem, 4vw, 2rem);
}
/* Better: the rem term responds to zoom and user font-size settings */
.title {
font-size: clamp(1rem, 0.5rem + 3vw, 2rem);
}Common mistakes
- Making the range too wide. If the maximum is more than about 2.5 times the minimum, zooming in can fail to enlarge the text enough.
- Writing
clamp(3rem, 2vw, 1.5rem)with the minimum above the maximum. The minimum wins, so the size is always 3rem. - Writing
2vw + 1remwithout spaces around+. Inside math functions,+and-need whitespace on both sides. - Forgetting units.
clamp(1rem, 0.5rem + 2.5, 2rem)is invalid because2.5is a bare number added to a length.
Exercise
You want a heading that is 1.25rem at a 320px viewport and 2.25rem at a 1120px viewport, assuming a 16px root font size. Write the clamp() declaration.
Show answer
The slope is (2.25 - 1.25) / (70 - 20) = 0.02, which is 2vw. The intercept is 1.25 - 0.02 * 20 = 0.85rem. The declaration is font-size: clamp(1.25rem, 0.85rem + 2vw, 2.25rem);