Sub topic 8
A container that never overflows
This chapter assumes you know the box model, the width property, and the units %, rem, and vw. It also assumes you have seen calc(). You will build a centered page container with min() that keeps a gutter on small screens and stops growing on large ones.
The problem with a fixed width
A fixed width is the simplest way to build a readable column.
.container {
width: 70rem;
margin-inline: auto;
}On a 1400px screen this works. On a 375px phone, the container is still 1120px wide (70rem at a 16px root size), so the page scrolls sideways.
The traditional fix sets width: 100% and adds max-width: 70rem. That works, but it uses two properties to say one thing: "be as wide as the parent, up to a limit."
One declaration with min()
min() returns the smallest of its arguments. Used for width, it acts as an upper bound.
.container {
width: min(100%, 70rem);
margin-inline: auto;
}The browser resolves both arguments against the container's parent and picks the smaller one:
- In a 375px parent,
100%is 375px and70remis 1120px, so the width is 375px. - In a 1400px parent,
100%is 1400px, so the width is 1120px.
Adding a gutter
With width: min(100%, 70rem), text touches the screen edge on a phone. You could add padding to the container. A simpler option is to subtract the gutter inside the function.
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}Inside min(), max(), and clamp(), you can write math directly without calc(). The usual operator rules still apply. + and - need spaces on both sides, so 100%-2rem is invalid.
Subtracting 2rem leaves 1rem free on each side. The margin-inline: auto declaration splits that free space equally, which creates the gutters. On wide screens, 70rem wins and the same auto margins center the column.
Padding versus subtracting
You may prefer padding on the container, which has one catch.
.container {
box-sizing: border-box;
width: min(100%, 70rem);
padding-inline: 1rem;
margin-inline: auto;
}This also works, but only because of box-sizing: border-box. Without it, the padding is added on top of the 100% width, and the container overflows by 2rem. The subtraction version has no such trap, because the gutter is part of the width calculation itself. It also leaves the container's background flush to the content, which you may or may not want.
Comparing the approaches
| Approach | Declarations | Gutter on small screens |
|---|---|---|
| `width: 70rem` | 1 | None, and it overflows |
| `width: 100%; max-width: 70rem` | 2 | Needs padding |
| `width: min(100%, 70rem)` | 1 | Needs padding |
| `width: min(100% - 2rem, 70rem)` | 1 | Built in |
Checking your work
Open the browser's developer tools and use the device toolbar to shrink the viewport. Watch for a horizontal scrollbar. In the Computed panel, the width value shows the single length that min() resolved to. If it is larger than the parent, some other rule is overriding your declaration.
Exercise
A container uses width: min(100% - 4rem, 60rem) with margin-inline: auto. The parent is 500px wide and the root font size is 16px. What is the container's width, and how wide is each side margin?
Show answer
4rem is 64px and 60rem is 960px. The first argument is 500px - 64px = 436px, which is smaller, so the width is 436px. The free space is 64px, and the auto margins split it into 32px per side.