Skip to content
Tutorials Center
My learning

Topics

Lesson 85 of 139 ยท Responsive Design

Fluid Typography

Make text scale smoothly between screen sizes with clamp(). Build a fluid type scale, keep lines readable, respect zoom, and choose viewport or container units.

A heading that looks perfect on a laptop can take up half a phone screen, and a heading sized for phones looks timid on a big monitor. The classic fix was to change font-size at each breakpoint, so text jumped from one size to another as the window grew. Fluid typography replaces those jumps with a smooth curve: text grows gradually with the available space, between a minimum and a maximum you choose.

This lesson shows how to do it with clamp(), how to work out the numbers, how to build a whole type scale, and how to keep fluid text accessible and readable.

From steps to a smooth line

Here is the old, stepped approach:

CSS
h1 { font-size: 1.75rem; }

@media (width >= 600px)  { h1 { font-size: 2.25rem; } }
@media (width >= 1000px) { h1 { font-size: 3rem; } }

It works, but at 599px the heading is small and at 600px it suddenly grows. You also need more breakpoints for every size in between.

The fluid approach uses one declaration:

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

clamp(MIN, PREFERRED, MAX) returns the preferred value, but never less than the minimum or more than the maximum. (See calc(), min(), max() and clamp() for the full story.) Here the preferred value, 1rem + 3vw, grows with the viewport width, because 1vw is 1% of the viewport's width. On a small phone the heading sits at 1.75rem; on a wide screen it stops at 3rem; in between it scales smoothly.

Fixed, viewport-only and clamped headings

Drag the preview from narrow to wide. Inside the preview, vw means 1% of the preview's width, since the preview is its own page. On a real site it's the browser window.

Why not just use vw?

You might wonder why the preferred value isn't simply 5vw. There are two problems.

First, pure vw has no limits. The middle heading in the demo becomes unreadably small on a phone and absurdly large on a wide monitor. clamp() solves that.

Second, and more serious, pure vw text ignores zoom. When someone presses Ctrl and + to zoom a desktop page, the browser makes the CSS viewport narrower, so 5vw gets smaller in CSS pixels just as everything else gets bigger. The result: the text barely grows at all. Accessibility guidelines require that text can be enlarged to 200%, so text sized only in vw fails.

The fix is to always mix in a rem value: 1rem + 3vw instead of 4vw. The rem part grows with zoom and with the user's default font size, so the text stays zoomable. Keep the rem part substantial and the vw part modest. A good habit is to make sure the maximum size is no more than about 2.5 times the minimum, so zooming always has a visible effect.

Use rem, not px, for the minimum and maximum too. People who set a larger default font size in their browser settings get larger text everywhere, including your clamped headings. Units explains why rem respects that setting.

Working out the preferred value

Picking numbers like 0.8rem + 4vw by trial and error works, but you can calculate them exactly. Decide four things:

  • The minimum size, and the viewport width where it should apply.
  • The maximum size, and the viewport width where it should be reached.

Say you want a heading of 20px (1.25rem) at a 320px viewport, growing to 32px (2rem) at a 960px viewport.

  1. Slope: how much the size changes per pixel of viewport. (32 โˆ’ 20) รท (960 โˆ’ 320) = 12 รท 640 = 0.01875.
  2. The vw part: slope ร— 100 = 1.875vw.
  3. The fixed part: minimum size โˆ’ slope ร— minimum viewport. 20 โˆ’ 0.01875 ร— 320 = 20 โˆ’ 6 = 14px = 0.875rem (dividing by 16).
CSS
h2 { font-size: clamp(1.25rem, 0.875rem + 1.875vw, 2rem); }

Check it: at 960px, 1.875vw is 18px, plus 14px makes 32px. At 320px, it's 6px plus 14px, which is 20px. Between those widths the size follows a straight line; outside them, clamp() holds it at the limits. Many online calculators do this same arithmetic, and now you know what they are doing.

Building a fluid type scale

