Sub topic 9
Fluid type with clamp()
This chapter assumes you know basic CSS syntax, the common length units (px, rem, em, %, vw), and the earlier chapters on min() and max(). Here you will use clamp() to make font sizes that grow with the viewport inside fixed limits.
The problem with fixed and stepped type
A fixed font-size looks right at one screen width and wrong at others. The usual fix is a media query at each breakpoint:
h1 { font-size: 1.75rem; }
@media (min-width: 40rem) {
h1 { font-size: 2.25rem; }
}
@media (min-width: 70rem) {
h1 { font-size: 3rem; }
}This works, but the size jumps at each breakpoint. Between breakpoints it stays flat, even when the viewport is much wider than the previous one.
Fluid type removes the jumps. The size is computed from the viewport width, so it changes smoothly. You still need limits, because text that scales without bounds becomes unreadably small on a phone and absurdly large on a wide monitor.
How clamp() works
clamp() takes three arguments:
clamp(MIN, PREFERRED, MAX)MINis the smallest value the result can have.PREFERREDis the value the browser uses when it falls between the limits. It is usually an expression that depends on the viewport.MAXis the largest value the result can have.
clamp() is shorthand for a nested max() and min():
clamp(MIN, PREFERRED, MAX)
/* is equivalent to */
max(MIN, min(PREFERRED, MAX))The browser resolves it in two steps. First min(PREFERRED, MAX) caps the preferred value at the maximum. Then max(MIN, ...) raises that result to the minimum if it fell below it. The result is always one single length.
h1 {
font-size: clamp(1.5rem, 1rem + 2vw, 3rem);
}The preferred value 1rem + 2vw depends on the viewport width. Assuming a 16px root font size, the browser computes it like this:
| Viewport width | `1rem + 2vw` | Result of `clamp(1.5rem, 1rem + 2vw, 3rem)` |
|---|---|---|
| 320px | 16px + 6.4px = 22.4px | 24px (the minimum, 1.5rem) |
| 800px | 16px + 16px = 32px | 32px (the preferred value) |
| 1600px | 16px + 32px = 48px | 48px (the preferred value equals the maximum) |
| 2000px | 16px + 40px = 56px | 48px (the maximum, 3rem) |
Why the preferred value needs a rem term
Beginners often write a preferred value made only of vw:
.title-viewport-only {
font-size: clamp(1rem, 4vw, 2rem);
}The size then depends only on the viewport width. It ignores the user's browser font-size setting, and it does not grow when the user zooms the page in some browsers. Add a rem term so part of the value follows the user's preference:
.title {
font-size: clamp(1rem, 0.5rem + 3vw, 2rem);
}The rem term scales with the root font size. Zoom and font-size settings therefore still have an effect. The accessibility details come later in this tutorial. For now, make it a habit to write the preferred value as rem + vw.
Choosing the limits
Pick the limits first, then the preferred value.
- Choose the smallest size you want on a small phone. This is
MIN. - Choose the largest size you want on a large desktop. This is
MAX. - Choose the two viewport widths where those sizes should apply. Compute the preferred value from them (see the next section).
Avoid a large ratio between MAX and MIN. A ratio above about 2.5 makes the text change a lot between small and large screens. It also makes the zoom behavior harder to predict.
Calculating the preferred value between two viewports
You can make the size equal MIN at one viewport width and MAX at another, and change linearly between them. The preferred value is a straight line, intercept + slope × viewport width.
Suppose you want 1rem at a 20rem (320px) viewport and 2rem at a 60rem (960px) viewport.
- Slope = (2 − 1) / (60 − 20) = 0.025. A slope of 0.025 per unit of viewport width equals 2.5vw, because
1vwis 1% of the width. - Intercept = 1 − 0.025 × 20 = 0.5rem.
/* 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);
}Check the result at both ends, with a 16px root font size:
- At 320px:
0.5rem + 2.5vw= 8px + 8px = 16px, which is1rem. - At 960px: 8px + 24px = 32px, which is
2rem.
Outside that range, clamp() holds the size at 16px or 32px.
The conversion from the slope to vw works because the slope is a size per rem of viewport width. Multiply it by 100 to get the vw coefficient. The formula is:
slope = (MAX - MIN) / (maxViewport - minViewport)
vwValue = slope * 100
intercept = MIN - slope * minViewportKeep all four values in rem while you calculate, then write the result as intercept(rem) + vwValue(vw).
Fluid scales with custom properties
A whole page needs several sizes. Define each clamp() once as a custom property and reuse 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); }Now a change to one variable updates every place that uses it. The same technique works for spacing, as --space-m shows. A clamped padding-block follows the same rules as a clamped font size:
section {
padding-block: clamp(1rem, 4vw, 3rem);
}
button {
padding-inline: max(1rem, 2vw);
}The section padding is limited at both ends. The button padding has only a floor, so it never falls below 1rem and grows freely with the viewport.
Note that padding cannot be negative, so clamp() limits here are never negative values. A vw-only preferred value is acceptable for spacing, because spacing does not need to follow the user's text preference as strongly as type does. Many designers still add a rem term to keep the layout proportional when the user zooms.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Declaration is ignored | Arguments are in the wrong order, or a comma is missing | Write `clamp(MIN, PREFERRED, MAX)` with two commas |
| Size never changes | `MIN` equals `MAX`, or the preferred value stays outside the limits | Check each value at the viewport widths you care about |
| Text stays at `MAX` on phones | The preferred value is larger than `MAX` at small widths | Recompute the line from two chosen points |
| Text will not grow when the user zooms | The preferred value uses only `vw` | Add a `rem` term |
| `calc()` error inside `clamp()` | Operators written without spaces, such as `1rem+2vw` | Put spaces around `+` and `-` |
Two behaviors are worth remembering. If MIN is larger than MAX, clamp() returns MIN, because the max() step is applied last. And you do not need calc() inside the arguments. clamp(1rem, 0.5rem + 2vw, 2rem) is valid as written.
Summary
clamp(MIN, PREFERRED, MAX)resolves to one length: the preferred value, held between the limits.- Use a
rem + vwpreferred value so text respects the user's font settings. - Compute the preferred value from two points with a slope and an intercept.
- Store each
clamp()in a custom property to build a reusable scale.
Exercise
You want a heading to be 1.25rem at a 20rem viewport and 2.25rem at a 70rem viewport. Write the clamp() declaration, and show the slope and intercept.
Show answer
Slope = (2.25 - 1.25) / (70 - 20) = 1 / 50 = 0.02, so the vw coefficient is 2vw. Intercept = 1.25 - 0.02 * 20 = 0.85rem. Declaration: font-size: clamp(1.25rem, 0.85rem + 2vw, 2.25rem);
Exercise
Why is font-size: clamp(1rem, 4vw, 2rem) a poor choice for body headings, and how do you improve it?
Show answer
The preferred value uses only vw, so it depends on the viewport and ignores the user's font-size setting, and zooming may not enlarge the text in some browsers. Add a rem term to the preferred value, for example clamp(1rem, 0.5rem + 3vw, 2rem).
Exercise
A page has clamp(1.5rem, 1rem + 2vw, 3rem) applied to h1 and a 16px root font size. What font size does the heading have at a 2000px viewport, and why?
Show answer
3rem (48px). The preferred value is 16px + 40px = 56px, which exceeds the maximum, so min(PREFERRED, MAX) returns 48px and max(MIN, 48px) keeps it.