Sub topic 11
Combining a Named-Area Page with an Auto-Fit Card Grid
This chapter joins two techniques from earlier chapters into one layout. The page frame uses grid-template-areas. The card list inside the main region uses repeat(auto-fit, minmax()). I assume you already know how to write and place named areas with grid-area, how auto-fit differs from auto-fill, how min(100%, Npx) prevents overflow, and how to redefine areas in a media query. The new material is how the two grids interact, and what goes wrong when they don't.
The working model: two grids with separate jobs
A named-area grid and an auto-fit grid solve different problems. Areas decide where whole regions sit on the page. Auto-fit decides how many columns of repeated, similar items fit in a given width. Trying to do both in one grid usually fails. The page grid's tracks (for example 220px 1fr) are fixed by the area map, so the cards would have to share those tracks.
The solution is nesting. One element, main, plays two roles:
- It is a grid item of the page grid, placed by
grid-area: main. - It is also a grid container for its own children, through
display: grid.
These roles are independent. The page grid's gap and columns never touch the cards. The card grid's gap and columns never touch the sidebar.
| Level | Container | Job | Technique |
|---|---|---|---|
| Page | `.page` | Place header, sidebar, main, footer | `grid-template-areas`, media query |
| Content | `.page > main` | Fit as many cards per row as the width allows | `repeat(auto-fit, minmax(...))` |
Step 1: Write the markup
The cards are children of main, not of .page. That nesting is what lets main become their grid container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grid layout</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="page">
<header>Header</header>
<main>
<article class="card">Card 1</article>
<article class="card">Card 2</article>
<article class="card">Card 3</article>
<article class="card">Card 4</article>
<article class="card">Card 5</article>
</main>
<aside>Sidebar</aside>
<footer>Footer</footer>
</div>
</body>
</html>The viewport meta tag matters. Without it, mobile browsers pretend the screen is about 980px wide, and neither auto-fit nor your media query will behave as you expect on a phone.
Step 2: Define the page grid
Start with the narrow layout, then widen it. The base rules describe a single column. The media query swaps in the two-column area map.
.page {
display: grid;
gap: 1rem;
grid-template-columns: minmax(0, 1fr);
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
.page > header { grid-area: header; }
.page > main { grid-area: main; }
.page > aside { grid-area: sidebar; }
.page > footer { grid-area: footer; }
@media (min-width: 48rem) {
.page {
min-height: 100vh;
grid-template-columns: 220px minmax(0, 1fr);
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}Three details here are easy to miss:
- The
grid-arearules appear once, outside the media query. Only the map changes, because items refer to area names, not positions. - The wide layout has three rows, so
grid-template-rows: auto 1fr autolines up with them. The middle row takes the leftover height, which pins the footer to the bottom on short pages. - Instead of a bare
1fr, the flexible track isminmax(0, 1fr). The reason is covered in the gotchas section below.
Step 3: Turn main into a card grid
.page > main {
grid-area: main;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
gap: 1rem;
align-content: start;
}
.card {
padding: 1rem;
border: 1px solid #ccc;
border-radius: 0.5rem;
}Read the main rule as two groups:
grid-area: mainis aboutmainas a child of.page.display: gridand everything after it is aboutmainas a parent of the cards.
The browser measures the width of main, not the viewport. At the 48rem breakpoint, the sidebar takes 220px and a gap takes 1rem. The cards therefore get a narrower space than the screen width suggests. auto-fit adapts to that space automatically. This is the main benefit of nesting: the card grid responds to its container, so you don't need to recompute breakpoints when the sidebar width changes.
Why `align-content: start` is there
In the wide layout, the middle row is 1fr, so it can be taller than the cards need. A grid container's rows default to align-content: normal, which behaves like stretch for auto-sized rows. Without start, the card rows would grow to absorb the extra height, and the cards would become unnaturally tall. Setting align-content: start packs the rows at the top at their natural height.
Behavior to expect when resizing
- On a narrow screen, the page is one column: header, main, sidebar, footer. Inside main,
min(100%, 220px)lets a single card shrink below 220px if the container is narrower. - Past 48rem, the sidebar appears on the left. The cards redistribute into as many columns as fit in the remaining width.
- With only two cards in a wide main,
auto-fitcollapses the empty tracks, so the two cards stretch across the row. If you would rather keep cards near 220px with blank space on the right, changeauto-fittoauto-fillin that one rule.
/* Keeps card widths stable when there are few cards */
.page > main.stable {
grid-template-columns: repeat(auto-fill, minmax(min(100%, 220px), 1fr));
}Gotchas and common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cards stack in one column at every width | `main` has `grid-template-columns` but no `display: grid` | Add `display: grid` to `main`. Without it the columns property does nothing. |
| Horizontal scrollbar on phones | `minmax(220px, 1fr)` with no `min()` | Use `minmax(min(100%, 220px), 1fr)`. |
| The main column grows past the viewport when a child is wide | A bare `1fr` is `minmax(auto, 1fr)`, so a wide child such as a `pre` block raises the minimum | Use `minmax(0, 1fr)` on the page track, or set `min-width: 0` on `main`. |
| Cards are tall and spaced oddly | `main` is stretched by a tall row, and its rows stretch with it | Add `align-content: start`. |
| An element drops below the layout in a new row | Its `grid-area` name is missing from the map, or the map is invalid (non-rectangular, uneven row lengths) | Check spelling. Verify that each name forms one rectangle and every row has the same number of cells. |
| Sidebar is hidden or squeezed | The element order or area map was changed in only one breakpoint | Make sure the base map and the media query map each list every area. |
An item whose grid-area name doesn't exist in the map doesn't produce an error. The browser treats it as a request for a named line that isn't there, creates implicit tracks, and places the item in a surprising spot. If a region lands in the wrong place, check the spelling first.
Source order and accessibility
Keyboard focus and screen readers follow the HTML order, not the visual order. The markup above places main before aside. This matches the narrow layout, where the main content comes first. On wide screens the sidebar appears on the left, but it is still later in the tab order. That mismatch is small and common, but it is still a mismatch. If the sidebar holds important navigation, consider whether the order a keyboard user experiences still makes sense.
Placing elements with areas does not change the DOM, so grid-area is not a way to fix poor markup order. Write the document in a logical reading order first, then use areas to arrange it visually.
Summary checklist
- Put the cards inside the element that owns the area, not directly in
.page. - Give that element both
grid-areaanddisplay: grid. - Use
repeat(auto-fit, minmax(min(100%, Npx), 1fr))for the cards. - Use
minmax(0, 1fr)for flexible page tracks that hold wide content. - Set
align-content: startif the area's row can be taller than its content. - Keep the HTML order logical, and let the media query change only the area map and tracks.
Exercise
A developer writes this rule, but the cards always stack in one column, even on a wide screen. .page > main { grid-area: main; grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); gap: 1rem; } What is missing, and why does the page layout still work?
Show answer
The rule is missing display: grid. The page layout still works because grid-area only requires main to be a child of the grid container .page, which is already display: grid. The columns property applies only to elements that are themselves grid containers, so on main it has no effect and the cards stay as normal block children.
Exercise
You want the cards in main to stay about 220px wide even when there are only two of them in a very wide main. Which single keyword do you change, and which nested rule do you leave alone?
Show answer
Change auto-fit to auto-fill in the card grid's repeat(). auto-fill keeps the empty tracks, so two cards stay near 220px. Leave the page grid's grid-template-areas and the grid-area rules alone, because the card grid is independent of the page grid.
Exercise
On narrow screens a long code block inside one card makes the whole page scroll sideways, although every grid uses min() or fr. Which page-level track definition is the most likely cause, and what replaces it?
Show answer
A bare 1fr in the page's grid-template-columns, which means minmax(auto, 1fr) and lets the code block's minimum content width set the track's minimum. Replace it with minmax(0, 1fr), or add min-width: 0 to main.