Fluid Layouts and Fluid Typography With clamp()
Sizes that scale smoothly instead of jumping at breakpoints: min(), max() and clamp() for widths, spacing and text, calculating a fluid type formula from two sizes, keeping fluid text accessible when users zoom, fluid scales, and container units.
kvi5hnuCode Sprout team member7 min readOn this page
Breakpoints change a layout in steps: one size below 48em, another above. Many things look better if they change continuously instead: a heading that's 2rem on a phone and 3.5rem on a desktop, growing smoothly in between; padding that's tight on small screens and generous on large ones. That's fluid design, and the CSS comparison functions min(), max() and clamp() (article 26) make it possible without any media queries.
min(), max() and clamp() for layout
.container { inline-size: min(100% - 2rem, 60rem); margin-inline: auto; } /* fluid, capped, with a gutter */
.section { padding-block: max(2rem, 5vw); } /* at least 2rem */
.sidebar { inline-size: clamp(12rem, 25%, 20rem); } /* between two limits */
Tested in Chrome, with body { margin: 0 }: the container is 288px wide in a 320px viewport (the full width minus 2rem), 688px in 720px, and stops at 960px (60rem) from 1200px up. The section's padding is 32px at 320px (the 2rem minimum, because 5vw is only 16px there), 36px at 720px (5vw) and 80px at 1600px.
min(a, b)uses the smaller value: good for "fluid, but no bigger than".max(a, b)uses the larger: "fluid, but no smaller than".clamp(min, preferred, max)keeps the preferred value between two limits: it'smax(min, min(preferred, max)).
min(100% - 2rem, 60rem) replaces the classic max-width: 60rem; width: 100%; padding: 0 1rem with one line, and works for any element.
Fluid typography
A fluid font size uses clamp() with a preferred value that mixes a fixed part and a viewport part:
h1 { font-size: clamp(1.25rem, 0.75rem + 2vw, 2.25rem); }

Tested in Chrome: 20px at 320px and 400px, 26.4px at 720px, 36px at 1200px and above.
Calculating the formula
Pick a size at a small viewport and a size at a large one. For 20px at 400px and 36px at 1200px:
- Slope: the size changes by 36 − 20 = 16px over 1200 − 400 = 800px of width, so 0.02px per pixel, which is 2vw (1vw is 1% of the width).
- Intercept: at 400px, 2vw is 8px, so the fixed part is 20 − 8 = 12px = 0.75rem.
- Clamp it between the two sizes:
clamp(1.25rem, 0.75rem + 2vw, 2.25rem).
In general: slope = (max size − min size) / (max width − min width), multiplied by 100 to get vw; intercept = min size − slope × min width. Online "fluid type calculators" do the same arithmetic.
Keeping it accessible
Text sized only with viewport units has a serious problem: it doesn't respond to the user's text settings. Browser zoom on a desktop makes each CSS pixel bigger but the viewport narrower in CSS pixels by the same factor, so pure-vw text stays the same size on screen as the user zooms, and a larger default font size has no effect on vw at all. That can fail WCAG 1.4.4 (Resize Text), which requires text to be resizable to 200%.
The rules that keep fluid type safe:
- Always include a
rempart in the preferred value (0.75rem + 2vw, not4vw), and useremfor the minimum and maximum, so the user's font size still counts. - Keep the range modest. If the maximum is more than about 2.5 times the minimum, zooming to 200% may not double the text. Big ranges are fine for display headings, not for body text.
- Leave body text mostly fixed, at
1remor a small fluid range likeclamp(1rem, 0.95rem + 0.25vw, 1.125rem). - Test by zooming to 200% and by changing the browser's default font size.
A fluid type and spacing scale
Fluid values work best as a small set of tokens in custom properties (article 27), used everywhere:
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem); /* body */
--step-1: clamp(1.2rem, 1.07rem + 0.65vw, 1.5rem);
--step-2: clamp(1.44rem, 1.2rem + 1.2vw, 2rem);
--step-3: clamp(1.73rem, 1.3rem + 2.1vw, 2.75rem); /* h1 */
--space-s: clamp(0.75rem, 0.65rem + 0.5vw, 1rem);
--space-m: clamp(1rem, 0.8rem + 1vw, 1.5rem);
--space-l: clamp(1.5rem, 1rem + 2.5vw, 3rem);
}
body { font-size: var(--step-0); }
h1 { font-size: var(--step-3); }
.section { padding-block: var(--space-l); }
.stack > * + * { margin-block-start: var(--space-m); }
Each step is roughly a fixed ratio larger than the one below, on both small and large screens, so the hierarchy stays consistent.

