Skip to article content
FrontendXLesson 24 / 30

Fluid typography and spacing with clamp

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

intermediate · about 35 minutes · 2 exercises

  • Predict
  • Implement

What you will be able to do

#
  • Use clamp to scale type and space between a minimum and a maximum.
  • Set a readable measure and line height.

Overview

#

clamp(min, preferred, max) returns the preferred value, bounded by a minimum and a maximum. It is the tool for fluid type and spacing that scales with the viewport without a stack of media queries.

snippet.css
:root {
  --step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --step-2: clamp(1.5rem, 1.3rem + 1vw, 2.25rem);
  --space-m: clamp(1rem, 0.8rem + 1vw, 2rem);
}

h1 { font-size: var(--step-2); }
p { font-size: var(--step-0); }
.section { padding-block: var(--space-m); }

The preferred value is usually a mix of a fixed size and a viewport unit, so text grows with the screen but never below the minimum or above the maximum. Using vw alone is dangerous because it ignores user font-size preferences and can produce unreadably small text; clamp with a rem floor respects the user's base size.

Readability depends on the measure, the line length. A comfortable measure is roughly 45 to 75 characters. Cap it with max-inline-size in ch units, which are relative to the current font.

snippet.css
.prose { max-inline-size: 65ch; line-height: 1.6; }

Line height should scale inversely with font size: large headings need tighter leading, body text needs more. A common pattern is line-height: 1.1 for display headings and 1.5 to 1.7 for body copy. Never set line-height in a unit that ignores the font size, such as a fixed pixel value on text that scales.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Create a fluid heading size that is never smaller than 1.5rem, never larger than 2.5rem, and scales with the viewport in between.

    starter.css
    h1 { /* add font-size */ }

    Reveal the reference answer
    reference.css
    h1 { font-size: clamp(1.5rem, 1.2rem + 1.5vw, 2.5rem); }

    Authored expected output: Heading text scales with the viewport and is bounded between 1.5rem and 2.5rem.

    clamp bounds the fluid preferred value with a rem floor and ceiling.

  2. Exercise 2Predict

    Given font-size: clamp(1rem, 0.5rem + 2vw, 2rem), what is the computed size on a 400px viewport?

    Choose the best answer

Common misconceptions

#
  • clamp with a vw preferred value ignores user font-size settings.
  • Longer lines are more efficient because they fit more words.

Follow-up practice

#
  • html-css-25-responsive-images

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.