Fluid Typography with clamp()

Intermediate · 14 min

How to grow font sizes smoothly between a minimum and a maximum with clamp(), why the preferred value needs a rem part, and what progress() changes.

Before thisText Spacing and ReadabilityValues, Units and Math Functions

Kevin Powell · 14 min

Fluid Typography with clamp()

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `clamp(min, preferred, max)` gives a size that follows the preferred value but never goes below the minimum or above the maximum. One declaration replaces a stack of media-query steps.
  • A preferred value built from `vw` alone ignores the user's font-size settings and does not respond properly to zoom. Mix in a `rem` term: `clamp(1rem, 0.667rem + 1.667vw, 2rem)`.
  • To go from size A at viewport width W1 to size B at W2, the preferred value is a line: slope `(B - A)/(W2 - W1)` in `vw`, plus an intercept in `rem`. I checked the example: it gives 16px at 320px and 32px at 1280px.
  • `progress(100vw, 320px, 1280px)` returns how far the viewport is between two widths as a plain number, so the interpolation can be written without hand-computed magic numbers.
  • The minimum and maximum define the design. A common guideline is to keep the maximum within about 2.5 times the minimum so zoomed text still scales; test it with real zoom.

Steps versus a slope

The traditional responsive text setup uses media queries: 16px, then 18px above 600px, then 20px above 1000px. Sizes jump at each breakpoint, and each new design size means another query. The alternative is a size that is a function of the viewport width, so it slides continuously.

Straight vw units do that, but badly, as the next section explains. clamp() fixes the worst problems by putting walls on both sides.

Anatomy of clamp()

h1 { font-size: clamp(1.5rem, 1rem + 2vw, 3rem); }

Three arguments: the minimum, the preferred value, and the maximum. The result is the preferred value, unless it falls below the minimum (then the minimum) or above the maximum (then the maximum). It equals max(min, min(preferred, max)).

I ran that rule in Chromium with a 16px root:

Viewport Preferred (1rem + 2vw) Result
320px 22.4px 24px (minimum)
600px 28px 28px
1000px 36px 36px
1400px 44px 44px

At very wide screens it would reach 48px (3rem) and stop. On phones it never drops below 24px.

Why the preferred value has rem in it

A preferred value of 4vw alone is a trap. It scales only with the window, so it ignores the font size a reader has chosen in browser settings, and when someone zooms the page the viewport width in CSS pixels shrinks along with it, so vw-only text does not get bigger. The resize text accessibility criterion expects text to scale. The common fix is to add a rem term: 1rem + 2vw scales with the window and grows with the user’s root size, and zooming still enlarges it because the rem part grows.

A commonly cited rule of thumb is to keep the maximum within about 2.5 times the minimum, so that text still reaches twice its size when the user zooms to 200%. Treat it as a guideline to test, not a guarantee.

Working out the numbers

The preferred value is a straight line through two points: size A at width W1 and size B at width W2.

  • slope = (B − A) / (W2 − W1), which as a percentage of viewport width gives the vw coefficient
  • intercept = A − slope × W1, which is the rem term

For 16px at 320px growing to 32px at 1280px: slope is 16 / 960 = 0.016667, so 1.667vw. The intercept is 16 − 0.016667 × 320 = 10.667px, which is 0.667rem. That gives:

font-size: clamp(1rem, 0.667rem + 1.667vw, 2rem);

I measured this in Chromium at 320, 800, 1280 and 1600px: 16px, 24px, 32px, 32px. The clamp’s floor and ceiling meet the line exactly where the line ends.

That arithmetic is what Kevin Powell calls magic numbers. Tools such as Utopia compute them for a whole type scale, but the values in the stylesheet remain opaque: a reader cannot tell what 0.667rem + 1.667vw was meant to do.

progress(): the line without the arithmetic

Newer CSS has a math function that does the interpolation directly. progress(value, start, end) returns a plain number for how far value is from start to end: 0 at the start, 1 at the end, outside that range if the value is. With it, the same size reads as an intent:

font-size: calc(
  1rem + 1rem * clamp(0, progress(100vw, 320px, 1280px), 1)
);

I ran this exact declaration in Chromium at 320, 800, 1280 and 1600px and got 16px, 24px, 32px and 32px, the same line as the hand-computed clamp(). The numbers in the call are the design’s two widths, so they can be read straight from a spec. progress() is in Chrome 138, Safari 26 and Firefox 155, per MDN’s compatibility data. The video also shows a tan(atan2()) trick that gets the same effect in browsers without progress(); this lesson does not reproduce that technique because I could not read the video’s walkthrough of it, so treat the video as the source for it.

A type scale

Real designs scale a whole set of sizes. Define each as a clamp, and store them as custom properties:

:root {
  --step-0: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
  --step-3: clamp(1.75rem, 1.2rem + 2.4vw, 3rem);
}
h1 { font-size: var(--step-3); }

Custom properties are the subject of custom properties. The same idea fits spacing: padding: clamp(1rem, 4vw, 3rem) gives breathing room that grows with the screen.

What the video covers

Kevin Powell’s “No more clamp() magic numbers” is about 14 minutes and is from October 2026, so it is the freshest video in this group. From its description, it argues that fluid type with clamp() leaves you with magic numbers to work out each time, and that the new CSS progress() function makes the approach predictable and readable, including type scales; a tan(atan2()) trick gets the same result in every browser today. The description names Utopia and MDN’s progress() page as further reading. The worked arithmetic and Chromium measurements are this lesson’s own. The runner-up argues against using viewport units alone for font sizes.

Where this fits

clamp() was introduced in values, units and math functions; here it does a real job. Next, text wrapping and truncation deals with what text does inside its box. Sizing text relative to its container instead of the window arrives with container queries, and the media-query approach is in the frontend roadmap’s responsive design lesson.

Check yourself

  1. 01What are the font sizes of `font-size: clamp(1.5rem, 1rem + 2vw, 3rem)` (16px root) at viewport widths of 320, 600, 1000 and 1400px?

    24px, 28px, 36px and 44px. At 320px the preferred value is 16 + 6.4 = 22.4px, below the 24px minimum, so the minimum applies. At 600px it is 16 + 12 = 28px, at 1000px 16 + 20 = 36px, at 1400px 16 + 28 = 44px, all inside the 24 to 48px range. I measured these in Chromium.

  2. 02Why is `font-size: 4vw` a poor choice for body text?

    It has no floor or ceiling, so text becomes tiny on a phone and huge on a wide monitor. It is also unaffected by the browser's default font-size setting, and it does not enlarge when the user zooms in, which fails the requirement that text can be resized.

  3. 03What preferred value makes text 16px at a 320px viewport and 32px at 1280px?

    Slope = (32 − 16) / (1280 − 320) = 0.016667 per pixel, which is 1.667vw. The intercept is 16 − 0.016667 × 320 = 10.667px, or 0.667rem. So `clamp(1rem, 0.667rem + 1.667vw, 2rem)`. I measured 16px, 24px and 32px at widths 320, 800 and 1280.

  4. 04What does `progress(100vw, 320px, 1280px)` return at a 800px viewport?

    0.5. It calculates `(800 − 320) / (1280 − 320)` as a unitless number, so you can multiply it by the size range and add the minimum. It is not clamped, so wrap it in `clamp(0, …, 1)` to stay in range.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark