Sub topic 6
clamp(): a value with a floor and a ceiling
This chapter assumes you know basic CSS syntax, the common length units (px, rem, %, vw), and how min() and max() pick a value. Here you combine them into clamp().
What clamp() does
clamp() takes three values: a minimum, a preferred value, and a maximum.
h1 {
/* clamp(MIN, PREFERRED, MAX) is equivalent to max(MIN, min(PREFERRED, MAX)) */
font-size: clamp(1.5rem, 1rem + 2vw, 3rem);
}The browser resolves it in this order:
- It computes the preferred value. Here that is
1rem + 2vw, which changes as the viewport width changes. - If the preferred value is below the minimum, the minimum wins.
- If the preferred value is above the maximum, the maximum wins.
- Otherwise the preferred value is used.
The result is a single length. The font size grows with the viewport but never drops below 1.5rem or exceeds 3rem.
Why it equals max(min())
min(PREFERRED, MAX) caps the preferred value at the ceiling. max(MIN, ...) then raises that result to the floor. Together they give the same answer as clamp(). You could write the nested form, but clamp() is shorter and states your intent more clearly.
| Viewport width | Preferred (`1rem + 2vw`) | Result of `clamp(1.5rem, 1rem + 2vw, 3rem)` |
|---|---|---|
| 320px (16px root) | 16 + 6.4 = 22.4px | 24px (minimum) |
| 800px | 16 + 16 = 32px | 32px (preferred) |
| 1600px | 16 + 32 = 48px | 48px (preferred, equals the maximum) |
| 2000px | 16 + 40 = 56px | 48px (maximum) |
Where you can use it
You can use clamp() anywhere CSS accepts a length, such as font-size, padding, margin, gap, and width. Each of the three arguments can be a plain value or a math expression, and you can mix units. Inside clamp() you don't need calc() for arithmetic.
section {
padding-block: clamp(1rem, 4vw, 3rem);
}The vertical padding follows 4vw between 1rem and 3rem. On a phone it stays at 1rem, and on a wide desktop it stops at 3rem.
Common errors
- Wrong argument order. The order is minimum, preferred, maximum. If you write
clamp(3rem, 2vw, 1.5rem), the minimum is larger than the maximum. The minimum wins, so the value is always3rem. - A preferred value with no relative part.
clamp(1rem, 2rem, 3rem)always gives2rem. The middle value needs a unit that changes, such asvwor%, or it needs to contain one. - A viewport-only preferred value.
clamp(1rem, 4vw, 2rem)ignores the user's font-size preference. Adding aremterm, as in0.5rem + 3vw, lets the text respond to zoom and browser settings. - Missing operator spaces. Inside math,
+and-need spaces on both sides.1rem+2vwis invalid.
Try it yourself
Create a page with a heading and this rule. Resize the window and watch the heading grow, then stop at both limits.
h1 {
font-size: clamp(1.5rem, 1rem + 2vw, 3rem);
}Open your browser's developer tools and select the h1. The computed font-size shows the single resolved value, which is the easiest way to see which of the three arguments is currently winning.
Exercise
What does width: clamp(20rem, 50%, 40rem) resolve to when the parent is 600px wide, assuming a 16px root font size?
Show answer
The minimum is 320px, the preferred value is 50% of 600px = 300px, and the maximum is 640px. The preferred value is below the minimum, so the width is 320px.
Exercise
Rewrite clamp(1rem, 3vw, 2rem) using only min() and max().
Show answer
max(1rem, min(3vw, 2rem)). The inner min() caps 3vw at 2rem, and the outer max() raises the result to at least 1rem.