Entry 05
Fluid sizes with clamp()
Use it for anything that should grow with the screen, starting with font sizes and section spacing. One declaration replaces a ladder of breakpoint overrides.
Code
:root {
--text-body: clamp(1rem, 0.75rem + 1vw, 1.75rem);
--space-section: clamp(3rem, 1rem + 6vw, 8rem);
}Why it works, the rules and the credits
Why it works
clamp(min, preferred, max)uses the preferred value and stops at the two bounds.- The
vwpart of the preferred value grows with the viewport. Therempart sets where the growth starts. --text-bodyabove is 16px up to a 400px viewport, 28px from 1600px, and a straight line between.
Rules
- Write the preferred value as
rem + vw, nevervwalone.clamp(1rem, 1vw, 1.75rem)stays at 16px on every screen narrower than 1600px, because 1vw is smaller than 1rem until then. A barevwvalue also ignores the reader's font size setting. - Work the preferred value out from two points. The slope is
(max - min) / (wide - narrow), in px over px, and times 100 it is thevwnumber. Therempart ismin - slope × narrow. - Keep both bounds in
remso they follow the reader's font size. - For a font size, keep the maximum at or below 2.5 times the minimum. The
vwpart does not grow when the reader zooms, so fluid text grows less than the page. Past 2.5 times, 500% zoom cannot double the text at some widths, and that fails WCAG 1.4.4. - Put fluid values in tokens. Components use the token and never repeat the math.
- For more than two or three sizes, derive a whole scale as One fluid scale for type and space does.
Credits
Borrowed from: Kyrylo Silin, @kyrylo on X, 2026-05-10 · James Gilyead and Trys Mudford, Utopia · Adrian Roselli, Responsive Type and Zoom · Maxwell Barvian, Addressing Accessibility Concerns With Using Fluid Type, 2023-11-07, for the 2.5 limit
Docs: MDN: clamp() · Adrian Bece, Modern Fluid Typography Using CSS Clamp