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
| Field | What 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.
- Slope = (large size − small size) ÷ (large width − small width). This is how many pixels the size gains for each pixel of screen width.
- Intercept = small size − slope × small width. This is where the line would cross a zero-width screen.
- 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. - 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?
How do I make font size responsive without media queries?
Why should I not use vw alone for font size?
How is the vw number in clamp() calculated?
Which screen widths should I enter?
Can I use clamp() for padding, margins and gaps?
Related guides
- Free Developer Tools That Replace a Dozen Browser Tabs Free developer tools grouped by job: format, convert, encode, generate and test. What each one does, which run on the server, and the limits to know f...
- How to Beautify CSS, HTML and XML (Online, VS Code, Prettier) Beautifying adds line breaks and indentation so minified CSS, HTML or XML is readable again. How the free formatters work, their limits, and VS Code a...
- How to Format JSON: Pretty-Print, Validate and Read Errors Paste JSON into a formatter to indent it and catch errors. The exact syntax rules, how to read an error position, and how to do it in VS Code, Python...