Sub topic 8
A container that never overflows
This chapter assumes you know basic CSS syntax, the box model, the width property, and the units px, rem, % and vw. It also assumes you have seen min() as an upper bound, which the previous chapters covered. Here you apply it to the most common layout job: a centered content column that fits any screen.
The problem with a fixed-width column
A typical page wrapper has a preferred width on large screens and should shrink on small ones. A first attempt often looks like this:
.container {
width: 70rem;
margin-inline: auto;
}On a 1200px screen this works. On a 375px phone, the box stays 70rem wide (1120px at the default root font size), so the page scrolls sideways. The element does not know the viewport is smaller than it is.
The classic fix: width plus max-width
The long-standing fix uses two properties:
.container {
width: 100%;
max-width: 70rem;
margin-inline: auto;
}The box wants the full width of its parent and is capped at 70rem. This is correct, and you will see it in most codebases. It has one weakness. The box touches the screen edges on narrow viewports, so you must add side padding separately:
.container {
width: 100%;
max-width: 70rem;
margin-inline: auto;
padding-inline: 1rem;
box-sizing: border-box;
}Now you have four properties that interact. If box-sizing is content-box, the padding is added on top of width: 100% and the box overflows by 2rem. This is a common source of horizontal scrollbars.
The same result with min()
min() takes a list of values and resolves to the smallest one. The browser evaluates each argument against the element's context and picks the smallest result. You can express the whole width rule in one declaration:
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}Read it as: "the width is 100% of the parent minus a 2rem gutter, but never more than 70rem."
Math works directly inside min(), max() and clamp(). You do not need to wrap 100% - 2rem in calc(). The calc() rules still apply inside these functions. Spaces around + and - are mandatory, so 100%-2rem is invalid.
The browser resolves this at layout time. The % value depends on the parent's width, which is why the result adapts without a media query.
| Parent width | `100% - 2rem` | `70rem` | Resolved width | Side gap per edge |
|---|---|---|---|---|
| 375px | 343px | 1120px | 343px | 16px |
| 800px | 768px | 1120px | 768px | 16px |
| 1136px | 1104px | 1120px | 1104px | 16px |
| 1600px | 1568px | 1120px | 1120px | 240px |
The table assumes a 16px root font size. The switch happens where 100% - 2rem equals 70rem, which is a parent width of 72rem (1152px). Below that, the gutter is fixed at 1rem per side. Above it, the container is capped and margin-inline: auto splits the extra space equally.
A complete example
Save this as an HTML file and open it. Resize the window and watch the gutter.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fluid container</title>
<style>
body {
margin: 0;
font-family: system-ui, sans-serif;
}
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
background: #eef3ff;
padding-block: 2rem;
}
.box {
/* Never wider than 40rem, but shrinks with its container */
width: min(100%, 40rem);
background: #c9d8ff;
}
</style>
</head>
<body>
<main class="container">
<h1>Intrinsic container</h1>
<p class="box">This box is at most 40rem wide and never overflows the container.</p>
</main>
</body>
</html>The .box rule shows the shorter form of the same idea. min(100%, 40rem) is equivalent to width: 100%; max-width: 40rem.
Why this is "intrinsic"
The container has no breakpoint. Its width comes from the available space and a cap, so it works in a phone, a desktop window, a narrow sidebar, or an embedded widget. A media query such as @media (min-width: 72rem) responds to the viewport only. A min() based on % responds to the actual parent, wherever the component is placed.
Choosing between min() and max-width
The two approaches produce the same box in most cases. They differ in these ways:
min()keeps the gutter inside the width calculation, so padding andbox-sizingdo not affect it.max-widthworks with the cascade in a waywidthdoes not. You can overridemax-widthin a variant class without touchingwidth.min()does not interact withmin-widthormax-width. If another rule setsmax-width: 50rem, it still caps the result of yourmin().- Some design systems prefer padding for the gutter, because backgrounds can then extend to the edge while text stays inset. With
min()andmargin-inline: auto, the container's own background stops at the gutter.
Pick one approach per project and apply it consistently.
Edge cases and gotchas
Avoid 100vw for the width
You might write width: min(100vw - 2rem, 70rem). This is fragile. 100vw includes the width of a classic vertical scrollbar, which is about 15px on many desktop browsers. The container then overflows the visible area by that amount and triggers a horizontal scroll. Use % so the width follows the parent, not the viewport.
Percentages need a definite parent
% in width resolves against the containing block's width. If the container sits in a shrink-to-fit context, such as a flex item with width: auto, an inline-block, or a float, the percentage can create a circular dependency. The browser falls back to treating it as auto for the intrinsic size calculation, and then resolves it later. The result can surprise you. When the container lives inside a flex row, add min-width: 0 to the flex item or give it an explicit flex value.
Nesting containers
If you apply .container inside another .container, each one subtracts its own 2rem. The inner element ends up 2rem narrower than the outer one's content box, and the gutters accumulate. Use the class once per page region.
Mixing the margin shorthand
margin-inline: auto centers the box only when width is not auto. With min() the width is always definite, so centering works. If another rule later sets margin: 0, you lose centering. Order your rules so that the shorthand comes first.
Make the gutter adjustable
Hard-coding 2rem in many places makes changes tedious. Store it in a custom property and reuse it:
:root {
--gutter: 1rem;
--page-max: 70rem;
}
.container {
width: min(100% - 2 * var(--gutter), var(--page-max));
margin-inline: auto;
}Multiplication inside min() is valid. One side of * must be a plain number, and 2 satisfies that. var(--gutter) supplies the length.
You can also make the gutter itself fluid with clamp(), which later chapters cover:
.container {
--gutter: clamp(1rem, 4vw, 3rem);
width: min(100% - 2 * var(--gutter), 70rem);
margin-inline: auto;
}Here the gutter grows from 1rem to 3rem as the viewport widens. The container is still capped at 70rem.
Debugging checklist
If the container overflows or sits off-center, check these in order:
- Open the browser's developer tools and read the computed
widthon the element. This shows which argument ofmin()won. - Look for a
min-widthon the same element. Amin-widthlarger than yourmin()result overrides it and causes overflow. - Check for spaces around
+and-. A missing space makes the whole declaration invalid, and the browser ignores it silently. - Check the parent. A parent with a fixed width, or a child sized with
100vw, defeats the intrinsic behavior. - Confirm that the viewport meta tag is present. Without it, mobile browsers lay out at roughly 980px and then scale down, so your 375px tests show nothing useful.
Summary of the pattern
width: min(100% - gutter, max)gives a fluid width, a cap, and a gutter in one declaration.margin-inline: autocenters the box once it hits the cap.- Use
%, notvw, for the shrinking value. - Put the gutter and the cap in custom properties when they appear in more than one place.
Exercise
A container uses width: min(100% - 3rem, 60rem); margin-inline: auto; inside a parent that is 900px wide, with a root font size of 16px. What width does the container resolve to, and how much space is on each side? What parent width is the smallest at which the 60rem cap takes effect?
Show answer
100% - 3rem is 900px - 48px = 852px. 60rem is 960px. The smaller value is 852px, so the container is 852px wide. The leftover space is 900 - 852 = 48px, split evenly by margin-inline: auto, so 24px on each side (1.5rem). The cap takes effect when 100% - 3rem equals 60rem, so the parent width is 63rem, which is 1008px. At that width or wider, the container stays 960px.
Exercise
A developer writes width: min(100vw - 2rem, 70rem) and users on desktop report a small horizontal scrollbar on pages that are tall enough to scroll vertically. Explain the cause and give the fix.
Show answer
100vw includes the width of the vertical scrollbar, but the space available for layout excludes it. On a narrow desktop window, the container is therefore wider than the visible content area by the scrollbar width, which causes horizontal overflow. The fix is to replace 100vw with 100%, so the width follows the parent's actual content width: width: min(100% - 2rem, 70rem).