CSS Clamp Generator
Work out a clamp() value that scales smoothly between two sizes across two viewport widths, with the resolved pixel size at each end shown so you can check it.
Loading tool…
The CSS is generated by JavaScript in your browser and never leaves the page. There is nothing to upload and nothing is stored.
What the CSS Clamp Generator does
The CSS clamp generator on this page will work out a clamp() value that scales smoothly between two sizes across two viewport widths, with the resolved pixel size at each end shown so you can check it. Everything runs inside this page, so the work happens on your own device rather than on a server. It is most often used for fluid heading sizes that scale between a phone and a desktop without media queries. We run a second free tools site as well — FreeToolsWorld.org.
How to use it
- Enter the size you want at the small viewport and at the large one.
- Enter those two viewport widths.
- Pick rem unless you have a specific reason not to.
- Check the resolved pixel sizes in the note, then copy the declaration.
How it works
clamp() takes three values — a minimum, a preferred value and a maximum — and the preferred one is where fluid typography happens. It is a straight line through two points: the size you want at a narrow viewport, and the size you want at a wide one. Expressed as CSS that line becomes an intercept in rem plus a slope in vw, and the clamp holds it flat outside the range. For 16px at 320px up to 24px at 1280px the answer is clamp(1rem, 0.8333rem + 0.8333vw, 1.5rem), which resolves to exactly 16px and 24px at those two widths and 20px halfway between.
The arithmetic is simple and the unit handling is where it goes wrong. The slope is a size per unit of viewport, so if the sizes are in rem the viewport widths have to be divided by the root font size too. Mixing rem sizes with px viewports scales the slope by sixteen and produces text that grows absurdly — it is the single most common error in a hand-written fluid formula, and it is why this tool shows the resolved pixel value at each end. A clamp whose endpoints do not land on the sizes you asked for is wrong, and that check catches it immediately.
Use rem rather than px for the minimum and maximum, because a px minimum ignores the reader’s browser font size setting entirely. Someone who has set their default text to 20px because they find 16px hard to read gets no benefit from a clamp whose floor is 16px — the vw part still scales with the window, but the floor does not move. With rem, the whole expression scales with their setting as well as with the viewport. This is a real accessibility failure and a quiet one, because it looks fine on the machine of whoever wrote it.
A vw unit alone, with no clamp, fails at both ends: text becomes unreadably small on a phone and absurdly large on a 4K monitor, and it never stops. The clamp is what makes the technique usable, and the two viewport widths you pick are a design decision rather than a technical one. Common choices are 320 to 1280, or 375 to 1440 for a mobile-first design — the narrow end being the smallest screen you care about and the wide end the width at which the design stops growing.
One thing to watch: the browser zoom and text-only zoom interaction. Because vw units do not respond to text zoom, a layout built entirely from vw-based sizes resists a reader’s attempt to make text bigger. Keeping a rem term in the expression — which this formula always does, since the intercept is in rem — means text zoom still has an effect. That is a further reason the rem output is the right default, and why a pure 4vw font size is a worse idea than it looks.
When to use it
- Fluid heading sizes that scale between a phone and a desktop without media queries.
- Section padding or gaps that grow with the viewport.
- A container width that scales but stops at a maximum.
- Replacing three or four font-size media queries with one declaration.
- Defining a fluid custom property that the rest of a stylesheet can reference.
Common mistakes to avoid
- Mixing rem sizes with px viewport widths, which scales the slope by sixteen.
- Using px for the minimum, which ignores the reader’s own font size setting.
- Using a bare vw size with no clamp, so text never stops shrinking or growing.
- Putting the minimum and maximum the wrong way round, which makes the clamp ignore the preferred value.
- Choosing a very wide size range for one step, which scales uncomfortably fast in the middle.
- Building a whole layout from vw units, which stops text zoom from working.
Frequently asked questions
What is the formula for a fluid clamp?
The slope is the size difference divided by the viewport difference, and the intercept is the small size minus the slope times the small viewport. The preferred value is then "intercept rem + slope×100 vw". Everything must be in the same unit first, which is where most hand calculations go wrong.
Should I use rem or px in clamp?
rem. A px minimum ignores the reader’s browser font size preference entirely, so someone who has increased their default text size gets no benefit from the floor. With rem the whole expression responds to both the viewport and their setting.
Why not just use vw for font size?
Because it never stops. At 4vw, text is 15px on a phone and 77px on a wide monitor, with nothing holding either end. clamp() is what turns a vw-based size into something usable by fixing a floor and a ceiling.
What viewport widths should I use?
A design decision rather than a technical one. 320 to 1280 is a common choice, or 375 to 1440 for a mobile-first design. The narrow end is the smallest screen you care about and the wide end is where you want the design to stop growing.
Does clamp work with properties other than font-size?
Yes, anywhere a length is accepted — padding, margin, gap, width, border-radius. It is particularly useful for section spacing, which otherwise needs its own set of media queries.
Does fluid type break browser zoom?
Full-page zoom works fine, because it scales the viewport too. Text-only zoom is the problem: vw units do not respond to it. Keeping a rem term in the expression, as this formula always does, means text zoom still has an effect on the result.