CSS Clamp Generator

A free tool by DeepRit, a Google Ads agency that does PPC and nothing else. We build and run Google Ads accounts so every click is accounted for. Book a free audit call

Free CSS clamp generator for fluid typography. Set the smallest and largest size and screen widths, and copy a clamp() value in rem or px that scales smoothly.

CSS

The quick brown loaf

Free, no sign-up. Runs in your browser: nothing you type is uploaded or saved.

How to use the CSS clamp generator

  1. Set the smallest size, in pixels, such as 16 for body text on phones.
  2. Set the largest size, such as 24 for large screens.
  3. Set the screen widths where the scaling starts and stops, such as 320 px (a small phone) and 1280 px (a laptop).
  4. Choose rem or px for the output, the CSS property, and your root font size (16 px unless you've changed it).
  5. Copy the CSS. A table shows the size you'll get at common screen widths, and the sample text uses the value so you can see it.

The CSS clamp generator gives you one line of CSS that grows smoothly with the screen, with no media queries.

What clamp() does

clamp() is a CSS function that takes three values:

font-size: clamp(MINIMUM, PREFERRED, MAXIMUM);

The browser uses the preferred value, but never goes below the minimum or above the maximum. When the preferred value includes vw (a percentage of the viewport width), the size scales with the screen between the two limits. This is called fluid typography.

For 16 px at 320 px wide up to 24 px at 1280 px wide, the generator gives:

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

On a 320 px phone that's 16 px, on an 800 px tablet 20 px, and on a 1280 px laptop or wider 24 px. Between those points it changes continuously instead of jumping at breakpoints.

The maths behind it

The preferred value is a straight line through the two points you choose: (smallest width, smallest size) and (largest width, largest size).

  1. Slope = (largest size − smallest size) ÷ (largest width − smallest width). Here: (24 − 16) ÷ (1280 − 320) = 0.008333.
  2. In vw, the slope × 100 = 0.8333vw, because 1vw is 1% of the viewport width.
  3. Intercept = smallest size − slope × smallest width = 16 − 0.008333 × 320 = 13.333 px, which is 0.8333rem at a 16 px root.

So the preferred value is 0.8333rem + 0.8333vw. The CSS clamp generator does this arithmetic and rounds to four decimal places, which is far more precise than any screen can show.

Why rem is better than px

The generator writes rem by default, and for font sizes that's the better choice. Rem units follow the user's browser font size setting. Someone who has set their browser to larger text for readability gets proportionally larger text on your site. Pixel values ignore that setting.

There's a subtle accessibility issue with any fluid type: because part of the size comes from vw, zooming the page doesn't enlarge text as much as people expect, since the viewport also gets smaller when you zoom. WCAG requires that text can be resized to 200%. To stay safe:

  • Keep the gap between the minimum and maximum modest (a maximum of about 2.5 times the minimum is a common guideline).
  • Use rem for the minimum and maximum, as the generator does.
  • Test by zooming to 200% in your browser.

Fluid type scales

Most sites use clamp() for a whole set of sizes, from body text up to the main heading. A typical scale might be:

Element Smallest Largest
Body text 16 px 18 px
Small headings (h3) 20 px 24 px
Section headings (h2) 24 px 36 px
Page title (h1) 32 px 56 px

Generate each one with the same screen widths so they all scale together, then store them as custom properties:

:root {
  --step-0: clamp(1rem, 0.9583rem + 0.2083vw, 1.125rem);
  --step-3: clamp(2rem, 1.5rem + 2.5vw, 3.5rem);
}

Headings benefit most from fluid sizing: a 56 px title that looks great on a laptop is far too big for a phone, and fluid sizing handles both without any breakpoints.

Beyond font sizes

clamp() works for any length. The generator can write the value for padding, margin, gap or width too:

  • Section padding that's 24 px on phones and 64 px on desktops.
  • Grid gaps that grow with the layout.
  • Container widths that stay within limits.

The same formula applies; only the property name changes.

Browser support

clamp() works in every modern browser: Chrome, Edge, Firefox and Safari have all supported it since 2020. If you must support very old browsers, put a plain fallback line first, such as font-size: 1.25rem;, followed by the clamp() line. Browsers that don't understand clamp() keep the first value.

For converting other pixel values to rem, use the px to rem converter. To check how your design looks at your own screen size, the screen resolution checker shows your viewport width. For the colors and depth of the elements you're sizing, try the CSS gradient generator and the box shadow generator.

Common mistakes

Choosing screen widths outside the real range. If the largest width is 1920 px but most visitors use 1366 px laptops, the text never reaches its maximum for them. Use the widths your visitors actually have.

A vw-only preferred value, such as clamp(1rem, 2vw, 2rem). It works, but it doesn't respond to the user's font size setting and the size is hard to predict. The rem + vw form the generator writes is more accessible.

Huge ranges. Text that goes from 14 px to 80 px swings too much and causes the zoom problem described above.

Forgetting line-height. Large fluid headings usually need a smaller line-height, around 1.1 to 1.2, than body text.

clamp(), min() and max()

clamp() has two simpler relatives. min(a, b) uses whichever value is smaller, and max(a, b) whichever is larger. In fact, clamp(MIN, VAL, MAX) is exactly the same as max(MIN, min(VAL, MAX)). min() is handy for widths, such as width: min(100%, 720px) for a column that never gets wider than 720 px; max() is useful for minimum tap-target sizes. For anything that should scale between two limits, clamp() is the clearest of the three.

Fluid spacing for whole layouts

Many design systems now use fluid values for spacing as well as type: padding inside sections, gaps between cards, margins around headings. Generating each spacing step with the same screen widths as your type keeps the whole layout in proportion: on a phone everything is a little tighter, on a large screen a little roomier, with no breakpoints to maintain. Generate a set of steps with the CSS clamp generator, store them as custom properties, and use them everywhere.

Testing your values

After adding a clamp() value, resize your browser window slowly from narrow to wide and watch the text. It should grow smoothly, stop growing at your maximum, and never get smaller than your minimum. Developer tools also let you pick device sizes; check a small phone, a tablet and a laptop. Finally, zoom to 200% and make sure text gets bigger, as accessibility guidelines require. The size table in this CSS clamp generator shows what to expect at each width.

A worked example

A bakery's website has a hero heading that's 56 px on desktop: beautiful on a laptop, but on a phone it breaks "Fresh sourdough every morning" into five lines and pushes the order button off the screen. The designer wants it at 32 px on a 375 px phone and 56 px from 1280 px.

She enters 32 and 56 as the sizes and 375 and 1280 as the widths, and keeps rem. The CSS clamp generator gives font-size: clamp(2rem, 1.3785rem + 2.6519vw, 3.5rem);. The table shows 32 px at 375 px, about 42.4 px at 768 px, 49.2 px at 1024 px and 56 px at 1280 px and above.

She replaces the old heading size and its two media queries with that single line. On her phone, the heading now fits in two lines with the button visible; on her laptop it's unchanged. She zooms to 200% to check accessibility, and the text grows as it should.

Need more customers from Google?

This tool is free and runs in your browser. If your business also needs more leads or sales, Google Ads is where people search with their wallet out. That is the whole job of DeepRit: a PPC-only agency that sets up and runs Google Ads accounts so every click is accounted for. You keep full ownership of your account, get a written report every week, and can stop at any time. See our pricing or ask for a free audit.

Frequently asked questions

What does CSS clamp() do?

It sets a value that scales between a minimum and a maximum. With a vw-based preferred value, sizes grow smoothly with the screen width.

How do I calculate a clamp() value for fluid typography?

Work out the slope between your two sizes and widths, convert it to vw, and add the intercept in rem. This CSS clamp generator does it for you.

Should I use rem or px in clamp()?

Use rem for font sizes, so text respects each person's browser font size setting.

Does clamp() work in all browsers?

Yes, in all modern browsers since 2020. Add a plain fallback value first if you need to support much older ones.

Can I use clamp() for padding and margins?

Yes. clamp() works for any length, including padding, margin, gap and width.

Is this CSS clamp generator free?

Yes. It's free, needs no sign-up and runs in your browser.

Free PPC audit

See where your ad budget leaks.

We look at your Google Ads account before the call, then walk you through what we'd fix first, in dollars. No obligation, no hard sell.

Book a free audit call 30-minute call · Free · The audit is yours to keep