Sub topic 9
Fluid sizing with clamp(), min(), and max()
Fixed sizes break at the extremes. A 3rem heading fits a desktop but wraps badly on a phone, and a heading sized only in vw can become unreadably small. This chapter covers three CSS math functions that let a size grow with the viewport while staying inside limits you choose.
This chapter assumes you know the units from the previous chapters (px, rem, em, %, vw) and can write basic rules with selectors. You should also know that 1rem follows the root font size, which is 16px by default.
The three functions
Each function takes comma-separated values and returns one length. The browser evaluates them when it computes the layout, so they react to viewport size, font settings, and parent size.
| Function | Returns | Typical use |
|---|---|---|
| `min(a, b, ...)` | The smallest value | An upper limit: "never wider than" |
| `max(a, b, ...)` | The largest value | A lower limit: "never smaller than" |
| `clamp(low, preferred, high)` | `preferred`, held between `low` and `high` | A fluid size with both limits |
The names can mislead. min() sets a ceiling, because the result can never exceed its smallest argument. max() sets a floor, because the result can never fall below its largest argument.
.container {
/* at most 60rem wide; on narrow screens, the full width */
width: min(100%, 60rem);
}
.tap-target {
/* at least 2.75rem tall, taller if the content needs it */
min-height: max(2.75rem, 6vh);
}If you read min(100%, 60rem) as "the minimum width is 60rem", it looks backwards. Read it as "pick whichever is smaller". On a 400px screen, 100% is smaller, so it wins. On a 1600px screen, 60rem (960px) is smaller, so it wins.
clamp() as min() inside max()
clamp(low, preferred, high) is shorthand for max(low, min(preferred, high)). The browser first caps the preferred value at high, then raises the result to at least low.
The heading from this tutorial's examples uses it like this:
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}The size stays at 1.75rem (28px) until the middle expression grows past it. It then follows 1rem + 3vw and stops at 3rem (48px). With a 16px root, 1rem + 3vw reaches 28px at a viewport of 400px and 48px at about 1067px.
| Viewport width | `1rem + 3vw` | Result of the clamp |
|---|---|---|
| 320px | 16 + 9.6 = 25.6px | 28px (floor applies) |
| 600px | 16 + 18 = 34px | 34px (fluid range) |
| 1000px | 16 + 30 = 46px | 46px (fluid range) |
| 1400px | 16 + 42 = 58px | 48px (ceiling applies) |
Math inside the functions
Inside min(), max(), and clamp() you can write arithmetic directly, without a calc() wrapper. You can mix units freely, which is the main reason these functions are useful. You can also nest them, and you can use calc() inside them if you prefer.
.page {
width: min(100% - 2rem, 65rem);
margin-inline: auto;
}This gives a centered column that leaves 1rem of space on each side on small screens and stops at 65rem on large ones.
The rules for operators are strict:
+and-need spaces on both sides.100%-2remis invalid, because the parser reads-2remas a negative number.*and/do not need spaces, but spaces keep the code readable.- You can multiply or divide only if at least one side is a plain number.
3vw * 2works, but3vw * 2remdoes not. - A bare number such as
10is not a length.clamp(1rem, 10, 3rem)is invalid.
Why mix rem with vw
A size written only in vw, such as font-size: 4vw, ignores the user's font settings. Browser zoom and text-only zoom scale rem and em, but vw depends on the window width alone. Text sized only in vw may not grow when a user zooms in, which can fail WCAG 1.4.4 (Resize Text).
Adding a rem part fixes this:
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}The 1rem term scales with the user's setting. The limits are also in rem, so zooming the page scales the floor and ceiling too. Zooming does not change the viewport width in CSS pixels in a way that holds the size down, because zooming in actually shrinks the viewport measured in CSS pixels. At 200% zoom a 1280px window behaves like a 640px one, so the vw part shrinks while the rem limits grow. The text ends up larger than it was before, which is what users who zoom expect.
Deriving the preferred value
Guessing 3vw works, but you can compute the expression so the size hits exact values at exact viewport widths. Pick a size range and a viewport range. Then use a straight line: size = intercept + slope * viewport.
Say the font should be 20px at a 400px viewport and 32px at a 1200px viewport.
- Slope: (32 − 20) / (1200 − 400) = 0.015. As a percentage of viewport width, that is
1.5vw. - Intercept: 20 − 0.015 × 400 = 14px, which is
0.875rem. - Limits: 20px is
1.25remand 32px is2rem.
:root {
--step-1: clamp(1.25rem, 0.875rem + 1.5vw, 2rem);
}
h2 {
font-size: var(--step-1);
}Check it: at 400px the middle term is 14 + 6 = 20px, and at 1200px it is 14 + 18 = 32px. Both match the plan. Storing the result in a custom property keeps one definition for every element that needs that step.
Fluid values beyond font size
Any length property accepts these functions. Spacing that scales with the screen avoids a cramped phone layout and an empty desktop one:
.section {
padding-block: clamp(2rem, 6vw, 5rem);
padding-inline: clamp(1rem, 4vw, 3rem);
}
.hero img {
width: min(100%, 40rem);
height: auto;
}Percentages inside these functions resolve against the same reference as they would on their own. In width, 100% means the containing block's width. In font-size, % means the parent's font size, so clamp(1rem, 100%, 2rem) is rarely what you want.
Common mistakes and edge cases
- Swapped limits.
clamp(3rem, 2vw, 1.75rem)has a low limit above the high one. In that case the low limit wins, so the result is always 3rem. Keep the order low, preferred, high. - Using max() when you mean min().
width: max(100%, 60rem)never gets narrower than 60rem and causes horizontal scrolling on phones. For a ceiling, usemin(). - A preferred value that never changes.
clamp(1rem, 2rem, 3rem)has no viewport-dependent part, so it is always 2rem. Include avw,%, or similar term. - Extreme slopes. A steep
vwcoefficient makes sizes jump quickly between the limits. Check the sizes at the narrowest and widest viewports you support. - Very wide screens. Without a ceiling, a
vw-based size keeps growing on ultrawide monitors. The high limit exists to prevent this. - Browser support. All current browsers support these functions. If you must support very old browsers, write a plain fallback declaration before the
clamp()one, since browsers skip declarations they cannot parse.
h1 {
font-size: 2rem;
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}Choosing between the three
Use clamp() when you want one value that moves between two limits, as with headings, spacing, and gutters. Use min() when you only need a ceiling, as with content widths and image sizes. Use max() when you only need a floor, as with tap targets and minimum padding. For layouts that change shape, as opposed to just scaling, media queries or container queries are the better tool.
Exercise
A paragraph should be 1rem at a 320px viewport and 1.25rem at an 1120px viewport (assume 16px = 1rem). Write the clamp() expression, giving the slope in vw and the intercept in rem.
Show answer
Sizes are 16px and 20px. Slope = (20 − 16) / (1120 − 320) = 0.005, which is 0.5vw. Intercept = 16 − 0.005 × 320 = 14.4px = 0.9rem. So: font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem). Check: at 320px, 14.4 + 1.6 = 16px. At 1120px, 14.4 + 5.6 = 20px.
Exercise
What is wrong with width: min(100%-2rem, 65rem), and what does the corrected version do on a 500px screen?
Show answer
There are no spaces around the minus sign, so the declaration is invalid and the browser ignores it. The corrected form is width: min(100% - 2rem, 65rem). On a 500px screen the container is 500px, so 100% - 2rem is 468px, which is smaller than 65rem (1040px). The width is 468px, leaving 1rem on each side.
Exercise
Why is font-size: 5vw a weaker choice for body text than clamp(1rem, 0.9rem + 0.5vw, 1.25rem)?
Show answer
5vw depends only on the window width, so it ignores the user's font-size setting, can become too small on narrow screens, and has no ceiling on wide ones. The clamp() version has a rem-based floor and ceiling, includes a rem term that follows user settings, and only varies within a small range.