Use clamp() with a rem minimum, a viewport-based preferred value and a rem maximum to make text scale smoothly between screen sizes without media queries.
The short answer
#clamp() takes three values: a minimum, a preferred value and a maximum. Give it a rem minimum and maximum, and a preferred value that mixes rem with a viewport unit such as vw. The text then grows smoothly with the screen width but never goes below or above your limits.
_10h1 {_10 font-size: clamp(2rem, 1.5rem + 2.5vw, 3.5rem);_10}
Why it happens
#Without clamp(), responsive type usually means a list of media queries that jump the font size at each breakpoint. That works, but the size changes in steps, and text that suits a 768px screen can look cramped at 760px.
Viewport units fix the steps: font-size: 5vw scales continuously. On their own, though, they have two problems. On a phone the text gets too small, and on a wide monitor it gets huge. Worse for accessibility, a size in pure vw does not respond to the user's browser font size setting, and it barely grows with page zoom, because zooming in makes the viewport narrower in CSS pixels, so the vw value shrinks while the zoom enlarges it.
clamp(min, preferred, max) solves both. The browser works out the preferred value and then limits it to the range. It is the same as max(min, min(preferred, max)). Adding a rem part to the preferred value, as in 1.5rem + 2.5vw, keeps the user's font size setting in the result and makes zoom work properly.
How to fix it
#Work out the preferred value from two sizes
Pick the size you want at a small viewport and at a large one. For example, 2rem (32px) at 400px wide and 3.5rem (56px) at 1360px wide.
- Slope: the size change divided by the width change. (56 - 32) / (1360 - 400) = 0.025, so 2.5vw.
- Intercept: the small size minus slope times small width. 32 - 0.025 x 400 = 22px, or 1.375rem.
_10h1 {_10 /* 32px at 400px wide, 56px at 1360px wide */_10 font-size: clamp(2rem, 1.375rem + 2.5vw, 3.5rem);_10}
You do not need to be exact. Many teams use an online fluid type calculator, or round the numbers until it looks right.
Build a fluid type scale with custom properties
Define each step once and use it everywhere.
_10:root {_10 --step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);_10 --step-1: clamp(1.25rem, 1.1rem + 0.6vw, 1.5rem);_10 --step-2: clamp(1.5rem, 1.25rem + 1.2vw, 2.25rem);_10 --step-3: clamp(2rem, 1.375rem + 2.5vw, 3.5rem);_10}_10_10body { font-size: var(--step-0); }_10h2 { font-size: var(--step-2); }_10h1 { font-size: var(--step-3); }
Scale with the container instead of the viewport
If a component can appear in a narrow sidebar or a wide main column, size its text from the container. Container query units such as cqi (1% of the container's inline size) work inside clamp() once the parent is a container.
_10.card-wrapper {_10 container-type: inline-size;_10}_10_10.card h3 {_10 font-size: clamp(1.125rem, 0.9rem + 3cqi, 1.75rem);_10}
Use it for spacing too
clamp() works on any length, so padding, gaps and margins can scale in the same way as the text.
_10.section {_10 padding-block: clamp(2rem, 1rem + 5vw, 6rem);_10}
Common pitfalls
#- Using only
vwin the preferred value. Text in pure viewport units ignores the user's font size preference. Always add aremterm. - Putting the maximum smaller than the minimum. If the minimum is larger, it wins and the text never changes size.
- Writing
1.5rem+2.5vwwith no spaces. Insidecalc()andclamp(), the plus and minus signs need a space on each side or the declaration is invalid. - Making body text too fluid. Large headings benefit from a wide range; body text should only change a little, if at all.
- Choosing a very wide range. WCAG asks for text to resize to 200%. A useful rule of thumb is to keep the maximum no more than about 2.5 times the minimum, then check with browser zoom that the text still gets bigger at large widths.
Related reading
#Was this resource helpful?