Sub topic 9
Fluid typography with clamp()
This chapter assumes you know CSS selectors, the box model, and the cascade. You should also be comfortable with rem and em units from the earlier chapter on relative units. Here you will build type that scales smoothly with the viewport, without a media query at every breakpoint.
Why fixed steps are not enough
A common approach is to set one font size for small screens and change it at a breakpoint:
body {
font-size: 1rem;
}
@media (min-width: 48em) {
body {
font-size: 1.25rem;
}
}This works, but the text jumps from 16px to 20px at one exact width. A tablet at 47.9em and a tablet at 48em look different for no good reason. If you add headings, spacing, and several breakpoints, the number of rules grows quickly.
Fluid typography replaces the jump with a continuous scale. The font size grows with the viewport width, between a minimum and a maximum that you choose.
How clamp() works
clamp() takes three values: a minimum, a preferred value, and a maximum.
body {
font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
}The browser computes the preferred value, then limits it to the range. In effect it evaluates max(minimum, min(preferred, maximum)).
- If the preferred value is below
1rem, the result is1rem. - If the preferred value is above
1.25rem, the result is1.25rem. - Otherwise the result is the preferred value.
The preferred value here is 0.9rem + 0.5vw. One vw is 1% of the viewport width. On a 400px-wide screen, 0.5vw is 2px. With a 16px root size, 0.9rem is 14.4px, so the preferred value is 16.4px. That is just above the minimum, so the font size is 16.4px.
On a 1200px-wide screen, 0.5vw is 6px, so the preferred value is 20.4px. That is above 1.25rem (20px), so the maximum wins and the font size is 20px.
The tags min() and max() also exist. clamp(a, b, c) is a shorter way to write the combination of both.
Why the preferred value mixes rem and vw
You might try a pure viewport unit such as font-size: 2vw. This has a serious accessibility problem. The size depends only on the viewport, so a user who increases their browser's default font size gets no change at all. Browser zoom may also fail to enlarge the text enough in some cases, because zooming changes the viewport width in CSS pixels, but a pure vw value gives you little control over how it responds.
Adding a rem part fixes this. In 0.9rem + 0.5vw, the rem part grows when the user raises their default font size. The text then scales with both the viewport and the user's preference.
The bounds should also be in rem. If you wrote clamp(16px, ..., 20px), a user's preferred default size would be ignored at the limits.
Calculating the preferred value
Guessing the middle part gets old fast. You can compute it from four numbers: the minimum size, the maximum size, and the two viewport widths at which those sizes should be reached.
Suppose you want 16px at a 400px viewport and 20px at a 1200px viewport.
- Find the slope:
(20 - 16) / (1200 - 400) = 0.005. As a percentage of the viewport, that is0.5vw. - Find the intercept:
16 - 0.005 * 400 = 14px. Inrem, with a 16px root, this is0.875rem. - Combine them:
clamp(1rem, 0.875rem + 0.5vw, 1.25rem).
At 400px, 0.875rem + 0.5vw is 14px + 2px = 16px. At 1200px, it is 14px + 6px = 20px. Between those widths, the size changes in a straight line.
The example in the earlier code used 0.9rem rather than 0.875rem. The result differs by less than half a pixel, so either works. The method matters more than the exact numbers.
Applying it to a type scale
Headings usually change more than body text. You can give each level its own clamp() and keep the values in custom properties so they are easy to adjust.
:root {
--step-0: clamp(1rem, 0.875rem + 0.5vw, 1.25rem);
--step-1: clamp(1.25rem, 1rem + 1vw, 1.75rem);
--step-2: clamp(1.5rem, 1rem + 2vw, 2.5rem);
}
body {
font-size: var(--step-0);
line-height: 1.5;
}
h2 {
font-size: var(--step-1);
line-height: 1.25;
}
h1 {
font-size: var(--step-2);
line-height: 1.15;
}Notice that the styles are mobile-first. The minimum values describe the small-screen design, and no media query is needed to enlarge anything. The scale grows on its own as space becomes available.
Use unitless line-height. A unitless value is multiplied by each element's own font size, so line spacing follows the fluid size automatically.
Using fluid sizes for spacing
The same idea works for padding, margins, and gaps. A clamp() value can go anywhere CSS accepts a length.
.section {
padding-block: clamp(1.5rem, 1rem + 2vw, 4rem);
padding-inline: clamp(1rem, 0.5rem + 2vw, 3rem);
}Keeping type and spacing on the same scale makes a page feel proportionate at every width.
Gotchas and edge cases
Zoom and the 200% rule
Accessibility guidelines (WCAG 1.4.4) expect text to be resizable up to 200% without loss of content. Because the rem part of your formula scales with the user's setting, fluid type built as above generally passes. Test it. Zoom to 200% in the browser and check that the text really doubles in size. If the bounds and slope are too tight, the text may grow less than you expect.
Container width versus viewport width
vw measures the viewport, not the element's container. In a narrow sidebar on a wide screen, text sized with vw can look too large for its box. If a component needs type that follows its own container, use container query units such as cqi, which you can combine with container-type: inline-size from the container queries chapter.
.card-list {
container-type: inline-size;
}
.card h3 {
font-size: clamp(1rem, 0.75rem + 2cqi, 1.5rem);
}Line length
Font size grows with the viewport, but line length is controlled separately. Very wide screens can produce lines of 120 characters or more, which are hard to read. Limit the measure with max-inline-size: 65ch on text containers.
Mixed units inside clamp()
Inside clamp(), the middle value may use arithmetic such as 0.875rem + 0.5vw. Spaces around + and - are required. Without them, the declaration is invalid and the browser drops it. Multiplication and division do not need spaces.
Browser support
clamp() is supported by all current major browsers. If you must support very old browsers, put a plain fallback declaration before the clamp() line. A browser that does not understand clamp() ignores that line and keeps the earlier one.
h1 {
font-size: 2rem;
font-size: clamp(1.5rem, 1rem + 2vw, 2.5rem);
}When not to use fluid type
Fluid scaling suits headings and display text, where a larger size on larger screens helps. For body text, a small range works best, as in the example above. A body size that varies from 14px to 28px would make the layout hard to predict. Controls such as buttons and form fields often look better at a steady size, because their dimensions are tied to touch targets, which the next chapter covers.
Summary
clamp(min, preferred, max)limits a value to a range.- Mix
remwithvwin the preferred value so the text still respects user settings. - Give the bounds in
rem. - Calculate the slope and intercept from two chosen sizes and two viewport widths.
- Use custom properties for a reusable scale, and
cqiwhen a component should follow its container. - Check zoom behavior and line length.
Exercise
You want body text to be 1rem at a 320px viewport and 1.125rem at an 1120px viewport, assuming a 16px root size. Write the clamp() declaration, showing the slope and intercept you used.
Show answer
The sizes are 16px and 18px. Slope: (18 - 16) / (1120 - 320) = 0.0025, which is 0.25vw. Intercept: 16 - 0.0025 * 320 = 15.2px, which is 0.95rem. The declaration is font-size: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
Exercise
A teammate sets a heading to font-size: 4vw so it scales with the screen. A user who raises their browser's default font size reports that the heading does not change. Explain why, and show a fix that keeps the fluid behavior.
Show answer
A value in vw depends only on the viewport width, so the user's default font size has no effect on it. Add a rem component so the size also responds to that setting, and bound it with rem values. For example: font-size: clamp(1.5rem, 1rem + 2vw, 3rem);