Real designs have several text sizes that relate to each other: body text, small print and a few heading levels. Define them once as custom properties and use the variables everywhere. Each step can be fluid, with larger steps growing faster than smaller ones, so headings gain more contrast on big screens while body text barely changes.

CSS
:root {
  --step--1: clamp(0.83rem, 0.8rem + 0.15vw, 0.9rem);
  --step-0:  clamp(1rem, 0.95rem + 0.25vw, 1.13rem);
  --step-1:  clamp(1.2rem, 1.1rem + 0.5vw, 1.5rem);
  --step-2:  clamp(1.44rem, 1.25rem + 0.95vw, 2rem);
  --step-3:  clamp(1.73rem, 1.4rem + 1.65vw, 2.67rem);
}
A fluid type scale

Notice that the headings use a smaller line-height than the body. Large text needs less space between lines, and 1.1 to 1.25 usually looks right for headings, as explained in Line Height and Vertical Rhythm. Because the line height is unitless, it scales with the fluid font size automatically.

Keep lines a readable length

Fluid font sizes are only half of readable text. The other half is line length, also called the measure. Lines that are too long make it hard for the eye to find the start of the next line; lines that are too short break the reading rhythm. About 45 to 75 characters per line is a comfortable range for body text.

The ch unit is ideal for this. 1ch is roughly the width of one character (strictly, the "0" glyph) in the current font, so:

CSS
article {
  max-width: 65ch;
}

caps the text column at around 65 characters, whatever the font size. As fluid text grows, the column grows with it, so the measure stays right. It's one more reason the demo above uses max-width: 65ch on the article.

For headings, which are short and big, text-wrap: balance evens out the line lengths so you don't get a single orphaned word on the last line. See Wrapping, Overflow and Hyphens.

Viewport units or container units?

Using vw in the preferred value ties the text to the viewport. That's right for page-level text, such as the main heading and article body, whose size should follow the page.

For text inside a component, the viewport is often the wrong thing to measure. A card's title should depend on how wide the card is, not how wide the window is. A card in a narrow sidebar on a desktop would otherwise get desktop-sized text. That's what the container query units from Container Queries are for: swap vw for cqi.

Text that scales with its container

The same zoom rule applies: keep a rem part in the preferred value, as in 0.7rem + 4cqi, so the text still responds to zoom and user font settings.

A practical split:

  • Page text (body copy, page headings): fluid with vw, from your type scale.
  • Component text (card titles, widget numbers, pull quotes): fluid with cqi, inside a container.
  • Small UI text (labels, buttons, captions): often best left fixed in rem. Tiny text doesn't need to grow, and steady sizes keep controls predictable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does font-size: clamp(1.5rem, 1rem + 2vw, 3rem) do?

    The preferred value 1rem + 2vw grows with the viewport, and clamp() keeps it between the 1.5rem minimum and the 3rem maximum.
  2. 2. Why should the preferred value include a rem part instead of only vw?

    Zooming shrinks the CSS viewport, so vw values get smaller as everything else gets bigger. A rem part keeps text responsive to zoom and the user's default font size.
  3. 3. Which declaration keeps body text at a comfortable line length?

    ch is about one character wide, so max-width: 65ch caps lines at roughly 65 characters, and it grows with the font size.
  4. 4. A card title should scale with the card's width, not the window's. Which unit do you use in the preferred value?

    cqi is 1% of the query container's inline size, so the text follows the component's width wherever it is placed.

Summary

  • Fluid typography replaces breakpoint jumps with clamp(MIN, PREFERRED, MAX), where the preferred value grows with the viewport or container.
  • Always mix rem into the preferred value, such as 1rem + 2vw, so text still responds to zoom and user font settings.
  • Calculate the preferred value from two points: the slope gives the vw part, and the minimum minus slope times width gives the fixed part.
  • Store a fluid type scale in custom properties so every size relates to the others.
  • Cap text columns with max-width in ch (about 45 to 75 characters) and use tighter line height on headings.
  • Use vw for page-level text and cqi for text inside components.

Next: Styling Lists, the first lesson in the Lists, Tables and Forms chapter.