CSS clamp() Generator
Build a fluid font size that grows smoothly between two viewport widths, then copy the CSS. The slope and intercept are shown so you can check the maths.
The base size is used only when the unit is rem. Sizes and viewport widths are whole numbers or decimals, for example 16 or 1.25.
Font size at three widths
The font size the clamp() rule gives at the minimum width, at the midpoint between the two widths, and at the maximum width.
How to use
- Type the smallest font size you want in Minimum font size and the largest in Maximum font size.
- Choose the Unit. Pick px for pixel output, or rem if you want the text to follow the reader's own font setting. When you choose rem, set the Base size for rem (16 is the browser default).
- Enter the Minimum viewport width where growth should begin and the Maximum viewport width where it should stop.
- Read the CSS in the result box, press Copy CSS, and paste the line into your stylesheet.
- Check the three readings under "Font size at three widths". They show what the rule gives at the smallest width, halfway, and the largest width.
Worked example
You want body text of 1rem on a 400px phone, growing to 1.5rem on a 1200px desktop, with a 16px base. Enter 1 and 1.5, choose rem, base 16, and widths 400 and 1200. The page returns:
font-size: clamp(1rem, 0.75rem + 1vw, 1.5rem);
The slope is 0.000625 rem per pixel of viewport, which is 0.01 px of text for every 1px of width. The three readings are 16px at 400px, 20px at 800px (the midpoint) and 24px at 1200px. Below 400px the browser stops at 1rem. Above 1200px it stops at 1.5rem.
The earlier default, 16px to 24px between 320px and 1280px, gives clamp(16px, 13.3333px + 0.8333vw, 24px). That one does not divide exactly, so the page says the values are rounded to four decimal places.
How it works
The font size moves in a straight line between the two viewport widths. Below the minimum width it stays at the minimum size, and above the maximum width it stays at the maximum size. CSS clamp(min, preferred, max) does exactly this: it returns the preferred value, but never less than the minimum or more than the maximum. The syntax is documented on MDN.
Slope is how much the font grows for each 1px of extra viewport width: (maximum size minus minimum size) divided by (maximum viewport minus minimum viewport). Both sizes are first converted to px, so the slope is the same for px and rem input.
Intercept is the font size the line would have at a viewport width of 0px: the minimum size minus slope times the minimum viewport width.
The CSS is clamp(minimum, intercept + viewport term, maximum). The viewport term is 100 times the slope, written in vw, because 1vw is one percent of the viewport width (see viewport-percentage lengths). For 16px at 320px to 24px at 1280px the slope is 1/120, the intercept 13.3333px and the term 0.8333vw, so the font is 20px at 800px.
Rem output divides the intercept by the base size, so the result is in rem. The viewport term is the same in both units. Values are rounded half up to 4 decimal places (6 for the slope), and the result says when rounding has happened.
Common mistakes
- Using vw alone. A size set only in
vwignores the reader's zoom and font settings, which can fail WCAG 2.2 success criterion 1.4.4 (Resize Text). The intercept in rem or px keeps part of the size fixed, so zooming still enlarges the text. - Choosing rem but leaving the wrong base. If your page sets
html { font-size: 62.5% }, the base is 10px, not 16px. Enter 10 or the output will be wrong. - A maximum viewport that is smaller than the minimum. The line then slopes the wrong way. Keep the minimum width below the maximum.
- Testing only at the ends. Resize the browser through the middle. The straight-line growth can feel too fast on tablets.
Frequently asked questions
Why is the intercept sometimes shown as a negative number?
If the font grows quickly over a narrow range, the line would have been below zero at a width of 0px. That is fine. The clamp() minimum stops the size from ever going that low.
Should I use px or rem?
Use rem for text. It respects the font size a person has chosen in their browser. Use px when you need an exact size that must not scale with that setting.
Does clamp() work in every browser?
It is supported in all current major browsers. Check the compatibility table on MDN if you must support very old versions.
Can I use it for spacing as well as fonts?
Yes. The same formula works for padding, margins and gaps. Enter the smallest and largest spacing in place of font sizes and use the output with the property you need.
Is anything I enter stored?
No. The calculation runs in your browser and nothing is sent or saved.