Key points
- clamp(min, preferred, max) scales a value smoothly between two limits, with no stack of media queries.
- The preferred value is calculated from two points (size on a small screen, size on a large one), so there’s no trial and error.
- Always mix rem and vw in the preferred value and test at 200% zoom: text sized in pure vw stops growing when users zoom.
A heading that wraps onto four lines on a phone and looks tiny on a large monitor is one of the layout flaws clients notice first, and the usual fix is a media query, then another, then a third. CSS clamp() solves it cleanly and works in every current browser. The tricky part is the middle value: most of the ones we find in stylesheets we inherit were arrived at by trial and error, and some of them stop text growing when the user zooms in. Here’s how we use it, from the maths through to a WordPress block theme.
clamp() in one minute
clamp(MIN, PREFERRED, MAX) returns the preferred value unless it falls below the minimum or goes above the maximum, in which case it’s held at that limit. It’s shorthand for max(MIN, min(PREFERRED, MAX)).
The magic is in the preferred value: when it depends on the viewport width through the vw unit (1vw = 1% of the width), the font size grows gradually with the screen, between the two limits. You swap a set of steps for a smooth curve:
/* Before: steps, with abrupt jumps between them */
h1 { font-size: 1.75rem; }
@media (min-width: 600px) { h1 { font-size: 2.25rem; } }
@media (min-width: 900px) { h1 { font-size: 2.625rem; } }
@media (min-width: 1200px) { h1 { font-size: 3rem; } }
/* After: one line, a smooth progression */
h1 { font-size: clamp(1.75rem, 1.214rem + 2.381vw, 3rem); }
It’s easier to maintain and looks better at in-between widths (tablets, resized windows, small laptops), which is exactly where step-based sizes tend to produce headings that are just slightly too big and wrap onto a second line.
clamp() isn’t just for fonts: margins, section spacing, container widths (width: clamp(320px, 90%, 72rem)) and border radii all benefit too.
Calculate the preferred value instead of guessing
The preferred value is a straight line: a fixed base plus a share proportional to the viewport width. All you need is two points: the size you want on a small screen and the size you want on a large one. We usually start from 360px (small phones) and 1200px (the width beyond which the content stops getting wider), but use whatever matches your design.
Goal: 16px at a 360px-wide viewport, 20px at 1200px
slope = (20 - 16) / (1200 - 360) = 0.004762
vw value = slope x 100 = 0.476vw
base = 16 - slope x 360 = 14.286px = 0.893rem
font-size: clamp(1rem, 0.893rem + 0.476vw, 1.25rem);
The base is converted to rem (divide by 16, the browser default) so it follows the user’s own font-size setting. The two limits are exactly the target sizes at the two widths: at 360px the formula gives 16px, at 1200px it gives 20px, and in between it scales linearly.
Rather than doing this by hand for every size, a tiny function does the job. We use it to generate a project’s variables, then paste the output into the stylesheet:
// fluid.js : builds the clamp() value to paste into your CSS
function fluid(minPx, maxPx, minVw = 360, maxVw = 1200, rootPx = 16) {
const slope = (maxPx - minPx) / (maxVw - minVw);
const base = minPx - slope * minVw;
const r = (n) => Math.round(n * 1000) / 1000;
return `clamp(${r(minPx / rootPx)}rem, ${r(base / rootPx)}rem + ${r(slope * 100)}vw, ${r(maxPx / rootPx)}rem)`;
}
console.log(fluid(16, 20)); // clamp(1rem, 0.893rem + 0.476vw, 1.25rem)
console.log(fluid(28, 48)); // clamp(1.75rem, 1.214rem + 2.381vw, 3rem)
A complete type scale
Once the method is in place, define the whole scale as custom properties and use them everywhere. Small text barely changes, large headings change a lot. That makes sense: a 48px heading is a problem on a phone, while 16px body text is perfectly readable there.
:root {
/* Fluid type scale : 360px -> 1200px */
--fs-small: clamp(0.875rem, 0.848rem + 0.119vw, 0.938rem);
--fs-base: clamp(1rem, 0.893rem + 0.476vw, 1.25rem);
--fs-h3: clamp(1.25rem, 1.089rem + 0.714vw, 1.625rem);
--fs-h2: clamp(1.5rem, 1.232rem + 1.19vw, 2.125rem);
--fs-h1: clamp(1.75rem, 1.214rem + 2.381vw, 3rem);
}
body { font-size: var(--fs-base); line-height: 1.6; }
h1 { font-size: var(--fs-h1); line-height: 1.15; }
h2 { font-size: var(--fs-h2); line-height: 1.2; }
h3 { font-size: var(--fs-h3); line-height: 1.3; }
On our projects, centralising the scale like this has a valuable side effect: when the client feels “the headings are a bit big on phones”, we change a single limit and the whole site follows, with no hunting for media queries across ten files.
The accessibility trap: zoom
This is what most tutorials leave out. The web accessibility guidelines (WCAG) require that text can be resized up to 200% without losing content. Browser zoom works by shrinking the viewport width measured in CSS pixels, so a size defined only in vw doesn’t grow when the user zooms; it can stay exactly the same.
Three rules avoid the problem:
- Always include a rem base in the preferred value, never
vwon its own: the rem part grows with zoom and with the font size set in the browser. - Set the limits in rem, not px, to respect users who have increased their default font size.
- Keep a sensible gap between minimum and maximum. The further apart they are, the bigger the vw share, and the harder it becomes to reach 200% by zooming. A heading going from 1.75rem to 3rem behaves well; one going from 1rem to 5rem won’t.
The check is simple and we do it every time: on a desktop screen, zoom to 200% and compare text sizes with 100%. They should roughly double. If a heading barely moves, reduce the vw share or bring the limits closer together.
In a WordPress theme: theme.json does the maths
If you work with a block theme, there’s no need to calculate formulas yourself for the sizes offered in the editor. theme.json supports fluid typography: you give a minimum and maximum, and WordPress generates the clamp() declarations in its preset font-size variables.
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 3,
"settings": {
"typography": {
"fluid": true,
"fontSizes": [
{ "slug": "medium", "name": "Medium", "size": "1.25rem",
"fluid": { "min": "1rem", "max": "1.25rem" } },
{ "slug": "x-large", "name": "Extra large", "size": "3rem",
"fluid": { "min": "1.75rem", "max": "3rem" } }
]
}
}
}
Editors then pick “Medium” or “Extra large” in the editor and the size adapts to the screen on its own. It’s a safeguard too: they don’t type a pixel size that looks perfect on their monitor and huge on a phone. On the sites we deliver, we often restrict custom sizes so the editor only offers the ones in the scale.
FAQ
Does clamp() work in every browser?
Yes, in all current browsers. Only very old ones ignore it; for those, declare a fixed size on the line just before the clamp() one and it will act as a fallback.
Why doesn’t my text get bigger when I zoom?
Most likely because its size relies mainly on vw. Add a rem base to the preferred value, express the limits in rem and narrow the gap between minimum and maximum, then test again at 200%.
Does your site look off on some screens, or do headings overflow on mobile? We can rework your theme’s front-end and typography so it looks right at every size.



