Sub topic 8
Layout patterns across frames: stacking, columns, and grids
This chapter assumes you know the CSS box model, display: grid and display: flex at a basic level, and the mobile-first min-width media query pattern from earlier chapters. It also assumes you have set up mobile, tablet, and desktop frames in a design tool such as Figma. The goal here is to turn those frames into a small set of layout patterns you can reuse and implement in CSS.
Three patterns that cover most pages
Most responsive layouts are built from three moves. Each frame in your design tool shows one of them, and each one maps to a short piece of CSS.
- Stacking: Blocks sit in one column, one after another. This is the default on narrow screens and costs no CSS beyond normal document flow.
- Columns: Blocks that were stacked now sit side by side. The number of columns is chosen by you, usually two or three, and changes at a breakpoint.
- Grids: Blocks sit in rows and columns at once. The browser can decide the column count itself from the available width.
A typical page uses all three. A page might stack everything on mobile, split into main content plus sidebar on tablet, and use a three-track grid on desktop.
Stacking: the mobile baseline
Stacking needs no layout CSS, because block elements already stack. What you do add is spacing between the blocks and padding at the edges of the page. A grid with a single implicit column does both with one gap declaration, and it gives you a base to add columns to later.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}Using display: grid on the container, even with one column, has a practical advantage over margins on each child. The gap property only adds space between items, so you never have to remove a trailing margin. It also means that the tablet and desktop rules only need to add grid-template-columns.
Source order matters here. On mobile, the visual order is the DOM order. If the design frame shows the sidebar after the main content on mobile but beside it on desktop, put the main content first in the HTML. Reordering with CSS order or grid placement changes only the visual order. Keyboard focus and screen readers still follow the DOM, so large mismatches confuse users.
Columns: adding tracks at breakpoints
When the tablet frame shows two columns, you add grid-template-columns inside a min-width query. The mobile styles stay as they are, and the query adds to 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;
}
}Three details in this code are worth understanding.
- The
frunit shares leftover space.2fr 1fr 1frmeans the first column gets half of the free space and the other two get a quarter each, after gaps are subtracted. The ratios hold at any width between breakpoints. max-widthwithmargin-inline: autocaps the layout. Without a cap, a desktop layout on a very wide monitor stretches lines of text to unreadable lengths. The cap is80remhere, which equals the breakpoint, so the layout stops growing where the desktop frame ends.- Breakpoints are in
em. Media queries inemrespond to the user's browser font size setting, so a person who raises their default text size gets the tablet layout later, when there is less room per column. That is usually the behavior you want.
Why `1fr` can overflow
A frequent bug: a column set to 1fr refuses to shrink below the width of its content, so a long unbroken string or a wide table pushes the layout past the screen. The cause is that 1fr is shorthand for minmax(auto, 1fr), and the auto minimum is the content's minimum size. The fix is to set the minimum explicitly.
.layout {
grid-template-columns: repeat(2, minmax(0, 1fr));
}With minmax(0, 1fr), the column can shrink to zero, and the overflowing content has to wrap, scroll, or be clipped inside its own box. Pair this with overflow-wrap: anywhere on text containers or overflow-x: auto on tables to decide how that overflow should look.
Grids that choose their own column count
Sometimes the content is a set of equal items, such as product cards or articles. The design frames might show one, two, and four cards per row, but there is no reason to set each number by hand. A grid can compute the count from the width.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}Here is how the browser reads this rule:
minmax(16rem, 1fr)says each column is at least16remwide and can grow to share extra space.repeat(auto-fit, ...)creates as many columns as fit at that minimum width.- Any leftover space is shared among the columns, so the cards stretch to fill the row.
On a 360px-wide phone, one 16rem column fits (256px at a 16px root size). At 700px, two fit. At 1200px, four or five fit. No media query was written, and the layout still passes through every width in between without a jump.
auto-fit versus auto-fill
Both keywords create as many columns as fit. They differ when there are fewer items than columns.
| Keyword | Empty columns | Result with 2 cards in a wide container |
|---|---|---|
| `auto-fill` | Kept, as empty tracks | Cards stay at their column width and leave blank space on the right |
| `auto-fit` | Collapsed to zero width | Cards stretch to share the whole row |
Use auto-fit when you want items to fill the row. Use auto-fill when you want items to keep a consistent size even when there are few of them, such as a gallery that should look the same with three items or thirty.
A limit of the minimum
If the container is narrower than the minimum, a minmax(16rem, 1fr) column overflows. This happens in a narrow sidebar or on a very small screen with side padding. A safe version caps the minimum with min():
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
}min(16rem, 100%) makes the minimum the smaller of 16rem and the container width, so one column always fits.
When to use a breakpoint and when to use an intrinsic grid
Both approaches are valid. The question is whether the layout change is about the number of identical items or about the structure of the page.
| Situation | Better tool | Reason |
|---|---|---|
| A set of equal cards or tiles | `auto-fit` with `minmax()` | Column count follows the width with no breakpoints to maintain |
| Main content plus sidebar | Breakpoint with named column ratios | The sidebar's role changes, and the ratio is a design decision |
| Different column widths per frame | Breakpoint | Intrinsic sizing cannot express "2fr 1fr 1fr" at one width and "1fr 1fr" at another |
| Items that must align across rows with a fixed count | Breakpoint | `auto-fit` count can change in ways the design does not allow, for example orphaned items |
You can also combine them. A page shell can use breakpoints for the structure, and a section inside it can use an intrinsic grid for its cards.
Matching the code to the frames
Your design frames define intent, and the CSS must produce the same result at each frame's width. A practical check is to write down, for each frame, three facts: column count, column ratio, and spacing. Then the CSS has to match this record.
| Frame | Min width | Columns | Ratio | Gap and padding |
|---|---|---|---|---|
| Mobile | 0 | 1 | n/a | `1rem` |
| Tablet | `48em` | 2 | `1fr 1fr` | `1rem` |
| Desktop | `80em` | 3 | `2fr 1fr 1fr` | `1rem`, capped at `80rem` |
If a frame in the design tool uses a 12-column grid with 24px gutters, you do not need to rebuild 12 columns in CSS. Translate the intent. A sidebar that takes 4 of 12 columns becomes 2fr 1fr, and the gutter becomes gap. If you keep the gutters as design tokens, such as --space-gutter, the code and the design tool stay in sync when they change.
:root {
--space-gutter: 1rem;
}
@media (min-width: 48em) {
:root {
--space-gutter: 1.5rem;
}
}
.layout {
display: grid;
gap: var(--space-gutter);
padding: var(--space-gutter);
}Changing the token at a breakpoint updates every layout that uses it, so you edit one rule instead of many.
Common errors
- Setting a breakpoint for every device. Add a query only where the layout stops working. A two-column rule that is wrong at 700px is a bug in the content-driven breakpoint, not a reason to add a new one for each phone model.
- Using
max-widthqueries for the base layout. This reverses the mobile-first model, forces mobile to undo desktop rules, and makes cascade conflicts hard to follow. - Fixed pixel column widths. A
300pxcolumn on a 280px screen causes horizontal scrolling. Preferfr,minmax(), and percentages. - Forgetting the cap on desktop. Check the layout on a 1920px-wide window as well as on the desktop frame.
- Reordering content visually without reordering the DOM. This breaks keyboard and screen reader order.
Exercises
Exercise
A .cards grid uses repeat(auto-fit, minmax(16rem, 1fr)). The container is 1000px wide, the root font size is 16px, the gap is 1rem, and there are only 2 cards. How many columns do the cards occupy, and how wide is each one? What changes if you switch to auto-fill?
Show answer
At 1000px, the browser fits 3 columns (3 × 256px + 2 × 16px gap = 800px; a fourth would need 1088px). With auto-fit and 2 cards, the unused third column collapses to zero, so the two cards share the row. Each is (1000 − 16) / 2 = 492px wide. With auto-fill, the third track stays as an empty track, so the cards are 3 columns' width each, about 322.67px, and blank space remains at the right.
Exercise
Your mobile frame stacks a sidebar below the article, and your desktop frame shows it on the left. Why should the article come first in the HTML, and how do you place the sidebar on the left on desktop without changing the HTML?
Show answer
Keyboard focus order and screen readers follow the DOM, and the article is the primary content, so it should come first. On desktop, use a grid with a narrow first column and place the sidebar there, for example grid-template-columns: 1fr 2fr with .sidebar { grid-column: 1; grid-row: 1; } and .article { grid-column: 2; grid-row: 1; }. The visual order changes, but the DOM order is unchanged, so keep the mismatch small and test with the keyboard.