Sub topic 10
Rearranging areas with media queries
This chapter assumes you know basic CSS selectors, the box model, grid-template-areas, grid-area, and how to write a media query. It shows how to change a page layout by redefining the area map at a breakpoint, without touching the HTML or the placement rules.
Why redefine areas
The grid-area rules assign each element to a name. They say nothing about where that name sits on the page. The map in grid-template-areas decides that. So you can move the whole layout by replacing the map and the column tracks inside a media query.
This works well because the placement rules stay in one place. You change one rule on the container, and every child follows.
Start with the HTML and placement rules
The markup and the grid-area rules from earlier chapters stay as they are:
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }Mobile first: one column
Write the narrow layout as the default. It uses one column, and each row holds one area name.
.page {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}The sidebar sits after main here, so on a phone the main content comes first.
Widen the layout with a media query
At a wider viewport, replace both the column tracks and the area map:
@media (min-width: 48rem) {
.page {
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}Two things must change together. The map now has two columns per row, so grid-template-columns needs two tracks. If you changed only the map, the grid would have a mismatch between the map and the tracks. In practice the extra column would be created implicitly with auto width, which gives a layout you did not plan.
The 48rem breakpoint is 768px at the default font size. Using rem makes the breakpoint follow the user's font-size setting.
Common errors
- Changing the map but not the tracks. The number of names per row should equal the number of column tracks. Update both.
- Writing an invalid map in the query. The rules from the validity chapter still apply. Every row needs the same number of cells, and each repeated name must form a rectangle. If the map is invalid, the browser drops the whole
grid-template-areasdeclaration. Because the declaration is ignored, the earlier mobile map stays active and the layout silently does not change. - Putting
min-widthqueries before the default. Media queries add no specificity. A later rule with the same selector wins, so the query must come after the base.pagerule. - Moving
grid-areainto the query for no reason. Leave placement alone unless an element needs a new name.
Try a third layout
You can add more breakpoints the same way. This one moves the sidebar to the right on large screens:
@media (min-width: 70rem) {
.page {
grid-template-columns: 1fr 260px;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
}Only the map and tracks change. The four grid-area rules are untouched.
Exercise
A developer adds a media query that changes grid-template-areas to a two-column map ("header header" / "sidebar main" / "footer footer") but forgets grid-template-columns, which is still 1fr. What is wrong, and how do you fix it?
Show answer
The map has two columns but only one explicit track is defined, so the second column is created implicitly with auto sizing and the sidebar width is not controlled. Fix it by setting grid-template-columns: 220px 1fr; inside the same media query, so the number of tracks matches the number of names per row.
Exercise
Your wide-layout map reads "header header" / "sidebar main" / "footer". The layout does not change at the breakpoint. Why?
Show answer
The last row has one cell while the others have two, so the rows are uneven and the map is invalid. The browser ignores the whole declaration and keeps the mobile map. Change the last row to "footer footer".