Sub topic 12
Fluid and Responsive Grids for Screens
This chapter assumes you know the parts of a grid (columns, gutters, margins), can calculate a column width, and have read basic CSS. It covers how a grid behaves when the page width is not fixed, which is the normal condition on screens.
Why screens need a different approach
A printed page has one size. A web page is shown in a phone held upright, a laptop window, a wide monitor, and a window the reader has dragged to any width in between. A grid built for one width breaks at the others. Three strategies deal with this, and most real sites combine them.
| Strategy | What changes with width | What stays fixed | Typical risk |
|---|---|---|---|
| Fixed | Nothing | Every column, gutter and margin in pixels | Horizontal scrolling on small screens, empty space on large ones |
| Fluid | Column widths | Column count, usually gutters | Lines too short on phones and too long on wide monitors |
| Responsive | Column count and often gutter and margin | Rules for each width range | Too many layouts to maintain and test |
Fluid grids: proportions instead of measurements
A fluid grid defines widths as shares of the container rather than as fixed lengths. In CSS, the fr unit means one fraction of the free space left after gutters are subtracted. repeat(12, 1fr) gives 12 equal columns that share whatever width is available.
This is the same arithmetic as the column-width function from the grid calculation chapter, with the page width replaced by the container width. If the container is 1000 px, the margins are 0 and the gutters are 24 px, each of 12 columns is (1000 − 11 × 24) / 12 ≈ 61.3 px. At 600 px each column is ≈ 28.8 px. The gutters stay the same, so columns absorb all the change.
Fluid layouts fail at the extremes. Stretch a four-column text grid across a 2560 px monitor and each line of body text runs far beyond a comfortable measure. Squeeze a 12-column grid onto a phone and each column is about 15 px wide, which is useless. This is why fluid grids need two additional controls: a maximum width and breakpoints.
Capping the width
A maximum width stops the grid from growing forever. Past that width the layout stays the same size and the leftover space becomes outer margin.
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: clamp(1rem, 4vw, 3rem);
}Here margin-inline: auto centres the container. The clamp() function sets the side padding to 4% of the viewport width, but never below 1rem or above 3rem. The margin therefore grows gently with the screen instead of jumping between fixed values.
Breakpoints and column counts
A breakpoint is a viewport width at which the layout rules change. In a responsive grid the usual change is the number of columns. Common choices are 4 columns on phones, 8 on tablets and 12 on desktops. All three are divisible in useful ways, so a 12-column layout can split into halves, thirds and quarters, an 8-column layout into halves and quarters, and a 4-column layout into halves.
.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; }
}The rules are written mobile first. The base rule describes the smallest layout, and each min-width query adds changes for wider screens. This keeps the narrow case simple and avoids overriding many rules.
Breakpoints are written in em here because media queries in em follow the reader's browser font size. A reader who has enlarged their default text gets the narrower layout sooner, which is usually what they need.
Spans must change with the column count
Changing the column count means every item's span must also be reconsidered. An article spanning 8 of 12 columns would make no sense on a 4-column phone grid, where it should span all 4.
.article, .sidebar { grid-column: 1 / -1; }
@media (min-width: 64em) {
.article { grid-column: 1 / span 8; }
.sidebar { grid-column: 9 / span 4; }
}The line number -1 means the last grid line, so 1 / -1 spans the full width whatever the column count. On narrow screens both elements stack in source order. On wide screens they sit side by side. Using -1 instead of a fixed number avoids repeating the span at every breakpoint.
Grids that adapt without breakpoints
Sometimes you do not want to choose the column count yourself. auto-fit with minmax() asks the browser to fit as many columns as possible, each at least a minimum width.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1.5rem;
}Each column is at least 16rem wide. If the container has room for four, you get four. If it shrinks, you get three, then two, then one, with no media query. This suits uniform items such as product cards or a gallery, which is a modular grid whose module count depends on the width.
The trade-off is control. Items cannot be told to span "8 of 12" because the column count is not fixed, and the columns of different sections will not line up with each other if the sections have different minimum widths. Use auto-fit for collections of similar items, and an explicit column count for editorial pages with a designed hierarchy.
Keeping the measure under control
The line length rules from the typography chapters still apply. A grid that makes columns flexible can make text columns unreadable. Two defences work well together.
- Limit the text block with a width in
chunits, where 1ch is the width of the "0" character in the current font. A cap ofmax-width: 70chkeeps lines near the upper end of a comfortable range. - Let the grid decide how many columns the text spans at each breakpoint, so the cap rarely has to act.
.prose { max-width: 70ch; }Vertical rhythm on screens
Fluid type complicates the baseline grid. If the font size scales with the viewport but the line height is a fixed 1.5rem, the lines either crowd or drift apart. A common compromise is to keep one spacing unit in rem, scale type only within a narrow range, and accept that the baseline grid is approximate on screen. Browsers apply their own text rounding and users may zoom or change fonts, so exact baseline alignment between columns is not reliably achievable on the web. Aim for consistent multiples of one unit rather than pixel-perfect alignment.
How the layout flows between widths
Common errors
- Fixing widths in pixels inside a fluid grid. One
width: 600pxitem overflows a 375 px phone and creates a horizontal scrollbar. Use spans ormax-width: 100%. - Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">in the page head, mobile browsers render a wide virtual page and your media queries match the wrong widths. - Too many breakpoints. Each one doubles what you must check. Three or four ranges cover most designs.
- Changing the column count but not the spans. Items keep their desktop placement and leave gaps or force implicit extra columns.
- Ignoring source order. Stacking on narrow screens follows the HTML order. Reordering with
grid-columnororderchanges what is visible but not what keyboard and screen reader users move through, so keep the HTML in a logical reading order.
Choosing between the approaches
Use a fixed grid only when the output has a known size, such as a banner or a slide. Use a fluid grid with a width cap for simple single-flow pages. Use explicit breakpoints when the layout has a designed hierarchy that must change shape, such as article plus sidebar. Use auto-fit for repeated items of similar size. Many pages use all of these: breakpoints for the page frame and auto-fit inside a gallery section.
Exercise
A container is 900 px wide with no side margins. It uses a 12-column fluid grid with 20 px gutters. (a) How wide is each column? (b) A sidebar must span the last 4 columns on desktop but should fill the full row on a 4-column phone grid. Write the two grid-column rules, assuming a media query at 64em.
Show answer
(a) There are 11 gutters, so the usable column space is 900 − 11 × 20 = 680 px. Divided by 12 columns, each is about 56.7 px. (b) Base rule: .sidebar { grid-column: 1 / -1; }. Inside @media (min-width: 64em): .sidebar { grid-column: 9 / span 4; }.
Exercise
A gallery uses grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)) with a 1rem gap and holds two items. The container is wide enough for six 12rem columns. How do the items look, and what would change with auto-fill?
Show answer
With auto-fit, the four empty tracks collapse to zero width, so the two items stretch to share the whole row and become very wide. With auto-fill, the empty tracks are kept, so the two items stay about one sixth of the row wide and leave empty space on the right.