Fluid typography with CSS clamp()
Build fluid font sizes and spacing with CSS clamp(): the formula, two worked examples, accessibility cautions and common mistakes.
By the tools4free.dev team · Last reviewed 4 December 2026
CSS clamp() lets a value grow smoothly with the viewport, but never go below a minimum or above a maximum. Written as clamp(MIN, PREFERRED, MAX), it replaces a stack of media queries with one line. This guide explains how the three arguments work, gives a formula for building the preferred value, and walks through two examples you can copy.
How clamp() works
The function takes three values and returns the middle one, limited by the outer two:
font-size: clamp(1rem, 0.5rem + 2.5vw, 2.5rem);
- MIN (1rem) is the smallest the value can be.
- PREFERRED (0.5rem + 2.5vw) is the value the browser tries to use. It usually contains a viewport unit so that it changes as the window changes.
- MAX (2.5rem) is the largest the value can be.
It is equivalent to max(MIN, min(PREFERRED, MAX)). If the preferred value falls below the minimum, you get the minimum. If it rises above the maximum, you get the maximum. In between, the size scales with the screen.
Why a viewport unit alone is not enough
You could write font-size: 4vw, but that causes two problems. On a very narrow screen the text becomes tiny, and on a wide monitor it becomes huge. Worse, text sized only in vw ignores the reader's browser font-size setting, which fails WCAG 1.4.4 Resize Text for people who rely on zoom. Mixing a rem part with a vw part keeps some connection to the user's base size, and the clamp limits stop the extremes.
The formula for the preferred value
To go from a minimum size at a small viewport to a maximum size at a large viewport, you need a straight line through two points. With sizes in pixels:
slope = (maxSize - minSize) / (maxViewport - minViewport)
intercept = minSize - slope * minViewport
preferred = intercept + (slope * 100) vw
Convert the intercept to rem by dividing by 16 (assuming the default root size of 16 pixels). The slope times 100 is the number of vw units, because 1vw is 1% of the viewport width.
Worked example 1: a heading from 16px to 40px
You want a heading that is 1rem (16px) at a 320px viewport and 2.5rem (40px) at a 1280px viewport.
- slope = (40 - 16) / (1280 - 320) = 24 / 960 = 0.025
- intercept = 16 - 0.025 x 320 = 16 - 8 = 8px = 0.5rem
- vw part = 0.025 x 100 = 2.5vw
Result: clamp(1rem, 0.5rem + 2.5vw, 2.5rem). Check the middle: at an 800px viewport the preferred value is 8 + 0.025 x 800 = 28px, which is exactly halfway between 16 and 40 at the halfway viewport. Below 320px the minimum holds at 16px, and above 1280px the maximum holds at 40px.
Worked example 2: body text from 18px to 24px
Body text moves less. Say 1.125rem (18px) at 375px and 1.5rem (24px) at 1440px.
- slope = (24 - 18) / (1440 - 375) = 6 / 1065 = 0.005634
- intercept = 18 - 0.005634 x 375 = 15.887px = 0.993rem
- vw part = 0.5634vw, rounded to 0.563vw
Result: clamp(1.125rem, 0.993rem + 0.563vw, 1.5rem). At 1440px the preferred value is 15.887 + 8.113 = 24px, matching the maximum. Rounding the numbers a little is harmless, because the limits catch any small error at either end.
Where clamp() works besides font-size
Any CSS property that accepts a length, percentage, number or similar value can take clamp(). Useful cases include:
| Use | Example | Effect |
|---|---|---|
| Fluid padding | padding-inline: clamp(1rem, 4vw, 3rem); | Side gutters grow from 16px to 48px. |
| Readable line length | width: clamp(20rem, 90%, 65ch); | A column that never gets too narrow or too wide for reading. |
| Gap between items | gap: clamp(0.5rem, 2vw, 1.5rem); | Grid spacing that relaxes on large screens. |
| Hero height | min-height: clamp(18rem, 50vh, 40rem); | A banner that scales with the window but stays sensible. |
clamp() compared with media queries
A media-query approach steps through sizes at fixed breakpoints: for example 16px up to 600px, 18px up to 900px, and 20px above. That is easy to reason about, but the text jumps at each breakpoint and you must maintain the numbers in several places. Fluid clamp() gives one rule that interpolates continuously. Media queries are still the right tool when a layout needs to change shape, such as going from one column to three, rather than just scale.
Accessibility: do not break zoom
Using rem in the minimum and maximum matters. If the limits are written in px, a reader who raised their browser's default font size gets no benefit. With rem limits, the whole range scales with their preference. Note that browser zoom changes the viewport width in CSS pixels, so zooming in a desktop browser shrinks the viewport and the vw part with it. A fluid size can therefore get smaller relative to the screen at the same time as a user zooms in, which is why a sensible minimum matters. Test your page at 200% zoom.
Common mistakes
- Using only
vwin the middle argument. Add aremoffset so that the value responds to user font settings. - Writing limits in
pxand defeating user preferences. - Putting the arguments in the wrong order. MIN comes first and MAX last; if MIN is larger than MAX, the minimum wins.
- Forgetting spaces around
+and-inside the function.0.5rem+2.5vwis invalid;0.5rem + 2.5vwis correct. - Making the type scale too steep. A heading that triples in size across the range can look cramped on small screens and oversized on large ones.
- Assuming the root font size is 16px. If a page sets
html { font-size: 62.5% }, your pixel-to-rem conversions must use 10px.
Testing a fluid scale
Check three widths at minimum: just below your minimum viewport, a mid value, and just above the maximum viewport. At each one, read the computed font size in your browser's developer tools. Confirm that the minimum and maximum behave as expected and that no line of text becomes uncomfortably long. For body text, aim for roughly 45 to 75 characters per line, which is what the ch unit in the table above helps with.
Frequently asked questions
Is clamp() supported in all browsers?
It is supported in all current major browsers. MDN keeps an up-to-date compatibility table, linked below, so check it if you need to support older versions.
Can I use clamp() inside calc()?
Yes. Functions can be nested, so calc(clamp(1rem, 2vw, 2rem) * 1.2) is valid. You can also use min() and max() on their own when you need just one limit.
What about container-based sizing?
The cqi container query units let you scale against a container instead of the viewport, and they work inside clamp() in the same way. The formula is identical; replace the viewport widths with container widths.
Should every size be fluid?
No. Fluid sizing suits headings, large spacing and hero areas. Body text often only needs a small range, and small UI text, such as labels, is better kept fixed so it stays legible.