Sub topic 6
Containers: maximum width and centering
This chapter assumes you know basic HTML and CSS syntax, units such as px, rem, and %, and the box model (margin, padding, width). You will see how a container limits the width of a page, centers it, and supplies the outer margins of a column grid.
What a container does
A container is an element that wraps the grid. It has three jobs:
- It caps the page width, so lines of text do not stretch across a very wide monitor.
- It centers the page horizontally when the viewport is wider than the cap.
- It keeps content away from the screen edges on narrow viewports.
The grid itself only divides the space it is given. The container decides how much space that is.
Markup and CSS
The container wraps the grid element, and the grid holds the items.
<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;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}Each of the three container properties does one job.
max-width: 72remsets the cap. At a 16px root font size, that is 1152px. Below that width the container shrinks with the viewport, so it is fluid up to the cap and fixed beyond it.margin-inline: autosplits the leftover horizontal space equally between the left and right sides. This centers the container. It only works when the element has a width smaller than its parent, whichmax-widthprovides on wide screens.padding-inline: 1remcreates the side margins. On a narrow phone the container is as wide as the viewport, so this padding is the only thing keeping text off the edges.
Why padding and not margin for the side space
The container's margin-inline is already used for centering. Putting the edge spacing in padding keeps the two jobs apart. The padding sits inside the container's width, so the grid is always inset from the edge, whatever the viewport width.
Choosing a maximum width
There is no single correct value. Common choices fall between about 60rem and 80rem. Consider these factors:
- Readable line length. Body text is easiest to read at roughly 45 to 75 characters per line. A full-width text column at 1152px is longer than that, so text blocks usually take only part of the grid, such as eight columns.
- Column width. The cap sets the width that twelve columns and eleven gutters share. A very large cap makes each column wide and the layout sparse.
- Content type. Dashboards and data tables may need a larger cap than articles.
Use rem or ch for the cap instead of px, so it scales if the user changes their default font size.
Common errors
- Using
widthinstead ofmax-width. A fixedwidth: 72remcauses horizontal scrolling on any viewport narrower than 72rem.max-widthlets the container shrink. - Forgetting the side padding. The layout looks fine on a desktop and then touches the screen edges on a phone.
- Adding
box-sizing: border-boxtoo late. Without it,max-widthapplies to the content box only, and the padding adds to the total width. Setbox-sizing: border-boxglobally so the cap includes the padding. - Centering with
margin: 0 autoand then overriding the top or bottom margin elsewhere.margin-inline: autotouches only the horizontal axis.
Exercise
A container has width: 72rem; margin-inline: auto; and no padding. On a 400px-wide phone, what two problems appear, and what change fixes both?
Show answer
The page scrolls horizontally, because 72rem is wider than the 400px viewport, and the auto margins cannot center anything. There is also no space at the edges. Replace width with max-width: 72rem and add padding-inline: 1rem, with box-sizing: border-box set so the padding stays inside the cap.