← Back to HTML & CSS, page 3

html-css-0049 · Hard senior

How do you build fluid typography with clamp(), and why avoid sizing text with vw alone?

clamp(min, preferred, max) keeps a value between limits. For fluid text, the preferred value mixes a rem part with a vw part, so size grows smoothly between two viewport widths. Using vw alone ignores zoom and user font settings, so use rem limits and test at 200 percent zoom.

Full answer

clamp(min, preferred, max) returns the preferred value, but never below the minimum and never above the maximum. For fluid type, the preferred value mixes a rem part and a vw part, so text grows smoothly with the viewport between two limits, with no breakpoints.

Why not use vw alone? Text sized only in vw does not respond to the browser's zoom or to the user's font size setting in the same way, because the viewport width stays the same. A person with low vision who zooms the page may see no increase. Accessibility guidelines require that text can be enlarged to 200 percent. Adding a rem term to the preferred value, and using rem limits, keeps the text responsive to user settings.

To go from 16px at a 320px viewport to 24px at 1200px, compute the line size = slope * viewport + intercept.

  • slope = (24 - 16) / (1200 - 320) = 0.00909 per px, which is 0.909vw
  • intercept = 16 - 0.00909 * 320 = 13.09px, which is 0.818rem (at 16px per rem)

With a root font size of 16px, the heading is 16px at a 320px viewport, about 20px at 760px, and 24px at 1200px and above. The result is checked in Chrome for each width.

Keep line-height unitless so it scales with each size, and limit line length with ch. A common range is 45 to 75 characters. If you choose limits, use rem for both, so a user's larger default font size scales them, and test the layout with browser zoom at 200 percent.

html
<style>
  body { margin: 0; font-family: Georgia, serif; }
  h1 { font-size: clamp(1rem, 0.818rem + 0.909vw, 1.5rem); line-height: 1.3; }
  p  { max-width: 65ch; line-height: 1.6; }
</style>
<h1 id="h">Fluid heading</h1>
<p>Lines of body text stay near sixty-five characters wide so they are easy to read.</p>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • clamp(min, preferred, max) bounds a value.
  • Preferred value = intercept in rem + slope in vw.
  • vw-only text can fail zoom and user font-size settings.
  • Use rem for limits so default font size changes still apply.
  • Combine with unitless line-height and ch-based line length.

Common mistakes

  • font-size: 4vw with no limits. The text becomes tiny on phones and enormous on wide monitors.
  • using px limits in clamp(). They ignore the user's chosen default font size, so only rem limits respect it.

Follow-ups

  • can clamp() be used for things other than font size?
  • Can clamp() be used for spacing as well?
  • What does line-height with a unitless value do when font size changes?

Tips

  • Use a small helper (a Sass function, a build script or a calculator) to generate clamp() values, and write them as tokens such as --step-1.
  • Show the derivation: slope from the two sizes and viewport widths, then intercept.
  • Mention testing at 200 percent browser zoom.