CSS clamp() & Fluid Typography
Fluid typography scales text smoothly and continuously between a minimum
and maximum size as the viewport changes — no sudden jumps at breakpoints, no media queries for font sizes.
The key is clamp(min, preferred, max), paired with a viewport-relative
preferred value. This chapter covers how clamp() works mathematically,
how to build a complete fluid type scale, how to extend the same technique to spacing, and how to package it
all into a custom-property design token system.
clamp(min, preferred, max)
worksHow clamp() Works
clamp(minimum, preferred, maximum) evaluates the
preferred expression and returns it — unless it falls below the minimum or exceeds the maximum, in which
case the boundary value is used:
16px
never smaller
scales with viewport width
2.5vw + 8px
40px
never larger
/* clamp(MIN, PREFERRED, MAX) */
h1 { font-size: clamp(1.75rem, 4vw + 0.5rem, 3.5rem); }
/* On a 320px screen:
4vw = 12.8px, + 0.5rem(8px) = 20.8px
20.8px < 1.75rem(28px) → clamps to 28px (MIN) */
/* On a 768px screen:
4vw = 30.7px, + 8px = 38.7px
1.75rem < 38.7px < 3.5rem(56px) → uses 38.7px (FLUID) */
/* On a 1440px screen:
4vw = 57.6px, + 8px = 65.6px
65.6px > 3.5rem(56px) → clamps to 56px (MAX) */
/* min() and max() — single-boundary versions */
p { font-size: max(1rem, 1.5vw); } /* never below 1rem, grows upward */
p { font-size: min(2rem, 4vw); } /* never above 2rem, shrinks with small vp */
/* clamp = min(MAX, max(MIN, PREFERRED))
They are mathematically equivalent */
h1 { font-size: min(3.5rem, max(1.75rem, 4vw + 0.5rem)); }
Crafting the Preferred Expression
The middle argument — the preferred value — is what makes the scaling fluid. It must be a calculation that grows with the viewport. The most effective pattern combines a viewport-width percentage with a rem-based offset:
The intercept is often expressed in rem (÷16) for accessibility. Most designers skip the exact maths and tune by feel, targeting the right size at a specific viewport.
In practice: round to 1 decimal. Tools like utopia.fyi generate these values for entire type scales automatically.
Abrupt reflow at each breakpoint. Looks unnatural at in-between sizes.
One declaration. No breakpoints. Smooth at every viewport width including unusual sizes.
Building a Complete Fluid Type Scale
A type scale defines all text sizes in your design. With clamp(), each step scales fluidly. Resize your browser to watch this entire
scale respond:
/* ── Root: respect browser preference ── */
html { font-size: 100%; }
/* ── Fluid type scale — no media queries needed ── */
.text-display,
h1.display { font-size: clamp(2.25rem, 5vw + .75rem, 4.5rem); }
h1 { font-size: clamp(1.875rem, 4vw + .5rem, 3.75rem); }
h2 { font-size: clamp(1.5rem, 3vw + .5rem, 3rem); }
h3 { font-size: clamp(1.25rem, 2.5vw + .25rem, 2rem); }
h4 { font-size: clamp(1.125rem, 2vw + .25rem, 1.5rem); }
h5, h6 { font-size: clamp(1rem, 1.75vw + .25rem, 1.25rem); }
body, p { font-size: clamp(1rem, 1.5vw + .25rem, 1.125rem); }
small, .text-sm{ font-size: clamp(.8125rem, 1vw + .25rem, 1rem); }
/* ── Line heights ── */
h1, h2, h3 { line-height: 1.1; }
h4, h5, h6 { line-height: 1.3; }
body, p { line-height: 1.7; }
/* ── Readable line length ── */
p, li, blockquote { max-width: 65ch; }
Fluid Spacing with clamp()
The same technique works for any numeric property. Fluid spacing means components breathe proportionally at every size — sections have more room on wide screens, content is tight but readable on mobile, all without a single breakpoint:
/* ── Fluid section padding ── */
.section { padding-block: clamp(3rem, 8vw, 6rem); }
.section-lg{ padding-block: clamp(5rem, 12vw, 10rem); }
/* ── Fluid container padding ── */
.container {
width: min(100% - 2rem, 1200px);
margin-inline: auto;
padding-inline: clamp(1rem, 4vw, 3rem);
}
/* ── Fluid gap in grids ── */
.grid { gap: clamp(1rem, 3vw, 2rem); }
.stack { gap: clamp(.75rem, 2vw, 1.5rem); }
/* ── Fluid border radius ── */
.card { border-radius: clamp(.5rem, 2vw, 1.25rem); }
.hero { border-radius: clamp(1rem, 3vw, 2rem); }
/* ── Fluid max-width for prose ── */
.prose {
max-width: 65ch; /* font-relative */
font-size: clamp(1rem, 1.5vw + .25rem, 1.125rem);
line-height:1.7;
}
Design Token System with Custom Properties
The best way to manage a fluid type and spacing system is to store all values as
custom properties (CSS variables) on :root. This creates a single source
of truth you can reference throughout your stylesheet:
:root {
/* ── Typography tokens ── */
--text-display: clamp(2.25rem, 5vw + .75rem, 4.5rem);
--text-h1: clamp(1.875rem, 4vw + .5rem, 3.75rem);
--text-h2: clamp(1.5rem, 3vw + .5rem, 3rem);
--text-h3: clamp(1.25rem, 2.5vw + .25rem, 2rem);
--text-h4: clamp(1.125rem, 2vw + .25rem, 1.5rem);
--text-body: clamp(1rem, 1.5vw + .25rem, 1.125rem);
--text-sm: clamp(.8125rem, 1vw + .25rem, 1rem);
--text-xs: clamp(.75rem, .75vw + .25rem, .875rem);
/* ── Spacing tokens ── */
--space-xs: clamp(.25rem, .5vw, .5rem);
--space-sm: clamp(.5rem, 1vw, 1rem);
--space-md: clamp(1rem, 2vw, 2rem);
--space-lg: clamp(1.5rem, 3vw, 3rem);
--space-xl: clamp(2.5rem, 5vw, 5rem);
--space-2xl: clamp(4rem, 8vw, 8rem);
--space-3xl: clamp(6rem, 12vw, 12rem);
/* ── Layout tokens ── */
--container: min(100% - 2rem, 1200px);
--gap: clamp(1rem, 3vw, 2rem);
--radius-sm: clamp(.25rem, 1vw, .5rem);
--radius-md: clamp(.5rem, 2vw, 1rem);
--radius-lg: clamp(.75rem, 2.5vw, 1.5rem);
}
/* ── Using the tokens ── */
h1 { font-size: var(--text-h1); }
h2 { font-size: var(--text-h2); }
body { font-size: var(--text-body); }
.section { padding-block: var(--space-2xl); }
.card { padding: var(--space-md); border-radius: var(--radius-md); }
.grid { gap: var(--gap); }
.container { width: var(--container); margin-inline: auto; }
clamp() Patterns Reference
| Use case | clamp() expression | Notes |
|---|---|---|
| H1 heading | clamp(1.875rem, 4vw + .5rem, 3.75rem) |
30px → 60px over 320–1200px |
| H2 heading | clamp(1.5rem, 3vw + .5rem, 3rem) |
24px → 48px |
| Body text | clamp(1rem, 1.5vw + .25rem, 1.125rem) |
16px → 18px — subtle, comfortable |
| Section padding | clamp(3rem, 8vw, 6rem) |
48px → 96px — breathes on wide screens |
| Container padding | clamp(1rem, 4vw, 3rem) |
16px → 48px horizontal gutter |
| Card padding | clamp(1rem, 3vw, 1.5rem) |
16px → 24px |
| Grid gap | clamp(1rem, 3vw, 2rem) |
16px → 32px |
| Hero padding | clamp(4rem, 10vw, 8rem) |
64px → 128px |
| Fluid container | width: min(100% - 2rem, 1200px) |
Full width mobile, capped at 1200px desktop |
| Min tap target | min-height: max(44px, 2.75rem) |
Always at least 44px |
Try It Yourself
A complete fluid type + spacing token system in action. Resize the preview pane to watch every element scale smoothly:
Quick Check — Chapter 42
clamp(min, preferred, max) has three distinct zones of
behaviour:Zone 1 — Clamped low (below min): When the preferred expression evaluates to a value smaller than min, the min value is returned. Example: on a very small screen,
2vw might be only 6.4px on a 320px device — far below a 1rem (16px)
minimum — so the minimum 1rem is used instead.Zone 2 — Fluid (between min and max): When the preferred value falls between min and max, it is used directly and scales fluidly with the viewport. This is the responsive range where the text or spacing actually grows/shrinks smoothly.
Zone 3 — Clamped high (above max): When the preferred expression would exceed the max, the max value is used. Example: on a 4K screen,
4vw would be 76px —
exceeding a 3rem (48px) maximum — so it caps at 3rem.The min and max act as hard guardrails that prevent text from becoming unreadably small or distractingly large.
Xvw alone has two problems:1. Zero intercept:
4vw on a 320px screen is 12.8px
— well below readable size. You'd need a very large vw percentage to get a useful size on mobile, which
would then grow too fast on larger screens.2. Ignores user font preferences: A pure vw value doesn't respect the user's browser font size setting at all.
Adding a rem offset fixes both:
3vw + 0.5rem means the font starts
at 0.5rem (8px) at 0px viewport and grows at 3px per 100px of viewport. On a 320px screen: 9.6px + 8px =
17.6px — already a reasonable size. The rem component acts as a baseline that stays proportional to the
user's font preference.The formula gives you independent control over two things: the rate of growth (the vw coefficient) and the base size (the rem offset).
clamp(MIN, PREF, MAX) is exactly equivalent to:max(MIN, min(MAX, PREF))Reading from inside out:
1.
min(MAX, PREF) — takes the smaller of MAX and PREF. If PREF
exceeds MAX, it caps at MAX. Otherwise uses PREF.2.
max(MIN, result) — takes the larger of MIN and the result from
step 1. If the result is below MIN, it floors at MIN.So the final value is always between MIN and MAX, with PREF used in between.
You can also write it as
min(MAX, max(MIN, PREF)) — they're
identical.When to use each form:
—
clamp(): when you need both a floor and a ceiling — most fluid
type and spacing—
min(): when you only need a ceiling — e.g. width: min(100%, 1200px)—
max(): when you only need a floor — e.g. padding: max(1rem, 3vw)
clamp(1.875rem, 4vw + .5rem, 3.75rem) is verbose and hard to
remember. Writing it directly on every heading leads to copy-paste inconsistency and makes changes
difficult.Storing in custom properties solves this:
1. Single source of truth: change
--text-h1 in one
place and every h1 across the site updates.2. Readable code:
font-size: var(--text-h1) clearly
communicates intent; the long clamp expression does not.3. Design system alignment: token names match design tool names (Figma, etc.), making collaboration clearer.
4. Easy theming: override tokens in a specific context without touching the declarations:
.compact { --space-md: clamp(.5rem, 1vw, 1rem); } —
all children using var(--space-md) in that context get the compact
version.Custom property token systems are the foundation of every modern CSS architecture — from Bootstrap to Tailwind to Material Design all use this pattern.
clamp() works for any CSS property that accepts numeric length
values. It is not limited to typography:Spacing:
padding: clamp(1rem, 3vw, 2rem) — fluid
card paddingGap:
gap: clamp(1rem, 3vw, 2rem) — fluid grid
gapWidth:
width: clamp(200px, 50vw, 600px) — fluid
column widthBorder radius:
border-radius: clamp(.5rem, 2vw, 1.5rem) — proportional roundingMargin:
margin-bottom: clamp(1.5rem, 4vw, 3rem) —
fluid section spacingThe pattern is particularly powerful for fluid spacing systems where you want the breathing room around components to scale proportionally with the screen — not have tight padding on large screens or excessive padding on small ones.
The key is that the vw component must make sense for the property — viewport-width-relative values are most natural for anything that should scale with the container.
clamp(1rem, 5vw, 2rem) evaluate to? (assume 1rem = 16px)clamp(1rem, 3vw, 3rem)?clamp(1rem, 3vw + 0.5rem, 3rem) better
than clamp(1rem, 3vw, 3rem)?- clamp(min, preferred, max): returns preferred value, clamped between min and max. Three zones: clamp-low, fluid, clamp-high.
- Preferred expression: usually
Xvw + Yrem— vw provides viewport-relative growth, rem provides an accessible base size that scales with user font preferences. - Math equivalence:
clamp(A, B, C)=max(A, min(C, B)). Usemin()for ceiling-only,max()for floor-only. - Fluid type scale: one
clamp()per heading level, no@mediafor font sizes. Smooth, continuous scaling at every viewport width. - Fluid spacing:
clamp()works onpadding,gap,margin,border-radius— any numeric property. - Design token system: store all clamp values in custom properties on
:root. Usevar(--text-h1)throughout. Single source of truth — change in one place, updates everywhere. - Formula: slope = (maxSize − minSize) ÷ (maxVW − minVW). Intercept = minSize − slope × minVW. Round and pair with min/max bounds.
- Tools: utopia.fyi generates complete fluid type and spacing scales. Most developers tune by feel using the formula as a starting point.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.