Sub topic 8
Building a 12-column grid with CSS Grid
This chapter turns the column, gutter, and margin ideas from earlier chapters into working CSS. It assumes you know basic HTML structure and classes, CSS selectors and units (px, rem, %), and the box model (margin, padding, width). You do not need earlier experience with CSS Grid.
What you will build
You will build a page with three layers:
- A container that caps the page width, centers it, and provides the outer margins.
- A grid that divides the container into twelve equal columns separated by gutters.
- Items that occupy a chosen number of those columns.
Keeping these jobs separate makes the layout easier to change later. The container decides how wide the page is. The grid decides how that width is divided. The items decide how much of the division they use.
The markup
The markup has an outer container, a grid element, and two items.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>The class names describe roles, not appearance. .page is the container, .grid is the grid, and .main and .sidebar are items. Because the HTML carries no layout information, you can change the layout from CSS alone.
The container: width, centering, and margins
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}Each declaration has a separate purpose:
max-width: 72remstops the page from growing past 72 times the root font size. At the default 16px that is 1152px. Below that width the container simply fills the viewport, so it is still fluid on small screens.margin-inline: autosplits any leftover horizontal space equally between the left and right sides. This centers the container on wide screens. In a left-to-right language,margin-inlineis the same as settingmargin-leftandmargin-right.padding-inline: 1remcreates the side margins of the grid. On a narrow screen there is no leftover space forautomargins to use, so without this padding text would touch the screen edge.
The padding is part of the container's content box calculation. If you set box-sizing: border-box (a common reset), max-width includes the padding, so the grid inside is 72rem minus 2rem wide. If you leave the default content-box, the grid is the full 72rem and the container is 74rem wide. Pick one approach and use it everywhere.
Declaring the grid
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}Three properties do the work.
display: grid turns .grid into a grid container. Its direct children, .main and .sidebar, become grid items. Elements nested deeper are not grid items. This matters later when you need subgrid.
grid-template-columns defines the column tracks. A track is one row or column of a grid. repeat(12, 1fr) is shorthand for writing 1fr twelve times.
column-gap sets the gutter. The gap appears only between tracks, never before the first or after the last. That is the behavior you want, because the outer space belongs to the container's padding.
What `1fr` means
The fr unit stands for a fraction of the free space in the grid container. The browser first subtracts the gutters from the container width. It then divides what is left among the fr tracks in proportion to their values.
With twelve 1fr columns and a 1152px grid, the calculation is:
- Gutters: 11 gaps × 24px = 264px.
- Free space for columns: 1152px − 264px = 888px.
- Each column: 888px ÷ 12 = 74px.
Because the browser does this arithmetic, you never write column widths as percentages and you never subtract gutters yourself. When the container shrinks to 600px, the columns shrink to about 28px each, and the gutters stay at 24px.
Spanning columns
By default each grid item takes one cell. Without any placement, the two items would sit in the first two columns, each only a twelfth of the width. To fix that, tell each item how many columns to cover.
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}grid-column: span 8 means "occupy eight columns, starting wherever auto-placement puts me." The main area takes columns 1 to 8, and the sidebar takes columns 9 to 12. The spans add up to twelve, so the row is full.
The gutter between the two items is the same 1.5rem as every other gutter. A spanning item also covers the gutters inside its own span. An item spanning eight columns is therefore eight column widths plus seven gutters wide. This is why content in a wide item still lines up with the columns of the items above and below it.
Placing by line number
Grid lines are numbered, not the columns. Twelve columns have thirteen lines, numbered 1 to 13. You can also count from the end with negative numbers, so -1 is the last line.
.main {
grid-column: 1 / 9;
}
.sidebar {
grid-column: 9 / -1;
}This produces the same result as the spans above. 1 / 9 means "start at line 1, end at line 9", which covers columns 1 to 8. The end value is a line, not a column, which is the usual source of off-by-one errors. Use start / span N when you want a count, as in 5 / span 8, which covers columns 5 to 12.
| Declaration | Lines | Columns covered |
|---|---|---|
| `grid-column: span 8` | auto start, 8 wide | 8 columns from the next free spot |
| `grid-column: 1 / 9` | 1 to 9 | 1 to 8 |
| `grid-column: 9 / -1` | 9 to the end | 9 to 12 |
| `grid-column: 1 / -1` | first to last | all 12 |
| `grid-column: 5 / span 8` | 5 to 13 | 5 to 12 |
How the pieces fit together
Common mistakes in this setup
- Putting
display: gridon the container. If you put it on.page, then.gridbecomes the only grid item and your columns do not apply to.mainand.sidebar. Put it on the element whose direct children you want to place. - Using
marginon items to make gutters. Margins add to the span instead of sitting between columns, so the widths no longer add up. Usegap. - Spans that exceed twelve. If the spans in a row total more than 12, the extra item wraps to a new row. This is valid behavior, so you will see no error. If the layout looks wrong, add up the spans.
- Setting widths on grid items. A
widthon an item fights the track sizing. Let the track size the item, and change the span instead. - Forgetting that
gapis not the outer margin. The first and last columns touch the container's content edge. The outer space comes only from the container's padding.
Checking your grid in the browser
Browser developer tools can draw the grid for you. In Chrome, Edge, and Firefox, inspect the .grid element and click the "grid" badge next to it in the Elements panel. The overlay shows the column lines and numbers, which makes it easy to confirm that .sidebar starts at line 9. Firefox also shows line names and track sizes.
Summary of the working model
- The container sets maximum width, centers the page, and supplies the outer margins through padding.
repeat(12, 1fr)creates twelve flexible columns, and the browser calculates their widths after subtracting the gutters.column-gapcreates gutters only between columns.grid-columnplaces items by span or by line number, and spans should add up to twelve in each row.1frcolumns have a content-based minimum, so useminmax(0, 1fr)if content can overflow.
Exercise
A grid has grid-template-columns: repeat(12, 1fr) and column-gap: 2rem inside a container that is 1000px wide after padding. Assume 1rem = 16px. How wide is each column? Then write the grid-column declaration that places a footer note in columns 3 through 10 using line numbers.
Show answer
The gutters take 11 × 32px = 352px, which leaves 648px for the columns. Each column is 648 ÷ 12 = 54px wide. Columns 3 through 10 start at line 3 and end at line 11, because the end value is the line after column 10. So the declaration is grid-column: 3 / 11, or equivalently grid-column: 3 / span 8.
Exercise
A learner puts display: grid and grid-template-columns: repeat(12, 1fr) on .page, then sets grid-column: span 8 on .main, which is inside .grid inside .page. The span has no visible effect. Why, and what is the fix?
Show answer
.main is not a direct child of .page, so it is not a grid item of .page. Its parent .grid is the grid item, and that element has no grid of its own. The fix is to move the grid declarations onto .grid, the element that directly contains .main and .sidebar.