Fluid sizing with clamp()
Overview
With media queries, sizes jump: the heading is 2rem until 64rem, then suddenly 3rem. Fluid sizing makes values scale smoothly between a minimum and a maximum as the viewport grows — no breakpoints, no jumps.
The tool is clamp(), along with its siblings min() and max().
min(), max(), clamp()
These maths functions work anywhere a length is allowed:
| Function | Result | Example |
|---|---|---|
min(a, b) |
the smaller value | min(60ch, 100%) — 60ch, or full width if that’s narrower |
max(a, b) |
the larger value | max(50vh, 20rem) — half the screen, but at least 20rem |
clamp(min, val, max) |
val, limited to the range |
clamp(1rem, 4vw, 2rem) |
clamp(min, val, max) is the same as max(min, min(val, max)). You can mix units freely, and do arithmetic inside without writing calc().
Fluid type
h1 { font-size: clamp(1.5rem, 1rem + 2.5vw, 3rem); }- On a 320px phone:
1rem + 2.5vw= 16 + 8 = 24px — exactly the minimum. - On a 800px tablet: 16 + 20 = 36px.
- On a 1280px laptop: 16 + 32 = 48px — the maximum, which it stays at from then on.
The heading grows steadily with the screen, with no media queries.
Why “rem + vw” and not just vw?
font-size: 5vw scales with the viewport — but vw doesn’t change when people zoom. Browser zoom works by making the viewport narrower in CSS pixels, so on a wide screen a pure-vw heading can stay the same size, or even get smaller, when someone zooms to 200%. That fails WCAG’s requirement that text can be resized to 200%.
Adding a rem component keeps part of the size tied to the user’s font settings, so zooming always makes text bigger. The rem minimum and maximum in clamp() help too. A good rule: keep the vw part modest and always include rem.
Calculating the preferred value
To go from size A at viewport width X to size B at viewport width Y, the preferred value is a straight line:
slope = (B − A) / (Y − X)
intercept = A − slope × X
preferred = intercept + (slope × 100)vwExample: 28px at 320px and 56px at 1300px:
- slope = (56 − 28) / (1300 − 320) = 0.02857 → 2.857vw
- intercept = 28 − 0.02857 × 320 = 18.86px = 1.18rem
font-size: clamp(1.75rem, 1.18rem + 2.86vw, 3.5rem);You don’t need to do this by hand: fluid type calculators (search for “Utopia fluid type”) generate a whole type scale for you. What matters is understanding the shape — a minimum, a linear ramp and a maximum.
Fluid spacing
The same idea works for padding, margins and gaps, so a layout breathes more on big screens without breakpoints:
:root {
--space-s: clamp(0.75rem, 0.6rem + 0.75vw, 1.25rem);
--space-m: clamp(1rem, 0.75rem + 1.25vw, 2rem);
--space-l: clamp(1.5rem, 1rem + 2.5vw, 4rem);
}
.section { padding-block: var(--space-l); }
.grid { gap: var(--space-m); }Defining a few fluid spacing tokens and using them everywhere gives a whole site consistent, scaling rhythm.
Fluid widths with min()
min() is the cleanest way to write “this wide, but never wider than the container”:
.prose { inline-size: min(65ch, 100%); margin-inline: auto; }
.modal { inline-size: min(32rem, 100% - 2rem); } /* keep a 1rem gap on each side */
.cards { grid-template-columns: repeat(auto-fit, minmax(min(15rem, 100%), 1fr)); }The second line shows arithmetic directly inside min() — no calc() needed.
Fluid heights with max()
.hero { min-block-size: max(60svh, 24rem); }At least 60% of the small viewport height, but never less than 24rem — so it doesn’t collapse to a sliver on a phone in landscape.
Container-relative units
If an element should scale with its container rather than the viewport, use container query units (cqi = 1% of the container’s inline size), covered in Container queries:
.card-title { font-size: clamp(1rem, 0.75rem + 2cqi, 1.5rem); }Rule of thumb:
clamp(rem, rem + vw, rem)for type and spacing;min(length, 100%)for widths. Together they replace most of the breakpoints you’d otherwise write.
A fluid hero
Make .hero h1 fluid: 1.75rem minimum, 3.5rem maximum, growing with the viewport in between (use a rem + vw preferred value so it still responds to zoom). Make the section’s padding fluid between 1.5rem and 5rem, and give .hero p a readable width of at most 60ch using min() so it never overflows on small screens. Test that the heading is 28px at 320px wide and 56px at 1400px wide.
<section class="hero">
<h1>Layouts that work everywhere</h1>
<p>Learn modern CSS layout, from the box model to container queries, with real projects.</p>
</section>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.hero { background: #eaf0fc; }
.hero h1 { margin-top: 0; line-height: 1.1; }
/* your code here */
</style><section class="hero">
<h1>Layouts that work everywhere</h1>
<p>Learn modern CSS layout, from the box model to container queries, with real projects.</p>
</section>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.hero { background: #eaf0fc; }
.hero h1 { margin-top: 0; line-height: 1.1; }
.hero {
padding: clamp(1.5rem, 5vw, 5rem);
}
.hero h1 {
/* 28px at 320px wide → 56px from 1300px wide (see the maths below) */
font-size: clamp(1.75rem, 1.18rem + 2.86vw, 3.5rem);
}
.hero p {
inline-size: min(60ch, 100%);
}
</style>Summary
clamp(min, preferred, max)uses the preferred value but never goes below min or above max.- A preferred value of
rem + vwgrows with the viewport and still responds to browser zoom and font size settings. - Pure
vwfont sizes don’t grow when people zoom — always mix in a rem component. min()andmax()are great for widths:min(60ch, 100%),max(50vh, 20rem).- Fluid values replace many breakpoints: type, spacing and gaps scale smoothly across sizes.