Sub topic 11
Combining a Named-Area Page with an Auto-Fit Card Grid
This chapter joins the two techniques from earlier chapters. It assumes you can write grid-template-areas, assign elements with grid-area, and use repeat(auto-fit, minmax()). It also assumes you know basic media queries.
The idea: two grids, two jobs
The page layout and the card layout solve different problems.
- The page grid places a few large regions (header, sidebar, main, footer). The number of regions is fixed, so named areas suit it.
- The card grid places an unknown number of similar items. The count changes, so
auto-fitsuits it.
Grid is not inherited by descendants. Only the direct children of a grid container become grid items. To use a second grid inside main, make main a grid container too. This is called nesting.
The markup
The cards must be direct children of main.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>
<article class="card">One</article>
<article class="card">Two</article>
<article class="card">Three</article>
</main>
<footer>Footer</footer>
</div>The outer grid
Start with the mobile-first page layout. One column stacks the areas. At 48rem, the media query swaps in the two-column map.
.page {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
@media (min-width: 48rem) {
.page {
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}The media query is the only one in the whole layout. It controls the large-scale arrangement, and the cards need none.
The inner grid
Now turn main into a grid container. Its grid-area: main still applies, because that property positions main inside .page. The display: grid property affects main's own children.
.page > main {
grid-area: main;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
gap: 1rem;
align-content: start;
}Each property has a reason:
min(100%, 220px)stops overflow when themainarea is narrower than 220px.align-content: startpacks the card rows at the top. Without it, the rows can stretch to fill a tallmainarea, for example when the sidebar is long.gaponmainis separate fromgapon.page. Each grid controls its own spacing.
Common problems
- Cards ignore the grid. A wrapper element between
mainand the cards makes the wrapper the only grid item. Put the grid on the wrapper, or remove it. - A heading becomes a card. An
<h1>insidemainis also a grid item and takes one column. Give itgrid-column: 1 / -1so it spans the full row. - The page scrolls sideways at wide content. A
1frtrack has an automatic minimum size, so a wide child such as a long<pre>can push themaincolumn wider than the viewport. Write the page column asminmax(0, 1fr)to let it shrink. - Unexpected column count. The card grid measures the width of
main, not the viewport. When the sidebar appears,maingets narrower, so you may see fewer card columns at the same viewport width than before.
Check the result
Resize the browser. At narrow widths, the areas stack and the cards form one column. Past 48rem, the sidebar moves beside main. Inside main, the card count follows the space left over after the sidebar takes its 220px.
Exercise
A page uses .page > main { grid-area: main; } and cards placed directly inside main, but the cards stack in a single column at every width. What is missing, and what declarations fix it?
Show answer
main is not a grid container, so its children are laid out as normal block content. Add display: grid and grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)) to .page > main. Adding gap: 1rem is also recommended.
Exercise
At 1200px viewport width, the card grid shows fewer columns than a standalone card grid of the same viewport width would. Why?
Show answer
The card grid sizes its columns from the width of the main area, not the viewport. The 220px sidebar and the gap take part of the width, so main is narrower and fewer 220px columns fit.