Fluid Typography With CSS clamp(): The Formula, the Pitfalls and Accessibility
For years, responsive typography meant a stack of media queries: 32px headings on desktop, 28px on tablets, 24px on phones. The text jumped between sizes at each breakpoint. CSS clamp() replaces that with a single line that scales smoothly with the screen, while staying within limits you control.
What clamp() does
font-size: clamp(1.125rem, 0.83rem + 1.26vw, 2.5rem);clamp(minimum, preferred, maximum) returns the preferred value, but never less than the minimum or more than the maximum. The preferred value usually mixes a fixed unit (rem) with a viewport unit (vw), so it grows as the screen gets wider. On a small phone the size sits at the minimum, on large screens at the maximum, and in between it scales linearly.
clamp() has been supported in all major browsers since 2020.
The formula
You want text to be minSize at minViewport and maxSize at maxViewport, scaling in a straight line in between. That’s linear interpolation:
- Slope = (maxSize − minSize) ÷ (maxViewport − minViewport)
- Intercept = minSize − slope × minViewport
- Preferred value = intercept + slope × 100vw
Example: 18px at 375px wide, 40px at 1440px wide.
- Slope = (40 − 18) ÷ (1440 − 375) = 22 ÷ 1065 ≈ 0.02066
- Intercept = 18 − 0.02066 × 375 ≈ 10.25px
- Preferred = 10.25px + 2.066vw
Converted to rem (dividing pixels by 16): clamp(1.125rem, 0.6406rem + 2.0657vw, 2.5rem). The CSS clamp generator above does this calculation and also shows the computed size at any preview width.
Media queries versus clamp()
| Aspect | Breakpoint media queries | clamp() |
|---|---|---|
| Size changes | In jumps at breakpoints | Smoothly at every width |
| Code | Several rules per element | One declaration |
| Control at specific widths | Exact | Linear between two points |
| Readability | Clear | Needs a generator or comments |
| Works with container queries | Yes | Yes, using cqi units instead of vw |
Many teams combine both: clamp() for smooth scaling, and an occasional media query for layout changes that aren’t about size.
Why mix rem and vw?
A font size of pure vw, like font-size: 4vw, has two serious problems:
- No limits. On a 320px phone it’s 12.8px; on a 2560px monitor it’s 102px.
- It ignores zoom. Browser zoom changes the viewport width in CSS pixels, so vw-based text scales differently from everything else. Text that is purely vw-based doesn’t get bigger when the user zooms, which can fail WCAG 1.4.4 (Resize Text), which requires text to be resizable to 200%.
Including a rem component in the preferred value, and using rem for the min and max, keeps the text tied to the user’s font size setting.
The zoom pitfall, in detail
Even with rem in the mix, aggressive fluid type can still resist zooming. When a user zooms to 200%, the viewport in CSS pixels halves, so the vw part shrinks while the rem part doubles. If most of your preferred value comes from vw, the net change can be far less than 2×.
A widely cited rule of thumb from accessibility researchers: keep the maximum font size at no more than about 2.5 times the minimum. Within that range, fluid type generally still reaches 200% enlargement when zoomed. Test it: zoom your browser to 200% at a desktop width and check that headings and body text have roughly doubled.
Choosing the viewport range
- Min viewport: 320–375px covers nearly all phones.
- Max viewport: 1200–1440px is a common choice; beyond that, many layouts stop widening because of a max-width container, so text growth should stop too.
Use the same min and max viewport for every fluid value in a project, so headings, body text and spacing all scale in sync.
Building a fluid type scale
Fluid sizing pairs beautifully with a modular type scale. Define two scales, a smaller ratio for mobile (say 1.2) and a larger one for desktop (say 1.333), then make each step fluid between its mobile and desktop size:
| Step | Mobile (375px) | Desktop (1440px) |
|---|---|---|
| Body | 16px | 18px |
| H4 | 19px | 24px |
| H3 | 23px | 32px |
| H2 | 28px | 42px |
| H1 | 33px | 56px |
Small text barely changes; large headings change a lot, which is exactly how good responsive typography should behave. Generate the base sizes with the type scale generator, then create a clamp() value for each.
Fluid spacing
clamp() isn’t just for fonts. Padding, margins and gaps can scale too:
section { padding-block: clamp(3rem, 1.5rem + 5vw, 7rem); }Generous whitespace on desktop and compact spacing on phones, without breakpoints. Keep the values aligned with your spacing scale at the endpoints.
Container query units
When a component can appear in a narrow sidebar or a wide main column, sizing it by the viewport doesn’t make sense. Container query units (cqi, cqw) work like vw but are relative to the nearest container with container-type: inline-size. Replace vw with cqi in the clamp formula and the component’s text scales with its own width.
How fluid type goes wrong
- Pure vw font sizes. Always use clamp() with rem limits.
- Too big a range. 14px to 96px will look broken somewhere and fail zoom tests.
- Different viewport ranges for different elements, causing headings and body text to scale out of proportion.
- Forgetting line height. Large fluid headings need tighter line heights; use unitless values so they scale with the font. Our line height guide covers this.