Sub topic 4
min(): capping a value
Prerequisites
This chapter assumes you know basic CSS syntax, the units px, rem, % and vw, the box model, and how width works. It also assumes you have seen calc().
How min() resolves
min() takes two or more comma-separated values. The browser evaluates each one for the property it is used in, then uses the smallest result.
The name can mislead you. Because the smallest value always wins, min() acts as an upper bound. The result can never exceed any of its arguments.
.box {
/* Never wider than 40rem, but shrinks with its container */
width: min(100%, 40rem);
}With a 16px root font size, 40rem is 640px. The 100% value depends on the container, so the result changes as the container changes.
| Container width | 100% | 40rem | Result |
|---|---|---|---|
| 320px | 320px | 640px | 320px |
| 640px | 640px | 640px | 640px |
| 900px | 900px | 640px | 640px |
Relation to max-width
The declaration above behaves like this pair:
.box {
width: 100%;
max-width: 40rem;
}For width, the two forms give the same result. min() is more general because you can use it in any property that accepts a length. That includes padding, margin, gap, font-size and border-width. A value like padding-inline: min(5vw, 2rem) has no max-width equivalent.
Math inside min()
Inside min(), max() and clamp() you can write arithmetic directly, without calc().
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}On a narrow screen, the container is the parent's width minus 2rem. That leaves a 1rem gutter on each side. On a wide screen, it stops at 70rem and margin-inline: auto centers it.
You can also nest functions. min(100%, calc(50% + 4rem)) works, and so does min(100%, 50% + 4rem). The calc() wrapper is optional here.
Common errors
- Reading min() as a lower limit. To set a floor, use
max(). The next chapter covers it. - Missing spaces around + and -.
min(100%-2rem, 70rem)is invalid. The browser drops the whole declaration, so the property falls back to an earlier rule or its default. Always put spaces around+and-. The*and/operators don't need them. - Unitless numbers.
width: min(100%, 40)is invalid because40is not a length. Only0can be written without a unit. - Forgetting what the percentage refers to. In
width, a percentage refers to the containing block's width. Inheight, it refers to the containing block's height. That height is oftenauto, so the percentage may not resolve as you expect. - Comparing incompatible types. All arguments must resolve to the same type. You can compare lengths with percentages in a length property, but you can't compare a length with a plain number.
Putting it together
Use min() when you want a value that follows available space until it reaches a limit. Write the flexible argument first, such as 100% or 90vw, and the fixed limit second. The order doesn't change the result, but this convention makes the code easier to read.
Exercise
A section has padding-inline: min(5vw, 2rem); and the root font size is 16px. What is the padding at a viewport width of 400px, and at 1000px?
Show answer
At 400px, 5vw is 20px and 2rem is 32px, so the padding is 20px. At 1000px, 5vw is 50px and 2rem is 32px, so the padding is 32px. The padding grows with the viewport until it reaches 2rem, then stays there.