Sub topic 8
Building a 12-column grid with CSS Grid
This chapter assumes you know basic HTML structure, CSS selectors and units (px, rem, %), and the box model. You will build the twelve-column layout from the earlier chapters: a centered container, flexible columns, and a fixed gutter.
The working model
CSS Grid lets a parent element define columns, and its children are placed into them. Three properties map to the parts of a grid:
| Grid part | CSS that controls it |
|---|---|
| Columns | `grid-template-columns` |
| Gutters | `column-gap` (or `gap`) |
| Margins | `padding-inline` on the container |
| Container width | `max-width` and `margin-inline: auto` |
Keep the container and the grid as separate elements. The container controls how wide the page is, and the grid controls how that width is divided.
The markup
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>The styles
.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;
}Here is what each part does.
.pagecaps the width at 72rem.margin-inline: autocenters it when the viewport is wider.padding-inline: 1remkeeps content off the screen edge on narrow screens, so it acts as the margin.display: gridturns.gridinto a grid container, and its direct children become grid items.repeat(12, 1fr)is shorthand for twelve1fr 1fr 1fr ...columns. Thefrunit is a fraction of the free space left after gutters are subtracted, so the columns stay equal at any width.column-gap: 1.5remputs space between columns only. It adds nothing at the outer edges, which is why the margin is handled by the container.span 8andspan 4make the two items cover 8 and 4 columns. Together they fill all twelve.
Why 1fr and not percentages
With percentages you must subtract the gutters yourself, and the arithmetic breaks when you change the gap. With 1fr, the browser subtracts all eleven gutters first and divides what remains. Changing column-gap needs no other edit.
Common errors
- Styling the wrong element.
grid-columnonly works on direct children of a grid container. If you wrap.mainin anotherdiv, that wrapper becomes the grid item. - Putting padding on the grid and expecting gutters. Padding adds outer space. Use
column-gapfor space between columns. - Forgetting the container. Without
max-width, the columns keep stretching on wide monitors and line lengths become hard to read. - Spans that exceed twelve. If items on one row add up to more than 12 columns, the extra item wraps to a new row.
Try it
Paste the HTML and CSS into a file and open it in a browser. Use the developer tools grid overlay (in the Elements panel, click the "grid" badge next to .grid) to see the twelve columns and gutters. Then change column-gap to 3rem and watch the columns narrow while the total width stays the same.
Exercise
A grid uses grid-template-columns: repeat(12, 1fr) and column-gap: 1.5rem. A designer wants a main area of 7 columns and a sidebar of 4 columns, then asks why a gap appears on the right side of the grid. What are the two items' grid-column values, and what causes the empty space?
Show answer
Use grid-column: span 7 for the main area and grid-column: span 4 for the sidebar. Together they cover 11 of the 12 columns, so one column stays empty at the end of the row. To fix it, make the numbers add up to 12, for example 8 and 4, or 7 and 5.