CSS3 Intermediate Responsive Design · 8 min read

Fluid Typography with clamp()

Lesson 15 of 41 in the CSS3 learning path.

How do I scale font size between a phone and a desktop without a stack of media queries?

clamp() takes a minimum, a preferred value, and a maximum, and it returns the preferred value when that value is inside the other two. Put a fluid expression in the middle, such as 1.15rem + 1.6vw, so type grows with the viewport but keeps a rem floor. A font size written as vw alone ignores browser text zoom. The title in the sample is clamp(1.75rem, 1.15rem + 1.6vw, 2.6rem).

With a 16px root, that title is the 1.75rem minimum (28px) at a 320px wide viewport, because 1.15rem + 1.6vw is only about 23.5px there. At 800px the preferred value is about 31px, so the title is 31px. At 1600px the preferred value is about 44px, so the maximum 2.6rem (41.6px) wins. The deck and the body use tighter steps. A custom property holds each step so the heading and the column measure can share the scale.

Syntax & Parameters

Syntax
h1 { font-size: clamp(1.75rem, 1.15rem + 1.6vw, 2.6rem); }
.article { width: clamp(16rem, 62vw, 38rem); }

Example

Interactive preview available
styles.css
:root {
  --ink: #1c2430;
  --muted: #5c6b7a;
  --step-body: clamp(1rem, 0.94rem + 0.25vw, 1.125rem);
  --step-deck: clamp(1.15rem, 1rem + 0.7vw, 1.45rem);
  --step-title: clamp(1.75rem, 1.15rem + 1.6vw, 2.6rem);
  --measure: clamp(16rem, 62vw, 38rem);
}
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.5;
  color: var(--ink);
  background: #f4f1ea;
}
.article {
  width: var(--measure);
  margin-inline: auto;
  padding: 1.25rem 1rem 2rem;
}
.kicker {
  margin: 0 0 0.35rem;
  color: #0f766e;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
h1 {
  margin: 0 0 0.45rem;
  font-size: var(--step-title);
  line-height: 1.15;
}
.deck {
  margin: 0 0 0.9rem;
  color: var(--muted);
  font-size: var(--step-deck);
  line-height: 1.35;
}
p {
  margin: 0 0 0.75rem;
  font-size: var(--step-body);
  max-width: 65ch;
}
.scale {
  margin: 1rem 0 0;
  padding: 0.7rem 0.8rem;
  background: #fff;
  border: 1px solid #e3ddd2;
  border-radius: 10px;
  font-size: 0.85rem;
  color: var(--muted);
}

Expected Output

The article column is centered and its width is clamp(16rem, 62vw, 38rem), so it is never narrower than 16rem and never wider than 38rem. The kicker stays a small uppercase label at 0.75rem. The title uses the fluid step. At a 16px root it is 28px on a 320px screen, about 31px at 800px, and 41.6px from roughly 1450px upward. The deck is a middle step. Body copy is only slightly larger than 1rem.

The white note under the paragraphs stays at 0.85rem so you can see that not every line is fluid. Resize the window and the title grows smoothly, then stops. It does not jump at 40rem or 64rem, because there is no breakpoint on the font size.

Line-by-Line Walkthrough

  1. clamp(MIN, PREFERRED, MAX) returns PREFERRED when it sits between the other two, MIN when the preferred value is smaller, and MAX when it is larger.
  2. The title preferred value is 1.15rem + 1.6vw. The rem part follows the root font and text zoom. The vw part follows the viewport width.
  3. A pure vw size does not grow when the reader zooms text. Mixing rem into the preferred value, and using a rem minimum, keeps a floor that zoom can still enlarge.
  4. At a 16px root and a 320px viewport, 1.15rem + 1.6vw is about 23.5px, which is below 1.75rem, so the title is 28px.
  5. At 800px the same expression is about 31px, inside the bounds, so the title is about 31px. No media query is involved.
  6. At 1600px the expression is about 44px, above 2.6rem (41.6px), so the title stops growing. It will not become a poster on an ultra-wide monitor.
  7. --step-deck and --step-body use tighter slopes so a paragraph does not balloon as fast as the title. The kicker stays a fixed 0.75rem label.
  8. The steps live in custom properties on :root. The heading reads var(--step-title) instead of repeating the clamp in every rule.
  9. --measure uses clamp for the column width: at least 16rem, preferred 62vw, at most 38rem. The text column grows and then stops.
  10. max-width: 65ch on the paragraph caps line length in characters, which is a reading measure, separate from the font-size clamp.

You still use a media query when the layout itself must change, such as a sidebar appearing. Type does not need that stack of queries.

Common Pitfalls & Security

clamp does not rescue a preferred value that is always outside the bounds, and a vw-only font size fails text zoom. Those are the two mistakes that make fluid type look broken.

  • If the preferred value is smaller than the minimum at every real viewport, the title sticks at the minimum and never looks fluid. Plot the expression at 320px and at 1600px before you ship it.
  • font-size: 4vw has no rem floor. Browser text zoom often leaves it unchanged, so a reader who needs larger type does not get it.
  • Setting the minimum in px, such as clamp(14px, 1rem + 1vw, 2rem), locks the floor against user font settings. Keep the minimum in rem.
  • A huge maximum, or no maximum, makes a heading enormous on a television. Cap it with a rem you would actually set in a desktop mock.
  • Line-height in px does not follow the fluid size. Use a unitless line-height so the line box grows with the font.

Best Practices & Modern Standards

Store each step in a custom property and build the preferred value as a rem plus a small vw term inside clamp(). Keep a rem minimum and a rem maximum. Use a steeper slope for the title and a gentle one for body text, and cap the column with a ch measure so line length stays readable after the font grows.

Media queries still belong on layout changes, not on a ladder of font sizes. When the type should follow a card instead of the viewport, a container query length such as cqi can replace vw inside the same clamp. :where() can set the base steps at low specificity so a component can override one heading without a fight. Do not animate font-size. If you move anything, transition transform, and honor reduced motion.

Real-World Use

A course article uses one title scale from a phone to a desktop monitor. The title grows from 1.75rem and stops at 2.6rem, the deck grows less, and the body barely moves. The column itself stops at 38rem so a wide monitor does not stretch the line to the edges.

Marketing heroes, documentation pages, and lesson players use the same three steps. The sidebar layout around the article, when there is one, still comes from a media query or a grid. The type does not.

Frequently Asked Questions

What are the three arguments to clamp()?

A minimum, a preferred value, and a maximum. The preferred value is used when it falls between the other two. Below the minimum you get the minimum, and above the maximum you get the maximum. For type, put the fluid rem-plus-vw expression in the middle and keep both bounds in rem.

Why not set font-size with vw alone?

A vw font size tracks the viewport and often ignores text zoom, so a reader who enlarges fonts in the browser may see no change. A rem term in the preferred value and a rem minimum still respond to the root font size. vw should be the small fluid part, not the whole size.

How do I check the title size at phone and desktop widths?

Assume a 16px root. At 320px, 1.15rem + 1.6vw is about 23.5px, so clamp returns the 1.75rem minimum, 28px. At 800px the preferred value is about 31px and that is the size. At 1600px the preferred value is about 44px, so the 2.6rem maximum, 41.6px, wins.

Do I still need media queries if type is fluid?

Yes, for layout. Fluid type removes the stack of font-size breakpoints. It does not move a sidebar, change a card grid, or turn off motion. Keep min-width queries for those structural changes, and keep container queries for a component that should follow its column.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…