Fluid Typography in CSS with clamp()
Fluid typography without media queries
The old way used several media queries with different font-sizes. clamp() scales smoothly between a min and max.
The old hack vs the modern fix
View demoh1 {
font-size: 1rem;
}
@media (min-width: 600px) {
h1 {
font-size: 1.5rem;
}
}
@media (min-width: 900px) {
h1 {
font-size: 2rem;
}
}h1 {
font-size: clamp(1rem, 0.5rem + 2.5vw, 2rem);
}<!-- Arbitrary value: clamp() works in any sizing utility --><h1 class="text-[clamp(1rem,0.5rem_+_2.5vw,2rem)]">Fluid heading</h1><!-- Also works for spacing --><section class="px-[clamp(1rem,5vw,4rem)]">Fluid padding</section>Min max clamp browser support
Live demo
Why the modern way wins
No breakpoint ladder
One declaration. Size scales with viewport instead of jumping at breakpoints.
Smooth scaling
clamp(min, preferred, max) keeps size between bounds. No sudden jumps.
Any unit
Use rem, em, vw, or a mix. Same pattern for line-height or spacing.
- Lines Saved
- 10 → 2
- 80% less code
- Old Approach
- Multiple @media
- Stepwise font sizes
- Modern Approach
- One clamp()
- Fluid between min and max
How it works
The old approach was a base font-size plus several media queries: at 600px switch to 1.5rem, at 900px to 2rem, and so on. Size jumped at each breakpoint and you had to maintain the ladder.
clamp(1rem, 0.5rem + 2.5vw, 2rem) means: never smaller than 1rem, never larger than 2rem, and in between use 0.5rem + 2.5vw. The rem part keeps the text growing when someone zooms the page. A plain vw value doesn't. One rule, smooth scaling, no media queries.