CSS clamp() generator — fluid font sizes and spacing between two breakpoints

Give a minimum and maximum size and the two viewport widths where each should apply, and get one CSS clamp() declaration that scales smoothly between them, in rem or px, with no media queries.

Live demo · real result from this tool
Output unit

CSS

font-size: clamp(1rem, 0.8333rem + 0.8333vw, 1.5rem);

Value at common widths

ViewportSize
320px16px
375px16.46px
768px19.73px
1024px21.87px
1280px24px
1440px24px
1920px24px

Using rem for the fixed parts keeps the value responsive to the user's browser font-size setting (WCAG 1.4.4 resize text).

Processed on your device — nothing is uploaded.

How to use the CSS clamp generator

  1. Enter the Minimum size and Maximum size in px, for example 16 and 24.
  2. Enter At viewport width (where the minimum applies) and Up to viewport width (where the maximum is reached), for example 320 and 1280.
  3. Choose the Output unit, rem or px. For rem, check the Root font size (default 16 px).
  4. Set the CSS property. The default is font-size.
  5. Copy the declaration, and check the Value at common widths table.

What it does and when to use it

Fluid sizing replaces a ladder of media queries with one line. A heading can grow from 16 px on a small phone to 24 px on a laptop, then stop. Use it for headings, text, padding and gaps.

Build a full type scale with the CSS typography generator. For breakpoint-based rules you still need, use the media query generator. To compare rem, vw and other units, look them up in the CSS units reference.

How it works

The size is a straight line between two points (viewport width, size):

slope     = (maxSize − minSize) / (maxVw − minVw)
intercept = minSize − slope × minVw
preferred = intercept + slope × 100 vw

The intercept is converted to rem by dividing by the root font size (in rem mode), and the slope is shown as vw. clamp() then holds the result between the smaller and the larger size. All numbers are rounded to 4 decimal places. If the second width is not larger than the first, you get the error The second viewport width must be larger than the first. Everything is calculated in your browser.

Worked examples

16 px at 320 px wide, growing to 24 px at 1280 px, in rem:

font-size: clamp(1rem, 0.8333rem + 0.8333vw, 1.5rem);
375px | 16.46px
768px | 19.73px
1024px | 21.87px
1440px | 24px

Spacing in px, 32 to 64 px between 400 and 1200 px wide:

margin-block: clamp(32px, 16px + 4vw, 64px);

Shrinking padding, 48 px on phones down to 28 px on wide screens (375 to 1440 px):

padding-inline: clamp(1.75rem, 3.4401rem - 1.8779vw, 3rem);

Limits and tips

  • The table assumes your root size. It shows pixels for a root font size equal to the one you entered.
  • Steep slopes can block zoom. If most of the size comes from vw, zooming the page barely changes the text. Keep the range modest for body text.
  • Viewport means the whole window. For sizes based on a component’s width, swap vw for container query units (cqi) by hand.

Frequently asked questions

Should I use rem or px?
Use rem for font sizes. The fixed parts then follow the reader's browser font-size setting, which helps meet WCAG 1.4.4 (Resize Text). px is fine for spacing that should stay the same whatever the font setting.
Can the value get smaller as the screen gets wider?
Yes. Make Maximum size smaller than Minimum size. The tool writes a negative vw term and keeps the smaller value as the clamp floor, so the size shrinks between the two widths and holds outside them.
What is "Root font size" for?
It converts your pixel sizes to rem. Use 16 unless your site sets a different font-size on the html element. If the reader changes their browser default, the rem parts scale but the vw part does not, so the real breakpoints shift a little.
Does clamp() work in all browsers?
CSS clamp() is supported in all current major browsers. Very old browsers such as Internet Explorer ignore the whole declaration, so add a plain fallback line before it if you still support them.
Can I use it inside calc() or for other properties?
Yes. Change CSS property to any length property, such as gap, margin-block, padding-inline or line-height in rem. The tool does not check that the property name exists, so check for typos.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close