Sub topic 5
max(): setting a floor
This chapter assumes you know basic CSS syntax, common length units (px, rem, %, vw), the box model, and how width behaves. You have already seen min() as a ceiling. max() is its mirror image.
What max() does
max() takes two or more values and resolves to the largest one. Because the result can never be smaller than any argument, each argument works as a lower bound, or floor.
.sidebar {
/* At least 15rem wide, wider when 25% of the parent is larger */
width: max(15rem, 25%);
}The browser compares the arguments at layout time. If the parent is 1000px wide and the root font size is 16px, then 25% is 250px and 15rem is 240px, so the width is 250px. If the parent shrinks to 800px, 25% is 200px, so 15rem (240px) wins.
The arguments can use different units. The browser converts them all to pixels before comparing.
Reading max() correctly
The names are easy to mix up. In max(15rem, 25%), the word "max" does not mean the value is capped. It means the function returns the maximum of its arguments, so the result is a minimum size in practice.
| Function | Returns | Acts as |
|---|---|---|
| `min(a, b)` | the smaller value | an upper bound |
| `max(a, b)` | the larger value | a lower bound |
Use this rule: to stop a value from getting too small, use max(). To stop it from getting too large, use min().
A floor on a width
The next example gives a box a flexible minimum width.
.box {
/* Never narrower than 20rem, but grows to half of its container */
min-width: max(50%, 20rem);
}This is the same idea as the sidebar. The box takes half of its container when that is at least 20rem. On a narrow container, 20rem takes over.
A floor on spacing
max() is also useful for padding that should scale up but never collapse.
button {
/* Grows with the viewport, never below 1rem */
padding-inline: max(1rem, 2vw);
}At a 400px viewport, 2vw is 8px, so the 1rem floor (16px) applies. At a 1200px viewport, 2vw is 24px and wins. Without the floor, the padding would shrink to almost nothing on small screens.
Math inside max()
As with min(), you can do arithmetic directly inside max(). You do not need a nested calc().
.panel {
/* Full width minus a 2rem gutter, but at least 18rem */
width: max(100% - 2rem, 18rem);
}The usual calc() rules still apply. Put spaces around + and -. Multiplication needs at least one unitless number, and division needs a unitless number on the right.
Common mistakes
- Swapping min() and max(). Writing
width: max(100%, 40rem)forces the element to be at least 40rem wide. On a small screen it overflows. To cap the width, you wantedmin(100%, 40rem). - A floor that is too large. A floor wins over every other argument. If the floor is bigger than the space available, the element overflows. Choose floors that fit your smallest supported viewport.
- Using a single argument.
max(10px)is valid but pointless. Pass at least two values that can actually compete. - Forgetting what a percentage refers to. For
widthandmin-width,%refers to the containing block's width. Forpadding, it also refers to the width, even for vertical padding.
Exercise
A card should be 30% of its parent's width but never narrower than 12rem. Write the width declaration. What width does it get inside a 300px parent, assuming 1rem is 16px?
Show answer
width: max(30%, 12rem); In a 300px parent, 30% is 90px and 12rem is 192px, so the larger value wins and the card is 192px wide.