Most CSS files have a block like this somewhere: the heading is 2rem on desktop, then a media query drops it to 1.75rem on tablets, and another one drops it to 1.5rem on phones. It works, but the text jumps in steps, and every new size means another rule to maintain.
CSS clamp() lets one line of CSS do the job of all three. The font size grows smoothly with the screen, but never goes below a minimum or above a maximum.
What is clamp()?
clamp() takes three values: a minimum, a preferred value and a maximum.
font-size: clamp(1rem, 2.5vw, 2rem);
- 1rem is the smallest the text can be.
- 2.5vw is the preferred size. It is 2.5% of the viewport width, so it scales with the screen.
- 2rem is the largest the text can be.
The browser uses the preferred value while it stays between the minimum and maximum. Once it goes outside that range, the nearest limit wins.
The old way vs. the clamp() way
/* Desktop-first, three rules */
h1 { font-size: 2rem; }
@media (max-width: 1024px) { h1 { font-size: 1.75rem; } }
@media (max-width: 600px) { h1 { font-size: 1.5rem; } }
/* With clamp(), one rule */
h1 { font-size: clamp(1.5rem, 1.2rem + 1.5vw, 2rem); }
| Media queries | clamp() | |
|---|---|---|
| Size changes | In steps at breakpoints | Smoothly at every width |
| Lines of CSS | One rule per breakpoint | One line |
| Maintenance | Update every breakpoint | Update min, preferred or max |
Why not use vw alone?
A font size written only in vw units has a real accessibility problem: when a visitor zooms the page, the text may not grow with it. Mixing a rem amount into the preferred value, such as 1.2rem + 1.5vw, keeps the text responsive to zoom and to the user's browser font settings. Using rem for the minimum and maximum helps too.
Tip: always put a rem part in the preferred value, not just vw.
A simple formula for the preferred value
Say you want text that is 16px on a 320px screen and 24px on a 1200px screen.
- Slope = (24 - 16) / (1200 - 320) = 0.00909, which is 0.909vw.
- Intercept = 16 - (0.00909 x 320) = 13.09px, which is about 0.818rem (at a 16px root size).
- Put it together:
font-size: clamp(1rem, 0.818rem + 0.909vw, 1.5rem);
At 320px wide this gives 16px, at 1200px wide it gives 24px, and anywhere in between it scales evenly.
Copy-paste type scale
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.125rem); /* body */
--step-1: clamp(1.25rem, 1.1rem + 0.75vw, 1.625rem); /* h3 */
--step-2: clamp(1.5rem, 1.2rem + 1.5vw, 2.25rem); /* h2 */
--step-3: clamp(1.875rem, 1.3rem + 2.5vw, 3rem); /* h1 */
}
body { font-size: var(--step-0); }
h3 { font-size: var(--step-1); }
h2 { font-size: var(--step-2); }
h1 { font-size: var(--step-3); }
This is already mobile-first: the smallest value in each clamp() is the phone size, and larger screens scale up from there.
Beyond font size
clamp() works anywhere CSS accepts a length, so you can use it for spacing too:
.section { padding: clamp(1rem, 4vw, 3rem); }
.container { width: clamp(280px, 90%, 1100px); }
When you still need media queries
clamp() is great for sizes and spacing, but it does not replace layout changes. Switching from one column to three, hiding a menu or moving a sidebar still needs @media (min-width: ...). If your stylesheet is still written with max-width queries, you can convert it with the free ConvertSwift CSS converter, then use clamp() to remove the media queries that only existed to resize text.
Browser support
clamp() is supported in all current major browsers, so you can use it in production today. If you must support very old browsers, put a plain fallback line first:
h1 {
font-size: 1.75rem; /* fallback */
font-size: clamp(1.5rem, 1.2rem + 1.5vw, 2rem);
}
Quick recap
clamp(min, preferred, max)gives you smooth, limited scaling in one line.- Mix
remandvwin the preferred value so zoom still works. - Use the slope and intercept formula to hit exact sizes at two screen widths.
- Keep media queries for layout changes, and use
clamp()for sizing.
FAQ
Can I use clamp() with px units?
Yes, but rem is better for accessibility because it respects the user's font size settings.
Does clamp() replace min-width media queries?
No. It replaces the ones that only change sizes or spacing. Layout changes still need media queries.
Can I mix units inside clamp()?
Yes. You can mix rem, vw, px and percentages, and you can add them with calc()-style math directly, like 1.2rem + 1.5vw.

No comments:
Post a Comment