Sub topic 5
max(): setting a floor
What this chapter assumes
You should know basic CSS syntax and the cascade, and the common length units (px, rem, em, %, vw). You should also know the box model and the width property. The previous chapter covered min(), which caps a value. This chapter covers its mirror image, max(), which sets a minimum.
How max() resolves
max() takes two or more comma-separated values and resolves to the largest one. The browser compares them when it knows the real sizes of everything involved, such as the container width and the viewport size. The comparison is therefore live. If the container is resized, the winner can change without any media query.
.box {
min-width: max(50%, 20rem);
}Here the browser computes 50% of the container and 20rem in pixels. Whichever is larger becomes the min-width.
Keeping the direction straight
The names are easy to mix up. max() picks the larger value, so the result can never be smaller than any of its arguments. That makes it a floor, or lower bound. min() picks the smaller value, so the result can never be larger than any argument. That makes it a ceiling, or upper bound.
| Function | Picks | Acts as | Typical use |
|---|---|---|---|
| `min(a, b)` | the smaller value | upper bound (ceiling) | `width: min(100%, 40rem)` |
| `max(a, b)` | the larger value | lower bound (floor) | `padding-inline: max(1rem, 2vw)` |
A reliable memory aid is to ask what the result can never go below or above. With max(1rem, 2vw), the result is never below 1rem. The lower number protects you from the small end, and the other argument supplies the growth.
Worked example: a flexible minimum width
Consider this rule again.
.box {
/* Never narrower than 20rem, but grows to half of its container */
min-width: max(50%, 20rem);
}Assume a 16px root font size, so 20rem is 320px. The table shows what happens at three container widths.
| Container width | 50% of container | 20rem | Resolved min-width |
|---|---|---|---|
| 480px | 240px | 320px | 320px |
| 800px | 400px | 320px | 400px |
| 1200px | 600px | 320px | 600px |
In a narrow container the fixed floor wins. Once half of the container exceeds 320px, the percentage takes over. A plain min-width: 50% would let the box become tiny in a small container. A plain min-width: 20rem would never grow with the layout. max() gives you both behaviors in one declaration.
Math inside max()
Like min() and clamp(), each argument of max() is a calculation. You can write arithmetic directly, without a nested calc().
.sidebar {
width: max(100% - 30rem, 15rem);
}The rules from calc() still apply:
+and-need whitespace on both sides.100%-30remis invalid, because the parser reads-30remas a separate token.*and/do not need spaces, but at least one side of*must be a plain number, and the right side of/must be a number.- You cannot add a unitless number to a length.
max(1rem, 2)is invalid in a length property, because2is not a length.
If one argument is invalid, the whole declaration is invalid and the browser drops it. The property then falls back to an earlier declaration or its default value.
Mixing units
Arguments can use different units, which is where max() becomes useful. A floor in rem respects the user's font size. A growth term in vw or % follows the layout. The browser converts both to pixels before comparing.
section {
padding-block: clamp(1rem, 4vw, 3rem);
}
button {
padding-inline: max(1rem, 2vw);
}For button, a 400px viewport gives 2vw = 8px, so the 1rem floor (16px) wins. At 1200px, 2vw is 24px, which beats the floor. The padding never drops below 1rem, and it has no upper limit. If you want a ceiling too, that is the job of clamp(), covered in its own chapter.
You can also set a floor on font size.
.note {
font-size: max(1rem, 1.2vw);
}On a small screen the text stays at 1rem. On very wide screens it grows with the viewport, with no upper limit. Think about whether that is what you want. Unbounded growth is often a problem, and clamp() is usually the better tool for type.
A practical floor: safe-area insets
A well-known use of max() is protecting content from device notches and rounded corners. The env() function reads the safe-area inset set by the device. You want at least your normal gutter, and more if the device needs it.
body {
padding-inline: max(1rem, env(safe-area-inset-left));
}On a device with no inset, env() returns 0px, so the 1rem gutter wins. On a phone in landscape with a 44px inset, the inset wins. For env() to report real values on iOS Safari, the page also needs viewport-fit=cover in its viewport meta tag. Without it, the insets are 0 and the floor always wins, which is still safe.
Common errors
Using max() when you wanted a ceiling
Writing width: max(100%, 40rem) looks like "at most 40rem". It actually means "at least 40rem". On a 320px screen the width becomes 640px, which overflows the viewport and produces a horizontal scrollbar. For a ceiling, use min(100%, 40rem).
/* Wrong for a cap: overflows narrow containers */
.bad {
width: max(100%, 40rem);
}
/* Right: shrinks with the container, stops at 40rem */
.good {
width: min(100%, 40rem);
}Putting a floor on a width that should fit its container
A floor can overflow its parent on purpose. min-width: max(50%, 20rem) forces at least 320px even in a 250px container. That is sometimes acceptable, as in a scrollable table cell. For layout columns it is risky. If overflow is not acceptable, put a min() guard on the floor itself, such as max(50%, min(100%, 20rem)). Then the floor shrinks to the container width when the container is narrow.
Forgetting that percentages depend on context
A percentage inside max() resolves against whatever that property normally uses. For width and padding-inline that is the containing block's width. Even padding-block uses the width, not the height, which surprises many developers. For font-size it is the parent's font size. Check the property's definition before trusting a percentage.
Spacing around operators
max(1rem, 50%-2rem) is invalid, as described above. Linters often miss this, so test visually if a declaration seems to be ignored. In browser DevTools, an invalid declaration appears struck through in the Styles panel.
Nesting without need
max(1rem, max(2vw, 0.5rem)) works, but max(1rem, 2vw, 0.5rem) is equivalent and easier to read. Because max() accepts any number of arguments, flatten nested calls of the same function. Nesting is still useful when you mix functions, such as min() inside max().
Relationship to clamp()
clamp(MIN, PREFERRED, MAX) is defined as max(MIN, min(PREFERRED, MAX)). The inner min() applies the ceiling, and the outer max() applies the floor. Because the floor is applied last, it wins if the two bounds conflict. For example, clamp(3rem, 1vw, 2rem) resolves to 3rem, because the minimum is greater than the maximum. Knowing this makes the behavior of clamp() easier to predict, and it lets you write a floor-only version when you do not want a ceiling.
Exercise
A card has width: max(100%, 30rem) inside a 20rem-wide container (assume 16px per rem). What is the resolved width, and what would you change if you want the card never to exceed 30rem?
Show answer
The arguments are 100% of 20rem = 20rem and 30rem. max() picks the larger one, so the width is 30rem (480px), which overflows the 20rem container. To cap the card at 30rem, use min(): width: min(100%, 30rem). In the same 20rem container that resolves to 20rem.
Exercise
Write a rule that gives .panel horizontal padding of at least 1.5rem, growing at 3% of the container width when that is larger. Then state the padding in pixels when the container is 400px wide, with a 16px root font size.
Show answer
.panel { padding-inline: max(1.5rem, 3%); }. At 400px wide, 3% is 12px and 1.5rem is 24px. The larger value is 24px, so the floor wins and the padding is 24px on each side.