Fluid Typography With clamp(): A Practical Guide
clamp() scales a font size with the viewport without a media query. Learn the formula, the accessibility limits, and the pitfalls.
By Velloreth
The function
font-size: clamp(1rem, 2.5vw + 0.5rem, 2rem);
clamp(min, preferred, max) returns the preferred value, bounded by the minimum and the maximum.
- Below the minimum, the minimum is used.
- Above the maximum, the maximum is used.
- In between, the preferred value is used.
The preferred value is usually a viewport unit plus a base, so the size grows with the screen but not linearly from zero.
Why the base matters
/* Too small on a narrow screen */
font-size: clamp(1rem, 4vw, 3rem);
At a 320px viewport, 4vw is 12.8px, which is below the 1rem minimum, so the minimum applies. That is fine.
But consider a heading:
/* The preferred value is 4vw, which is 12.8px at 320px */
font-size: clamp(1.5rem, 4vw, 3rem);
The minimum saves it, but the transition is abrupt: the size jumps from the minimum to the preferred as soon as the viewport is wide enough.
Adding a base smooths it:
font-size: clamp(1.5rem, 2vw + 1rem, 3rem);
At 320px, 2vw + 1rem is 6.4 + 16 = 22.4px, which is above the 1.5rem minimum, so the size scales smoothly from the start.
The rule: the preferred value should be above the minimum at the smallest viewport you support, so the scale is smooth.
A type scale
:root {
--step--1: clamp(0.875rem, 0.8rem + 0.25vw, 1rem);
--step-0: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
--step-1: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem);
--step-2: clamp(1.5rem, 1.3rem + 1vw, 2rem);
--step-3: clamp(1.875rem, 1.5rem + 1.5vw, 2.5rem);
--step-4: clamp(2.25rem, 1.75rem + 2vw, 3rem);
}
h1 { font-size: var(--step-4); }
h2 { font-size: var(--step-3); }
h3 { font-size: var(--step-2); }
body { font-size: var(--step-0); }
A scale with a consistent ratio looks more deliberate than a set of arbitrary sizes.
The ratio is usually between 1.2 and 1.333, and a larger ratio suits a display heading.
Accessibility
Respect the user’s font size setting. Use rem in the minimum and the maximum, so a user who has increased their browser font size gets a larger text.
/* Good: the minimum and maximum scale with the user's setting */
font-size: clamp(1rem, 2vw + 0.5rem, 1.5rem);
/* Bad: a px minimum ignores the user's setting */
font-size: clamp(16px, 2vw + 8px, 24px);
Do not go below 1rem for body text. A smaller size is hard to read, and the user can zoom, but a zoom is a worse experience than a readable default.
Test at 200% zoom. The WCAG requirement is that the content remains usable at 200%, and a fluid size should not break it.
A viewport unit does not respond to a zoom in the same way as a rem, which is another reason to include a rem in the preferred value.
Where clamp helps
A heading that should be large on a desktop and moderate on a phone.
A section’s padding that should be generous on a wide screen and tight on a narrow one.
.section {
padding-block: clamp(2rem, 5vw, 6rem);
}
A gap in a grid.
.grid {
gap: clamp(1rem, 2vw, 2rem);
}
A container’s width.
.container {
width: min(100% - 2rem, 65ch);
}
min() and max() are the single-bound versions: min(a, b) returns the smaller, and max(a, b) the larger.
Where clamp does not help
A layout change. A sidebar that moves from the side to the top is a media query, not a fluid size.
A component that changes shape. A card that becomes a list is a container query or a media query.
A font that must be a specific size. A logo or an icon should not scale with the viewport.
The rule: use clamp() for a continuous scale, and a media query for a discrete change.
Common mistakes
A pure viewport unit. 4vw alone is too small on a narrow screen and too large on a wide one.
A px minimum. It ignores the user’s font size setting.
A maximum that is too large. A 5rem heading on a 4K screen is overwhelming.
A minimum that is too small. Below 1rem is hard to read.
A clamp on everything. A fluid size on every element produces a layout that never settles.
No test at the extremes. Check the smallest and the largest viewport you support.
Fluid type in a design system
A fluid type scale is most useful when it is defined once as a set of custom properties and used everywhere, because the value of the approach is consistency rather than any individual size. A scale with a fixed ratio between the steps looks deliberate, and a set of arbitrary sizes looks accidental, even when each individual size is reasonable. The practical way to build one is to pick a ratio between 1.2 and 1.333, define the steps as custom properties with a clamp, and use the properties rather than a raw value in a component. The second benefit is that a change to the scale is a change in one place, so a redesign of the typography does not require a search for every font-size in the codebase. The limit of the approach is that it handles a continuous scale and not a discrete change, so a layout that rearranges at a breakpoint still needs a media query, and the two techniques are complementary rather than alternatives.
Practical guidance
- Use
clamp()for a font size and a spacing. - Add a
rembase to the preferred value. - Use
remin the minimum and the maximum. - Keep the minimum readable.
- Keep the maximum reasonable.
- Define a type scale with a consistent ratio.
- Test at the smallest and largest viewport.
- Test at 200% zoom.
- Use a media query for a layout change.
For the related guides, see css-units-explained and responsive-design-breakpoints. For the broader context, css-custom-properties and mobile-first-css.
Quotable summary
clamp(min, preferred, max) returns the preferred value bounded by the minimum and the maximum, and a viewport unit in the preferred value makes the size scale with the screen. Add a rem base to the preferred value, because a pure viewport unit scales linearly and becomes too small on a narrow screen, and the preferred value should be above the minimum at the smallest viewport you support so the scale is smooth. Respect the user’s font size setting by using rem in the minimum and the maximum, and keep the minimum readable at 1rem for body text. A fluid size removes a media query for a simple scale, but it does not replace a real layout change, which is still a media query.