Skip to content
All posts

§ AB-B09 · Web techniques

Fluid type with clamp()

Type and spacing that grow with the screen in one line of CSS, plus text-wrap to fix ragged headings and lonely last words.

· 4 min read · 3 demos

TL;DR
  • Write clamp(min, preferred, max). The middle value grows with the width. The other two cap it.
  • Pick two points, like 16px at 360px and 20px at 1280px. Draw a straight line between them. That line is the middle value.
  • Keep a rem part in the middle so text still grows when people zoom.
  • Use the same trick for spacing, not only type.
  • Add text-wrap: balance to headings and text-wrap: pretty to body text.

One size per breakpoint

The classic way is a size per breakpoint. 16px on phones, 18px from 768px, 20px from 1280px. It works, but it jumps. A tablet held one way gets one size, and turned the other way gets a different one. Every jump is another media query to keep in sync.

clamp() replaces the steps with a line. Text grows smoothly with the width and stops at a floor and a ceiling.

font-size: clamp(1rem, 0.902rem + 0.435vw, 1.25rem);

Read it from the outside in. Never smaller than 1rem, which is 16px. Never larger than 1.25rem, which is 20px. In between, follow the middle value.

Try it on a page

The box below acts as the screen. Drag its width. The heading, the body text and the padding all follow it. The demo uses cqi, which is 1% of the box width. A real page uses vw, 1% of the window width, in exactly the same way.

A page that follows its width
/* The box is the "screen". Real pages use vw the same way. */
.fluid-page {
  container-type: inline-size;
}

.fluid-page .inner {
  /* 16px to 40px */
  padding: clamp(1rem, 0.413rem + 2.609cqi, 2.5rem);
}

.fluid-page h3 {
  /* 28px at 360px wide, 48px at 1280px */
  font-size: clamp(1.75rem, 1.261rem + 2.174cqi, 3rem);
  line-height: 1.05;
  text-wrap: balance;
}

.fluid-page p {
  /* 16px at 360px wide, 20px at 1280px */
  font-size: clamp(1rem, 0.902rem + 0.435cqi, 1.25rem);
  line-height: 1.5;
  text-wrap: pretty;
}
Body 16 to 20px, heading 28 to 48px, padding 16 to 40px, all between 360px and 1280px wide. Wider boxes are scaled down to fit. The numbers are the real sizes.

Below 360px everything sits at its minimum. Above 1280px everything sits at its maximum. In between there are no steps at all. Press Replay to jump through three widths.

The slope math

The middle value is a straight line through two points. Pick them first. Our body text is 16px at 360px wide and 20px at 1280px wide.

  1. Slope. The size grows 4px over 920px of width. That is 4 ÷ 920 = 0.00435px per pixel of width.
  2. As vw. 1vw is 1% of the width, so multiply by 100. That gives 0.435vw.
  3. Base. Find the size at width 0. That is 16 minus 0.00435 × 360, which is 14.43px, or 0.902rem.
  4. Put it together. clamp(1rem, 0.902rem + 0.435vw, 1.25rem).

Check it at 1280px. 14.43 + 0.00435 × 1280 is 20. The line hits both points.

Change the two sizes and watch each step update.

From two sizes to a clamp()
// One straight line from (minW, minPx) to (maxW, maxPx), capped at both ends.
function fluid(minPx: number, maxPx: number, minW = 360, maxW = 1280) {
  const slope = (maxPx - minPx) / (maxW - minW);   // px per px of width
  const base = minPx - slope * minW;                // size at width 0
  const rem = (px: number) => +(px / 16).toFixed(3);
  return `clamp(${rem(minPx)}rem, ${rem(base)}rem + ${+(slope * 100).toFixed(3)}vw, ${rem(maxPx)}rem)`;
}

fluid(16, 20); // "clamp(1rem, 0.902rem + 0.435vw, 1.25rem)"
16px at 360px and 20px at 1280px give 0.435vw and a base of 0.902rem. The code is the same function the demo uses.

The base is in rem on purpose. If the middle value were only vw, browser zoom would do nothing to it, because zoom doesn't change the window width in CSS pixels. A rem part keeps text growing with zoom. That matters for people who need bigger text.

Space grows too

Type that grows inside fixed padding looks cramped on big screens and loose on small ones. Use the same line for spacing. The section padding in the first demo goes from 16px to 40px over the same range.

Put the values in tokens once, then use them everywhere:

:root {
  --text-body: clamp(1rem, 0.902rem + 0.435vw, 1.25rem);
  --text-h1: clamp(1.75rem, 1.261rem + 2.174vw, 3rem);
  --space-section: clamp(1rem, 0.413rem + 2.609vw, 2.5rem);
}

Keep the ratio between min and max modest for body text. 1.25 is plenty. Headings can stretch more. Ours go from 28px to 48px, a ratio of about 1.7.

Better line breaks

Fluid sizes change where lines break. A heading that fit on two even lines at one width leaves one word alone at another. Two values fix most of that.

  • balance spreads a heading evenly over its lines. Chrome only balances blocks of 6 lines or fewer, so use it on headings, not paragraphs.
  • pretty fixes the last line of body text, so a paragraph doesn't end on one short word. It costs a little more work, so browsers run it where it counts.

Compare them side by side, and change the width.

text-wrap: normal · balance · pretty
/* What you would ship */
h1, h2, h3 { text-wrap: balance; }
p, li      { text-wrap: pretty; }
Same text and width on both sides. At 230px the normal paragraph ends on one word, and pretty fixes it. At 260px the normal heading leaves one word alone, and balance evens it out.

Notice that neither changes the width of the text box. Balance can make a heading look narrower, so leave room for it in your layout.

Support

  • clamp() works in every current browser.
  • Container units like cqi: Chrome 105, Safari 16, Firefox 110. Real pages use vw, which works everywhere.
  • text-wrap: balance: Chrome 114, Safari 17.5, Firefox 121.
  • text-wrap: pretty: Chrome 117, Safari 26. Firefox doesn't support it yet.

The fallback is free. A browser that doesn't know balance or pretty ignores them and wraps text the normal way. Nothing breaks, so add them today.

Next

Fluid type follows the screen. Often the space you have is a slot inside it. Next: Container queries.

Next · AB-B10 Container queries: style the slot, not the screen