How to Calculate Fluid Font Sizes with CSS clamp()
Last updated: · 9 min read
Fluid typography means a font size that grows smoothly with the width of the screen: text that is 16 pixels on a narrow phone reaches 24 pixels on a wide desktop and changes in a straight line in between, not in steps. The CSS function that does this in one line is clamp(), which takes a lower bound, a preferred value that depends on the viewport width, and an upper bound. The hard part is finding the middle value, because you need the equation of a line that gives exactly the two sizes you want at the two viewport widths you chose. This guide explains that equation (slope and intercept), why the result should be written in rem, how browser zoom affects fluid text, and the mistakes people make most often. The values in the worked example are real output from the CSS clamp() Calculator.
Formula
slope = (max size − min size) ÷ (max viewport − min viewport) intercept = min size − slope × min viewport font-size: clamp(min, intercept + (slope × 100)vw, max); 1vw = 1% of the viewport width → vw coefficient = slope × 100 rem output: pixel values are divided by the root font size (16 px by default) clamp(MIN, VAL, MAX) = max(MIN, min(VAL, MAX))
The font size is treated as a linear function of the viewport width: size = intercept + slope × width. The slope says how many pixels the size gains for each pixel of width; the intercept is the size the line would give at a width of zero, and it positions the line so that it passes exactly through the minimum size at the minimum viewport. In CSS the width is expressed with the vw unit; under the CSS Values and Units specification 1vw is one percent of the viewport width, which is why the slope is multiplied by 100. Left alone, this line would keep shrinking below the minimum viewport and keep growing above the maximum one; clamp() holds it between the two bounds. According to the specification, clamp(MIN, VAL, MAX) represents the same value as max(MIN, min(VAL, MAX)). The tool rounds values to four decimal places, always uses a dot as the decimal separator in the CSS, and also shows the resulting size at the minimum, middle and maximum viewport widths.
What the inputs mean
- Minimum font size
- The size used at the minimum viewport width and below, in pixels (1 to 1000). For body text this is usually 16 px.
- Maximum font size
- The size used at the maximum viewport width and above, in pixels (1 to 1000). If it equals the minimum, clamp() is not needed and the tool returns a fixed value.
- Minimum viewport width
- The viewport width at which growth starts, in pixels (100 to 7680). For small phones, 320 or 360 is typical.
- Maximum viewport width
- The viewport width at which growth stops. It must be greater than the minimum viewport width; the widest state of your content area (for example 1280) is the usual choice.
- Root font size
- The font size of the html element, in pixels (1 to 64). The browser default is 16 px; if your site changes the root size (for example to 10 px with 62.5%), enter that value, otherwise the rem figures will be wrong.
- Output unit
- rem (default) or px. rem follows the font size the user has chosen in the browser; px ignores that setting.
Worked example: Body text that is 16 px at 320 pixels and 24 px at 1280 pixels
- Input: Minimum font size = 16, Maximum font size = 24, Minimum viewport width = 320, Maximum viewport width = 1280, Root font size = 16, Output unit = rem.
- Slope = (24 − 16) ÷ (1280 − 320) = 8 ÷ 960 = 0.008333. The text grows by 0.008333 px for every pixel of viewport width, and the vw coefficient is 100 times that. The tool reports it as: Slope: 0.8333 px per 100 px of viewport width (0.8333 vw)
- Intercept = 16 − 0.008333 × 320 = 16 − 2.6667 = 13.3333 px, which divided by 16 is 0.8333 rem. The tool's line: Intercept: 13.3333 px (0.8333 rem)
- The lower bound is 16 ÷ 16 = 1rem and the upper bound 24 ÷ 16 = 1.5rem. Output: font-size: clamp(1rem, 0.8333rem + 0.8333vw, 1.5rem);
- Check: at a width of 800 pixels, 13.3333 + 0.008333 × 800 = 20 px. The tool's three sample lines agree: At 320 px wide: 16 px; At 800 px wide: 20 px; At 1,280 px wide: 24 px.
- With px as the output unit the expression becomes font-size: clamp(16px, 13.3333px + 0.8333vw, 24px); and the tool adds this note: The px unit ignores the browser's font-size setting; rem is recommended for accessibility.
- A heading: 32 px at 360 pixels and 56 px at 1440 pixels. Slope = 24 ÷ 1080 = 0.022222; intercept = 32 − 0.022222 × 360 = 24 px. Output: font-size: clamp(2rem, 1.5rem + 2.2222vw, 3.5rem); with the midpoint At 900 px wide: 44 px
- If the minimum and maximum size are the same (18), the output is font-size: 1.125rem; Note: The size is fixed: minimum and maximum are equal, so clamp() is not needed.
- If the viewport widths are entered the wrong way round (minimum 1280, maximum 320), the tool does not calculate: The maximum viewport width must be greater than the minimum viewport width.
Result: The CSS line for 16 to 24 px between 320 and 1280 px is font-size: clamp(1rem, 0.8333rem + 0.8333vw, 1.5rem); The expression stays at 16 px below 320 pixels and at 24 px above 1280 pixels; in between it grows by 0.8333 px for every 100 pixels of width.
🔠 CSS clamp() Calculator (Fluid Typography)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
The reason to use the rem output is accessibility. If a user raises the browser's default font size from 16 to 20 pixels, 1rem is now 20 pixels; the expression from the example then runs between 20 px and 30 px, and the text grows with the user's preference. Written in px (clamp(16px, …, 24px)), the same expression ignores that preference. A size given in vw alone (font-size: 2vw) is worse still: it has no lower bound and responds neither to the user's setting nor to zoom. Browser zoom is a separate matter. When a page is zoomed, the width of the viewport measured in CSS pixels shrinks, so the part that depends on vw shrinks as well, and fluid text grows more slowly than the zoom factor. With the expression from the example, zooming to 200% in a 1280-pixel window reduces the viewport to 640 CSS pixels; the computed size becomes 18.67 px, and on screen the text is about 37 pixels tall where it was 24: 1.56 times larger, not twice. In this example it takes roughly 280% zoom to reach double the size. Success Criterion 1.4.4 of WCAG 2.2 requires that text can be resized up to 200 percent without assistive technology and without loss of content or functionality. So keep the ratio between the maximum and minimum size moderate, and test the page at 200% zoom as well as with a larger browser font-size setting. Fluid sizing pays off most on headings; for body text a narrow range, such as 16 to 18 px, is usually enough.
Common mistakes
- Setting the font size in vw alone. font-size: 2vw becomes unreadably small on narrow screens, grows without limit on wide ones and ignores the user's font-size setting; add bounds with clamp() and the user's preference with rem.
- Using the px output. When the bounds and the intercept are in px, the browser's font-size setting is ignored, which is why the tool adds a warning under px output.
- Entering the wrong root font size. If the site has a rule such as html { font-size: 62.5% }, 1rem is 10 pixels; leaving the root at 16 makes every rem value 1.6 times too small.
- Rounding the slope or intercept by hand. Writing 0.8vw in place of 0.8333vw lowers the size at the maximum viewport by about 0.4 pixels; copy the tool's four-decimal values as they are.
- Choosing a very large ratio between the minimum and maximum size. The steeper the slope, the larger the vw-dependent share and the less the text grows under zoom; do not publish without testing at 200% zoom.
- Assuming vw is measured against the parent element. vw is always the width of the viewport; text inside a narrow sidebar is sized by the window, not by the sidebar.
- Writing the bounds in the wrong order. In an expression such as clamp(1.5rem, …, 1rem) the specification lets the minimum win, so the result is always 1.5rem.
Frequently asked questions
What does clamp() do?
It takes three values: a minimum, a preferred value and a maximum. The preferred value is used when it lies between the two bounds; otherwise the nearer bound is used. Under the CSS Values and Units specification, clamp(MIN, VAL, MAX) is the same as max(MIN, min(VAL, MAX)).
Why does the middle value contain both rem and vw?
The vw part provides the growth (the slope) and the rem part the starting height of the line (the intercept). With vw alone the line starts at zero and cannot pass through both of the sizes you want at both of the widths you chose. The rem share also takes the user's font-size preference into account.
Which viewport widths should I choose?
For the minimum, take the narrowest phone width you support, usually 320 or 360 px. For the maximum, take the point where your content stops getting wider, for instance 1280 or 1440 px. Outside those two points the size stays fixed.
Is fluid typography a problem for accessibility?
It can be if it is set up carelessly. Because the vw-dependent share shrinks under browser zoom, the text grows more slowly than the zoom factor. Write the bounds in rem, keep the gap between the maximum and minimum size moderate, and test the 200% resize that WCAG 1.4.4 asks for on your own page.
What happens if the minimum is larger than the maximum?
Under the specification, when the bounds are in the wrong order the minimum wins. In the tool, entering a maximum size smaller than the minimum is not an error: it produces an expression with a negative slope for text that shrinks as the viewport widens, puts the bounds in the right order and says so in a note.
Can I use the same expression for spacing?
Yes. clamp() is a general math function, not a font-size feature, so the same expression works in properties that accept a length, such as padding, margin and gap. The tool starts its line with font-size; take the clamp(…) part and use it with another property.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.