Resell services with the EthicalSMM API

CSS Clamp Generator

Create a CSS clamp() value that scales a font size or spacing smoothly between two screen widths, in accessible rem units.

Was this tool helpful?

Thanks for the feedback!

This tool writes a CSS clamp() value that grows a font size, or any other length, smoothly from one size on a phone to another on a laptop. You give it two sizes and two screen widths in pixels and it returns a formula in rem and vw that you can paste straight into your stylesheet. It is meant for front-end developers and theme editors who would rather write one line than three media queries for every heading.

What each field means

FieldWhat to enter
Size on small screens (px)The size you want at the narrow end, such as 16 for body text on a phone.
Size on large screens (px)The size you want at the wide end, such as 24.
Small screen width (px)The viewport width where growth starts. Default 375.
Large screen width (px)The viewport width where growth stops. Default 1280.
Root font size (px)The pixel value of 1rem on your site. Default 16, which is the browser default.

Leave the widths and root blank and the tool falls back to 375, 1280 and 16.

How the formula is worked out

The tool draws a straight line between two points: (small width, small size) and (large width, large size). Then it turns that line into CSS.

  1. Slope = (large size − small size) ÷ (large width − small width). This is how many pixels the size gains for each pixel of screen width.
  2. Intercept = small size − slope × small width. This is where the line would cross a zero-width screen.
  3. The middle term of clamp() is the intercept ÷ root, written in rem, plus slope × 100, written in vw. The ×100 is there because 1vw is one hundredth of the viewport width.
  4. The first and last terms are the smaller and larger size ÷ root, in rem, so the value can never go below or above what you asked for.

Every figure is rounded to four decimal places. Under the formula, the results panel shows the size at each width, the size halfway between them, and the growth per 100px of viewport, so you can check the line is what you expected.

Worked example

Body text of 16px on a 375px phone and 24px on a 1280px laptop, with a 16px root. The slope is 8 ÷ 905 = 0.00884, so the vw part is 0.884vw. The intercept is 16 − 0.00884 × 375 = 12.685px, which is 0.7928rem.

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

The tool reports 20px at the halfway width of 828px and growth of 0.884px for every 100px. For a page heading of 32px at 360px rising to 56px at 1440px, it gives clamp(2rem, 1.5rem + 2.2222vw, 3.5rem), which is 44px at 900px.

Why the middle term uses rem plus vw

A font size in vw alone is tied only to the window width. When a reader presses Ctrl and + to zoom, the browser makes each CSS pixel bigger, which makes the window narrower in CSS pixels. A vw-only size therefore stays about the same on screen, and zoom stops working for that text. WCAG success criterion 1.4.4 Resize Text (Level AA) asks that text can be resized up to 200 percent without loss of content or functionality.

The rem part of the middle term, and both rem limits, do grow with zoom. They also follow the default font size a reader has chosen in their browser settings, which some readers with low vision rely on. The bigger the gap between your two sizes, the more of the value comes from vw, so test large jumps at 200% zoom before you ship them.

Edge cases the tool handles

  • If the large screen width is not bigger than the small one, you get "The maximum viewport must be wider than the minimum viewport."
  • A size of zero or less gives "Sizes must be above zero."
  • If the small-screen size is the larger one, the tool still orders the limits correctly and the vw term goes negative, for example clamp(1rem, 1.7072rem + -0.884vw, 1.5rem) for 24px shrinking to 16px. That is valid CSS.
  • If your site sets a different root size, enter it. With a 20px root, the 16 to 24 example becomes clamp(0.8rem, 0.6343rem + 0.884vw, 1.2rem). The vw figure does not change.

To convert a single fixed size, use the PX to REM converter. For depth on cards and buttons, try the CSS box shadow generator.

Frequently asked questions

What does clamp() do in CSS?
clamp(MIN, PREFERRED, MAX) uses the preferred value but never lets it drop below MIN or rise above MAX. For fluid type, the preferred value is a mix of rem and vw so the size follows the screen width between your two limits.
How do I make font size responsive without media queries?
Use a clamp() value with a rem plus vw middle term. For 16px on a 375px screen rising to 24px on a 1280px screen, the tool gives clamp(1rem, 0.7928rem + 0.884vw, 1.5rem).
Why should I not use vw alone for font size?
Text sized only in vw does not get bigger when a reader zooms the page, which can fail WCAG 1.4.4 Resize Text. Adding a rem part and rem limits keeps zoom and the browser font size setting working.
How is the vw number in clamp() calculated?
The tool divides the change in size by the change in screen width to get the slope, then multiplies by 100 because 1vw is 1% of the viewport. Growing 8px over 905px gives 0.884vw.
Which screen widths should I enter?
The defaults of 375px and 1280px roughly match a small phone and a common laptop. Use the narrowest and widest widths of your own layout if they differ, since the size stays fixed outside that range.
Can I use clamp() for padding, margins and gaps?
Yes. clamp() works for any length property, so copy the value and change font-size to padding, margin or gap. The same slope and intercept maths applies.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.