Sub topic 6
Containers: maximum width and centering
This chapter assumes you know basic HTML (elements and classes), basic CSS (selectors, properties, and the units px, rem, and %), and the box model (margin, padding, width). It also assumes you have met columns, gutters, and margins in the earlier chapters.
What a container does
A column grid has columns and gutters. It also needs a boundary: a width the whole grid is allowed to occupy, and a position for that width on the screen. A container is the element that supplies this boundary. It does three jobs:
- It caps the width of the page content so lines of text and rows of cards do not stretch across a very wide monitor.
- It centers the content horizontally when the viewport is wider than the cap.
- It keeps a minimum space between the content and the viewport edges when the viewport is narrow. This is the outer margin of the grid.
Without a container, a 12-column grid on a 2560px screen produces columns that are each over 190px wide. Paragraphs spanning eight of those columns become far too long to read comfortably. The container prevents this.
The standard container
The markup and styles below are the same container pattern used in the rest of this tutorial.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}Each declaration has one job.
| Declaration | Job |
|---|---|
| `max-width: 72rem` | Lets the element shrink with the viewport but never grow beyond 72rem. |
| `margin-inline: auto` | Splits any leftover horizontal space equally between the left and right sides, which centers the element. |
| `padding-inline: 1rem` | Keeps content 1rem away from the container's edges, so on narrow screens text does not touch the screen edge. |
Why max-width and not width
A fixed width: 72rem forces horizontal scrolling on any viewport narrower than 72rem. A block element with max-width behaves differently. By default a block element is as wide as its parent allows, so on a narrow screen it fills the available width. The cap applies only once the viewport is wide enough to reach it. You get a fluid layout below the cap and a fixed-width layout above it, with no media query.
Why auto margins center the box
For a block-level element with a constrained width, the browser computes the horizontal margins so that the margin, border, padding, and content widths add up to the width of the parent. If both margin-left and margin-right are auto, they receive equal shares of the leftover space. When there is no leftover space, because the viewport is narrower than the cap, the auto margins resolve to zero and nothing is centered because nothing needs to be.
margin-inline: auto is the logical-property form of margin-left: auto; margin-right: auto. It also works in right-to-left and vertical writing modes, where "inline" points in a different direction. The older shorthand margin: 0 auto also centers, but it sets the top and bottom margins to 0 as well, which can silently erase vertical margins you set earlier in the cascade.
Why padding supplies the side margins
The outer margin of a grid could be made with margin on the container, but that conflicts with margin-inline: auto. You cannot ask the same property to both center the box and reserve a fixed gap. Putting the gap in padding-inline avoids the conflict. The two properties work together:
- When the viewport is narrow, the auto margins are 0 and the padding is the whole outer margin.
- When the viewport is wide, the auto margins grow and the padding stays inside the container as a small extra inset.
The box-sizing effect
Whether the cap includes the padding depends on box-sizing.
- With the default
content-box,max-width: 72remlimits only the content area. The total width is 72rem plus 2rem of padding, so 74rem. - With
border-box,max-width: 72remincludes the padding. The content area is 70rem.
Most projects set border-box globally so that widths mean what they say:
*,
*::before,
*::after {
box-sizing: border-box;
}Take the container above at 16px per rem. With border-box, the container is 1152px wide and the content area is 1120px. A 12-column grid with a 1.5rem (24px) gutter has 11 gutters totalling 264px, so each 1fr column is (1120 − 264) ÷ 12, about 71.3px. The exact column width is something to know, not something to set. The fr unit computes it for you, which is why the container's width is the only number you need to choose.
Choosing the maximum width
There is no universal correct value. These are common approaches:
- rem-based caps such as
72rem(1152px at the default font size). If a user raises their default font size, the container grows with it, which keeps the number of characters per line stable. This is usually the better choice. - px-based caps such as
1200px. These are predictable for design handoff but do not respond to user font settings. - ch-based caps such as
65ch. Onechis the width of the "0" glyph in the element's font, so this directly targets line length. It suits a single text column, not a whole page grid.
A practical rule: use a wide container (roughly 64 to 80rem) for the page grid, and rely on column spans to keep text blocks narrow. If you have a page that is mostly one article, a separate narrow container of about 65ch to 75ch for the text often reads better than any column span.
Container and grid: one element or two
You can place display: grid on the same element as the container:
.page-grid {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}This is shorter, but it has a cost. Every direct child becomes a grid item, so you cannot put a full-width background band inside the container, and every section must be placed on the 12 columns. Using two elements, an outer .page and an inner .grid, gives you a place to attach things that belong to the container but not to the columns, such as a background colour or a header that spans the full viewport.
Full-bleed backgrounds
A common need is a background colour that reaches both screen edges while the content stays inside the container. The reliable pattern is to put the colour on an outer element and the container inside it:
<section class="band">
<div class="page">
<div class="grid">
<p class="main">Content stays inside the container.</p>
</div>
</div>
</section>.band {
background: #eef2f7;
}.band is a normal block, so it fills the viewport width. The .page inside it is capped and centered. Do not put the background on .page itself, because it would stop at the cap and leave plain stripes on either side.
An alternative: min() instead of padding
The same effect can be achieved with a single width declaration:
.page {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}Here the width is 2rem less than the parent, up to a maximum of 72rem. The 1rem gaps on each side then come from the auto margins, and no padding is needed. This keeps the container's content width at exactly 72rem, regardless of box-sizing. The trade-off is that a full-bleed child that must extend to the container's padding edge cannot be built easily with this pattern. Choose one approach per project and use it consistently.
Edge cases and common errors
- Centering fails because the element is not block-level.
margin-inline: autodoes not center an inline element such as aspan. It also behaves differently on flex items, where auto margins absorb free space along the main axis. A container should be a block-leveldivor a semantic element such asmain. - Horizontal scrollbar from
100vw. Thevwunit includes the width of a vertical scrollbar on systems where scrollbars take space. A container set towidth: 100vwcan be wider than the visible area. Prefer100%or no width at all. - Padding added on top of
width: 100%. Incontent-box,width: 100%pluspadding-inline: 1remmakes the box 2rem wider than its parent and causes overflow. Useborder-box, or omit the width. - Percentage padding.
padding-inline: 5%is a percentage of the parent's width, so the margin grows with the viewport. That can be what you want, but it is easy to forget when the container sits inside another padded element. - Nested containers. Placing a
.pageinside another.pagedoubles the padding and may shrink the grid below what you expect. Use one container per page region. - Missing
max-widthon very wide screens. The page looks fine on a laptop, so the problem goes unnoticed until someone opens it on an ultrawide display. Test at 1920px and wider.
Exercise
A container has max-width: 60rem, margin-inline: auto, padding-inline: 1rem, and box-sizing: border-box. The default font size is 16px. What is the width of the content area, and how wide is each of the two auto margins when the viewport is 1200px wide (ignoring any scrollbar)?
Show answer
The container is 60rem = 960px wide in total, and the 1rem (16px) padding on each side leaves a content area of 960 − 32 = 928px. The leftover space is 1200 − 960 = 240px, split equally, so each auto margin is 120px.
Exercise
You want a pale blue background that spans the full viewport width behind a section, while its content stays inside a centered 72rem container. Which element should carry the background colour, and why can't .page carry it?
Show answer
An outer wrapper element (such as a section with a band class) should carry the colour, with .page nested inside it. The wrapper is a normal block and fills the viewport width. .page is capped at 72rem, so a background on it would stop at the cap and leave unfilled space on both sides of wide screens.