Sub topic 6
clamp(): a value with a floor and a ceiling
This chapter assumes you know basic CSS syntax and the cascade, the common length units (px, rem, em, %, vw), the box model, and how min() and max() pick one value from a list. If min() as an upper bound and max() as a lower bound are still unclear, review those chapters first.
What clamp() does
clamp() takes three values and returns one:
clamp(MIN, PREFERRED, MAX)MINis the floor. The result is never smaller than this.PREFERREDis the value the browser uses when it falls between the floor and the ceiling.MAXis the ceiling. The result is never larger than this.
The definition is equivalent to nesting the two functions you already know:
clamp(MIN, PREFERRED, MAX) = max(MIN, min(PREFERRED, MAX))Read from the inside out. min(PREFERRED, MAX) caps the preferred value at the ceiling. max(MIN, ...) then raises that result to the floor if it fell below it. clamp() is shorthand for this pattern, and it is easier to read because the three roles appear in the order you think about them: low, target, high.
h1 {
font-size: clamp(1.5rem, 1rem + 2vw, 3rem);
}The font size grows with the viewport through the preferred value 1rem + 2vw. It never drops below 1.5rem and never exceeds 3rem.
How the browser resolves it
The browser computes each argument to a length, then picks one. Take the heading above with a root font size of 16px.
| Viewport width | Preferred (`1rem + 2vw`) | Result |
|---|---|---|
| 320px | 16px + 6.4px = 22.4px | 24px (floor, `1.5rem`) |
| 600px | 16px + 12px = 28px | 28px (preferred) |
| 1200px | 16px + 24px = 40px | 40px (preferred) |
| 1600px | 16px + 32px = 48px | 48px (ceiling, `3rem`) |
| 2000px | 16px + 40px = 56px | 48px (ceiling, `3rem`) |
The result tracks the viewport in the middle range and flattens at both ends. Those flat regions are the point of clamp(). Without them, a pure vw size would shrink to unreadable text on a phone and grow absurdly on a large monitor.
Syntax rules
The rules are the same as for min() and max().
- Separate the three arguments with commas.
- Each argument can be a length, a percentage, a number (where the property accepts one), or a math expression.
- You can write math directly inside an argument without
calc().1rem + 2vwis valid as it stands. - Inside an argument,
+and-need whitespace on both sides.1rem+2vwis invalid, and1rem + -2vwis easy to misread, so write1rem - 2vw. - Multiplication and division do not need whitespace, but at least one side of
*must be a number, and the right side of/must be a number. - All three arguments must resolve to compatible types. You cannot mix a length with a unitless number.
Choosing the three values
Most of the work is deciding what goes in each slot.
The minimum
Pick the smallest value that is still usable. For body text that means a size people can read on a small phone, commonly 1rem. Use rem so the floor respects the user's font-size preference.
The maximum
Pick the largest value that still looks right. For text, a ceiling stops lines from growing into oversized headings on wide screens. For widths, the ceiling is often the width of your reading column.
The preferred value
The preferred value is where the fluid behavior lives. It usually contains a viewport unit such as vw or a percentage, often combined with a rem term:
.title {
font-size: clamp(1rem, 0.5rem + 3vw, 2rem);
}The rem term gives the size a fixed anchor that scales with the user's font settings. The vw term supplies the growth. Section "Keeping the preferred value accessible" below explains why this matters.
Using clamp() beyond font sizes
clamp() works anywhere a length, percentage, or number is accepted.
section {
padding-block: clamp(1rem, 4vw, 3rem);
}
.hero {
height: clamp(20rem, 50vh, 40rem);
}
.sidebar {
width: clamp(12rem, 25%, 20rem);
}The sidebar is a quarter of its container, but never narrower than 12rem and never wider than 20rem. A percentage is resolved against the property's usual reference. For width that is the containing block's width.
Edge cases
MIN greater than MAX
If the floor is larger than the ceiling, the floor wins. The specification defines clamp() as max(MIN, min(PREFERRED, MAX)), so the outer max() always returns at least MIN.
.broken {
/* MIN (3rem) is larger than MAX (2rem), so the result is always 3rem */
font-size: clamp(3rem, 2vw, 2rem);
}This is almost always a typo. If a fluid value seems stuck, check the order of the arguments first.
A preferred value that never leaves the range
If the preferred value is always between the floor and the ceiling, clamp() adds nothing. Likewise, if it is always below the floor or above the ceiling, the result is a constant. The viewport-based term needs a slope that crosses the range inside the viewport widths you care about.
Omitting a bound
clamp() accepts the keyword none in the spec for MIN or MAX in some drafts, but browsers do not reliably support it. When you only need one bound, use min() or max() instead. That is simpler and works everywhere.
Invalid values
If any argument is invalid, the whole declaration is invalid and the browser ignores it. The property then falls back to an earlier declaration or its inherited or initial value. A common cause is missing whitespace around +.
.card {
font-size: 1rem;
/* Invalid: no spaces around +, so this line is dropped and 1rem applies */
font-size: clamp(1rem, 1rem+2vw, 2rem);
}Percentages in font-size
In font-size, a percentage refers to the parent element's font size, not the viewport or the container width. clamp(1rem, 100%, 2rem) therefore means "the inherited size, held between 1rem and 2rem", which is rarely what you want for fluid type.
Keeping the preferred value accessible
A preferred value built only from vw ignores the user's font-size setting, and browser zoom may not increase the text as much as users expect. Compare these two:
/* Harder to zoom: the size depends only on the viewport */
.title-viewport-only {
font-size: clamp(1rem, 4vw, 2rem);
}
/* Better: the rem term responds to zoom and user font-size settings */
.title {
font-size: clamp(1rem, 0.5rem + 3vw, 2rem);
}When a user zooms a page, the viewport in CSS pixels gets smaller, so a vw-based size can actually shrink or stay the same instead of growing. Adding a rem term makes the result respond to the root font size. A good habit is to test the page at 200% zoom and confirm the text is visibly larger than at 100%.
The floor and ceiling should also be in rem, not px, so that a user who raised their default font size also raises the limits.
Custom properties for reuse
Because clamp() expressions are long, store them once:
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.25rem);
--step-1: clamp(1.25rem, 1.1rem + 0.75vw, 1.75rem);
}
body { font-size: var(--step-0); }
h2 { font-size: var(--step-1); }A custom property holds the expression as text. It is resolved where it is used, so vw and rem are evaluated in the context of the element that uses the variable.
Common mistakes
- Swapping the arguments, which makes the floor and ceiling collide.
- Writing a preferred value in
vwonly, which hurts zoom behavior. - Using
pxfor the limits, which ignores user font settings. - Forgetting spaces around
+and-. - Expecting a percentage inside
font-sizeto track the viewport.
Exercise
A heading uses font-size: clamp(1.25rem, 0.5rem + 4vw, 2.5rem) with a 16px root font size. What size does it get at a 400px viewport, and at a 1000px viewport? Explain which argument decides each result.
Show answer
At 400px, the preferred value is 8px + 16px = 24px. The floor is 1.25rem = 20px and the ceiling is 2.5rem = 40px, so 24px lies between them and the preferred value is used: 24px. At 1000px, the preferred value is 8px + 40px = 48px, which is above the ceiling of 40px, so the ceiling decides and the result is 40px.
Exercise
Rewrite clamp(2rem, 5vw, 4rem) using only min() and max(), then say what happens if the arguments are written as clamp(4rem, 5vw, 2rem).
Show answer
clamp(2rem, 5vw, 4rem) is max(2rem, min(5vw, 4rem)). With clamp(4rem, 5vw, 2rem), the floor is larger than the ceiling. The expression is max(4rem, min(5vw, 2rem)), and because min(5vw, 2rem) can never exceed 2rem, the outer max() always returns 4rem. The result is a constant 4rem.