Sub topic 13
Building grids with CSS Grid
This chapter assumes you know basic HTML and CSS, and that you understand columns, gutters, modules and spanning from the earlier chapters. Here you turn those ideas into working CSS. The goal is a small set of rules that you can read back as a grid specification.
From grid terms to CSS properties
CSS Grid maps directly onto the parts of a grid you already know.
| Grid part | CSS Grid |
|---|---|
| Columns | `grid-template-columns` |
| Rows (modules) | `grid-template-rows` |
| Gutters | `gap`, `column-gap`, `row-gap` |
| Outer margins | `padding` or `margin-inline` on the container |
| Spanning | `grid-column` and `grid-row` with `span` |
The fr unit is a fraction of the free space left after gutters are removed. It does the arithmetic from the grid math chapter for you. In the Python version, you subtract the gutters and divide. In CSS, repeat(12, 1fr) does the same.
A multi-column grid
This grid has 12 columns and a fixed gutter. The article spans eight columns and the sidebar spans the remaining four.
.columns {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
max-width: 72rem;
margin-inline: auto;
}
.article { grid-column: 1 / span 8; }
.sidebar { grid-column: 9 / span 4; }Read grid-column: 9 / span 4 as "start at line 9, cover four columns." Grid lines are numbered, not the columns. A 12-column grid has 13 lines. Counting columns instead of lines is the most common mistake here.
max-width and margin-inline: auto center the grid and cap the line length, which protects the measure on wide screens.
A modular grid
A modular grid adds fixed-height rows. Here is a four-column, six-row grid with a feature image that fills a block of two columns by three rows.
.modular {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(6, 8rem);
gap: 1rem;
}
.feature { grid-column: 1 / span 2; grid-row: 1 / span 3; }
.caption { grid-column: 3 / span 2; grid-row: 1; }Fixed row heights are what make this modular. If content is taller than its module, it overflows. Give captions and text a size that fits, or let the row height be flexible with minmax(8rem, auto). Choose one on purpose, because the two options make different trade-offs between strict modules and no clipped text.
Making the grid responsive
A responsive grid changes its column count at breakpoints. The markup stays the same.
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}
@media (min-width: 48em) {
.grid { grid-template-columns: repeat(8, 1fr); }
}
@media (min-width: 64em) {
.grid { grid-template-columns: repeat(12, 1fr); gap: 1.5rem; }
}Spans must change along with the column count. An element that spans 8 columns on a 4-column grid creates extra implicit columns and breaks the layout. Redefine its span inside each media query.
Vertical rhythm on screen
Browsers have no true baseline grid. You approximate one by making line height and vertical spacing multiples of one unit.
body {
font-size: 1rem;
line-height: 1.5rem;
}
p, h2, figure {
margin-block: 0 1.5rem;
}Every text line sits on a 1.5rem step. Images need heights that are also multiples of that unit, or the rhythm drifts.
Common errors
- Counting columns rather than lines in
grid-column. - Leaving a span at 8 when the grid drops to 4 columns.
- Using fixed row heights with text of unknown length, which causes overflow.
- Setting gutters with margins on items, which makes the gutter uneven at the edges. Use
gap.
Exercise
A 12-column grid has column-gap: 1.5rem. You want a card to fill the middle six columns, with three empty columns on each side. Write the grid-column value.
Show answer
grid-column: 4 / span 6. The three empty columns are 1 to 3, so the card starts at line 4 and covers six columns, ending at line 10. Columns 10 to 12 stay empty.
Exercise
A layout uses grid-column: 1 / span 8 for an article. On screens under 48em the grid has 4 columns. What goes wrong, and how do you fix it?
Show answer
The span asks for eight columns in a four-column grid, so the browser adds implicit columns and the layout overflows. Inside a media query, or as the default, set the article to grid-column: 1 / -1 so it fills all columns. At the wider breakpoint, use 1 / span 8 again.