The headings grow much more than the body text: between 360px and 1280px, h1 gains about 16px while body text gains only 2px (from 16.1px to the 18px maximum), so small screens get a tighter hierarchy and large ones more contrast.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.frame {
inline-size: var(--w, 20rem); box-sizing: border-box;
container-type: inline-size; /* cqi units below are relative to this frame */
/* the frame is the container, so cqi padding goes on a child: a container can't use its own cqi */
margin-top: 0.5rem; border: 2px dashed #d8cfc0; border-radius: 8px;
}
h1 { margin: 0 0 0.5rem; color: #8a3b12; line-height: 1.1;
font-size: clamp(1.1rem, 0.2rem + 7cqi, 2rem); }
.inner { padding: clamp(0.5rem, 4cqi, 1.5rem); }
p { margin: 0; font-size: clamp(0.8rem, 0.75rem + 0.6cqi, 0.95rem); }
The demo uses container query units: cqi is 1% of the container's inline size (article 78), so the type scales with the frame instead of the window. Drag the slider: the heading and the padding shrink smoothly, and the heading stops at its 1.1rem minimum near the narrow end. The padding is on an inner element, because cqi on the container itself would refer to the next container up, or the viewport.
Viewport units or container units?
| Unit | Relative to | Use |
|---|---|---|
vw, vi | the viewport | page-level type and spacing |
cqi, cqw | the nearest size container | components that appear at different widths, like a card in a sidebar and in the main column |
A heading in a narrow sidebar shouldn't be as large as one in a wide main column, even on the same screen. Container units give each component its own fluid scale. Without a container, they fall back to the small viewport units.
What's current
min(),max()andclamp()work in all browsers, in any property that takes a length.- Container query units (
cqiand the rest) are Baseline since 2023 (article 78). - Logical viewport units (
vi,vb) and the dynamic ones (dvh, article 25) work everywhere.
Common questions
What's the difference between clamp() and media queries for font size?
Media queries change the size in steps; clamp() changes it continuously between two limits.
Why does my fluid text not get bigger when I zoom?
It's sized only with vw. Add a rem part to the preferred value, and use rem limits.
How do I calculate the middle value?
Slope = (max − min) / (max width − min width) × 100, in vw; intercept = min − slope × min width, in rem.
Should body text be fluid? Only a little, if at all. Large ranges are for headings.
Summary
min(),max()andclamp()give fluid sizes with limits, without breakpoints.inline-size: min(100% - 2rem, 60rem)is a one-line fluid container.- Fluid type:
clamp(min, intercept + slope·vw, max), calculated from two sizes at two widths. - Always include
remin fluid type, keep ranges modest, and test zoom and default font size, for WCAG 1.4.4. - Use container units (
cqi) for components and viewport units for the page.
Practice questions
Write a font-size that is 1.5rem at a 320px viewport and 3rem at 1280px, fluid in between.
Answer
The slope is (48 − 24) / (1280 − 320) = 0.025, so 2.5vw. At 320px that's 8px, so the intercept is 24 − 8 = 16px = 1rem.
h1 { font-size: clamp(1.5rem, 1rem + 2.5vw, 3rem); }
How wide is this element in a 500px viewport, and in a 1500px one?
.c { inline-size: min(100% - 3rem, 50rem); }
(Assume the element is a direct child of body, with body { margin: 0 }.)
Answer
At 500px: 500 − 48 = 452px. At 1500px: 1452px is larger than 50rem, so 800px.
Why is font-size: 5vw an accessibility problem?
Answer
It ignores the user's font size setting, and desktop zoom narrows the viewport in CSS pixels, so the text stays the same size on screen when the user zooms. Use clamp() with a rem part and rem limits.
When should a component use cqi instead of vw?
Answer
When it appears in containers of different widths, like a sidebar and a main column. cqi follows the container's width, vw the whole viewport's.
Previous: Media Queries: Media Types, Range Syntax, and User-Preference Features
Next: Container Queries: Size Containers, Container Names and Container Units

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.