Sub topic 17
Putting it together: common layout patterns
This chapter combines the techniques from earlier chapters into four layouts you will build often: a page shell, a responsive card grid, a dense gallery, and a nested grid inside a card. It assumes you know grid-template-columns, fr, repeat(), minmax(), gap, named areas, and the basics of explicit and implicit grids.
How to choose a pattern
Start from the problem, then pick the Grid feature that solves it.
| Problem | Main tools |
|---|---|
| Fixed page regions (header, sidebar, main, footer) | `grid-template-areas`, `fr`, `auto` rows |
| A list of similar cards that reflows with width | `repeat(auto-fit, minmax())` |
| Items of different sizes packed tightly | `grid-auto-rows`, `grid-auto-flow: dense`, spans |
| Content inside a grid item that needs its own columns | Nested grid |
Pattern 1: the page shell
The classic page has a full-width header and footer, with a sidebar beside the main content. Named areas let you draw the layout in the CSS.
.layout {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.layout > header { grid-area: header; }
.layout > aside { grid-area: sidebar; }
.layout > main { grid-area: main; }
.layout > footer { grid-area: footer; }The middle row uses 1fr. With min-height: 100vh on the container, that row takes all leftover height, so the footer sits at the bottom of the viewport even when the content is short. The header and footer rows are auto, so they are only as tall as their content.
Making the shell responsive
On narrow screens, replace the area map with a single column. Only the template changes. The grid-area assignments on the children stay the same.
@media (max-width: 700px) {
.layout {
grid-template-columns: 1fr;
grid-template-rows: auto auto 1fr auto;
grid-template-areas:
"header"
"sidebar"
"main"
"footer";
}
}Note that the row list now has four entries to match the four area strings. A mismatch between the number of rows in grid-template-areas and grid-template-rows is not an error. Extra area rows still create tracks, but your 1fr row may end up on the wrong region, so keep the two in step.
Pattern 2: the responsive card grid
When you have a list of similar cards, you usually want as many columns as fit, without writing a breakpoint for each width.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.5rem;
}Read the column definition from the inside out:
minmax(240px, 1fr)means each column is at least 240px wide and can grow to an equal share of the free space.repeat(auto-fit, ...)creates as many of those columns as fit in the container.- Leftover space is shared by the columns, so the row is always filled.
auto-fit versus auto-fill
The two keywords differ only when there are fewer items than the row could hold. auto-fill keeps the empty tracks, so items stay at their column width and blank space remains on the right. auto-fit collapses empty tracks to zero width, so the existing items stretch to fill the row.
For a card list, auto-fit is usually what you want. Use auto-fill when you need items to keep a consistent width regardless of how many there are, for example a tag list that should line up with other rows.
A common error: minimum wider than the container
If the container is narrower than 240px, such as on a very small phone with padding, the column cannot shrink below its minimum and the grid overflows horizontally. A fix is to cap the minimum with min().
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr));
gap: 1.5rem;
}Here min(240px, 100%) picks the smaller of the two values, so the column never exceeds the container width.
Pattern 3: the dense gallery
A gallery mixes tall, wide, and standard tiles. Use a fixed row height, let spans create the variety, and enable dense packing.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
grid-auto-flow: row dense;
gap: 8px;
}
.gallery .wide { grid-column: span 2; }
.gallery .tall { grid-row: span 2; }No rows are defined in grid-template-rows. Every row comes from the implicit grid, so grid-auto-rows: 120px sets their height. A .tall tile spans two 120px rows plus the 8px gap between them, which makes it 248px high.
Without dense, the auto-placement algorithm never moves backward. If a wide tile does not fit in the space left on a row, it jumps to the next row and leaves a hole. With dense, later, smaller items are allowed to fill those holes.
Pattern 4: a nested grid inside a card
A grid item can itself be a grid container. This is useful when a card has its own internal structure, such as an image, a title, and a price row.
.product-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 1.5rem;
}
.product {
display: grid;
grid-template-columns: 80px 1fr;
grid-template-areas:
"thumb title"
"thumb price";
gap: 0.25rem 1rem;
align-items: center;
}
.product img { grid-area: thumb; width: 100%; }
.product h3 { grid-area: title; margin: 0; }
.product .price { grid-area: price; }The outer grid decides how many cards appear per row. The inner grid arranges each card's contents. The two grids are independent: the inner tracks do not line up with the outer ones. If you need inner rows to align across neighboring cards, that is the job of subgrid, covered in the nested grids chapter. A note on that: browser support for subgrid is now broad, but check your target browsers before depending on it.
Also note the shorthand gap: 0.25rem 1rem. The first value is the row gap and the second is the column gap.
Combining patterns
Real pages mix these patterns. The shell uses named areas, the main region holds an auto-fit card grid, and each card may be a small grid of its own.
To place the card grid inside main, give main no display of its own and put the cards in a child container. Alternatively, make main the grid container directly:
.layout > main {
grid-area: main;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.5rem;
align-content: start;
}align-content: start matters here. The main area stretches to fill the 1fr row. Without it, the card rows would stretch to share the extra height, and the cards would grow taller than their content.
Common errors
- Content overflows a
1frcolumn.1fris shorthand forminmax(auto, 1fr), and theautominimum means a track will not shrink below its content, such as a long word or a wide image. Useminmax(0, 1fr)to allow shrinking, and handle overflow in the item. - Items overlap unexpectedly. Two items with explicit placement can occupy the same cell. Grid does not prevent this. Check line numbers and area names.
- Unwanted empty rows. A
grid-template-areasmap with more rows than you have content creates empty tracks. Match the map to your design. - Gaps look doubled.
gaponly applies between tracks. If you also add margins to items, the spacing adds up. Pick one mechanism. - Cards have uneven heights. Grid items stretch by default (
align-items: stretch), so cards in a row match in height. If they do not, check whether something setalign-selforalign-itemsto another value.
Exercises
Exercise
You want a card list that fills each row, uses columns at least 200px wide, and never overflows a 150px-wide container. Write the grid-template-columns value.
Show answer
repeat(auto-fit, minmax(min(200px, 100%), 1fr)). The min() function limits the minimum column width to the container width, so the column shrinks when the container is narrower than 200px.
Exercise
A photo wall uses grid-auto-flow: row dense. A user tabs through the photos with the keyboard and finds the focus jumps around the screen. Why does this happen, and when is dense a poor choice?
Show answer
Dense packing changes where items appear on screen but not their order in the HTML. Keyboard focus follows the HTML order, so focus can jump between visually distant tiles. dense is a poor choice when order carries meaning, such as ranked lists or step-by-step instructions.
Exercise
In the page shell, the footer is not at the bottom of the viewport when the page has little content. Which two declarations on .layout produce the correct behavior, and why?
Show answer
min-height: 100vh makes the container at least as tall as the viewport, and grid-template-rows: auto 1fr auto gives all the leftover height to the middle row. The footer row is only as tall as its content, so it sits at the bottom of the container.