Sub topic 12
Fluid and responsive grids for screens
This chapter assumes you know what columns, gutters and margins are, and that you can read basic CSS. A print page has one fixed size. A screen can be any width, so a screen grid has to adapt.
Fixed, fluid and responsive
A fixed grid uses pixel widths. It looks exact on one screen and breaks on others. A fluid grid defines columns as proportions of the available width, so the layout stretches and shrinks. A responsive grid is fluid and also changes its structure at set widths, called breakpoints. A breakpoint is a viewport width at which the layout rules change.
| Type | Column width | Behavior on a narrow screen |
|---|---|---|
| Fixed | Pixels | Scrolls sideways or is cropped |
| Fluid | Fractions of the width | Columns get thin |
| Responsive | Fractions, plus new rules at breakpoints | Fewer, wider columns |
Fluid alone fails at the extremes. At 320 px, twelve columns are too narrow to hold text. At 2000 px, lines run far past a readable measure. Responsive grids fix this by changing the column count and capping the width.
Changing the column count
A common pattern is 4 columns on phones, 8 on tablets and 12 on desktops. The numbers divide neatly, so a layout can span half the grid at every size. Margins and gutters often grow with the width too.
The code below uses the same markup at all three sizes. Only the column count and the gutter change.
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
max-width: 72rem;
margin-inline: auto;
padding-inline: 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; }
}The max-width caps the grid on very wide screens. The margin-inline: auto centers it, so the extra space becomes outer margin.
Spans must change too
Changing the column count is not enough. An item that spans 8 columns fills a whole 8-column tablet grid, but it only fits a 4-column phone grid if you change its span. Set the span at each breakpoint.
.article, .sidebar { grid-column: 1 / -1; }
@media (min-width: 64em) {
.article { grid-column: 1 / span 8; }
.sidebar { grid-column: 9 / span 4; }
}On small screens both items take the full width and stack. The line 1 / -1 runs from the first grid line to the last, whatever the column count is.
Choosing breakpoints
Pick breakpoints from your content, not from device models. Widen the browser until the layout looks wrong, such as a line becoming too long or a gutter looking cramped. Then add a breakpoint there. Use em units in media queries so the breakpoints follow the user's font size setting.
Fluid grids without breakpoints
CSS Grid can adjust the column count by itself. The auto-fit keyword with minmax() creates as many columns as fit, each at least a minimum width.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1rem;
}This suits uniform items like cards. It cannot place an item at a specific column, because the number of columns is not fixed. For editorial layouts with set spans, use explicit breakpoints.
Common errors
- Fixing the gutter in pixels while the columns are fluid. The gutter then looks too large on small screens.
- Forgetting to cap the width, which gives very long lines on large monitors.
- Leaving a span of 8 columns on a 4-column grid. The browser creates extra implicit columns and the layout overflows.
- Choosing breakpoints for specific phones instead of for where the content breaks.
Exercise
A page uses a 12-column grid with .sidebar { grid-column: 9 / span 4; } and no other rules. What goes wrong on a phone, and how do you fix it?
Show answer
On a phone the grid has only 4 columns, so the sidebar starts at a column that does not exist. The browser adds implicit columns and the page overflows sideways. Set the default to grid-column: 1 / -1; so the sidebar fills the full width, and apply 9 / span 4 only inside a wide breakpoint such as @media (min-width: 64em).