Sub topic 8
Layout patterns across frames: stacking, columns, and grids
This chapter assumes you know the CSS box model, selectors, and rem and fr units. It also assumes you have read the earlier chapters on media queries, mobile-first CSS, and choosing breakpoints. Here you will apply those ideas to the three layout patterns you will use most.
The three patterns
Most responsive pages are built from three patterns:
- Stacking: blocks sit in one column, one after another. This is the default in the mobile frame.
- Columns: the same blocks sit side by side. Tablet frames often use two columns.
- Grids: a repeating set of items, such as cards, fills the available width and wraps by itself.
Your design frames should show which pattern applies at each width. The CSS then follows the frames.
Stacking and columns with mobile-first CSS
Write the stacked layout first. Add columns only when the frame has room for them.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 48em) {
.layout {
grid-template-columns: 1fr 1fr;
}
}
/* Desktop and up */
@media (min-width: 80em) {
.layout {
grid-template-columns: 2fr 1fr 1fr;
max-width: 80rem;
margin-inline: auto;
}
}The base rule has no grid-template-columns, so the children stack in a single column. At 48em the layout becomes two equal columns. At 80em it becomes three columns, with the first twice as wide as the others. The max-width and margin-inline: auto pair keeps lines of text from stretching across very wide monitors and centers the layout.
Note that the layout is built with fr units and a gap. It stays fluid between the breakpoints, so a 60em window still looks correct.
Grids that need no breakpoints
A card list does not always need media queries. Let the grid decide how many columns fit:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Each card is at least 16rem wide. The browser fits as many columns as it can, then shares the leftover space with 1fr. A narrow phone gets one column, a tablet gets two or three, and a desktop gets more.
Use this pattern when the items are all equal and their order does not matter. Use explicit breakpoints, as in the previous example, when you need named regions such as a main column and a sidebar, or when the design frames show a specific column count.
Common errors
- Putting
grid-template-columnsin the base rule. This forces columns on the smallest screens, and you must then undo them in amax-widthquery. - Forgetting the width cap. Without
max-width, a three-column layout on a very wide monitor leaves columns far apart and lines too long to read. - Reordering content only in CSS. Changing visual order with
orderor grid placement does not change the keyboard and screen-reader order. Keep the HTML source order sensible for the stacked mobile layout. - Designing only three widths. The frames are checkpoints. The layout must also work at widths between them.
Exercise
A product list must show one column on phones, and as many 14rem columns as fit on larger screens. Which approach needs fewer media queries, and what is the key line of CSS?
Show answer
Use a fluid grid with no media query: grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));. On a phone narrower than about two cards, only one column fits, so it stacks. On wider screens the browser adds columns automatically.