Sub topic 10
Rearranging areas with media queries
This chapter shows how to change a grid-template-areas layout at different viewport widths without touching the HTML. It assumes you can read the earlier chapters' area maps and grid-area rules, know semantic elements such as header, main, aside and footer, and have written a basic @media (min-width: ...) rule.
The idea: the map is the layout
With named areas, the arrangement of the page lives in one place: the grid-template-areas value on the container. The children only say which name they belong to. If each child has a grid-area name, you can rearrange the page by replacing the map. You do not need to move elements, change their classes, or rewrite their placement rules.
That gives a clean split of responsibilities:
- The
.page > header { grid-area: header; }rules assign elements to names. They stay the same at every width. - The container's
grid-template-columnsandgrid-template-areasdescribe the shape. These are what a media query overrides.
A mobile-first base layout
Start with the narrowest layout as the default and add rules as the screen widens. This is the mobile-first approach, and it uses min-width queries.
The HTML is the same structure used throughout this tutorial:
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>The base CSS stacks everything in one column. The map has one name per row, so each area is a full-width row:
.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; }Notice that the stacked order is header, main, sidebar, footer. In the HTML the aside comes before the main. The map reorders them visually, which the last section of this chapter returns to.
Swapping in the wide layout
At 48rem and wider, replace both the column list and the map:
@media (min-width: 48rem) {
.page {
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}Only the .page rule appears inside the query. The four grid-area rules outside it still apply, so aside still means "sidebar", but now the name points to a different cell.
What a media query replaces
A media query does not merge with the base declaration. Each property is replaced as a whole value by the later, matching rule, following the normal cascade. This has three practical effects.
- Replace the map and the columns together. The map in the query must be complete. You cannot override only one row.
- Match the column count. The area map defines how many columns the explicit grid has. If the map has two names per row, there are two columns.
- Keep the query after the base rule. A media query adds no specificity. If the base
.pagerule comes later in the stylesheet, it wins over the query. Put the query after the rule it overrides.
The table lists what happens when the columns list and the map disagree:
| Situation | Result |
|---|---|
| Map has 2 columns, `grid-template-columns` lists 2 tracks | Works as intended. |
| Map has 2 columns, `grid-template-columns: 1fr` only | Valid. The second column is sized by `grid-auto-columns`, which is `auto` by default, so it is not the width you meant. |
| Map has 1 column, `grid-template-columns: 220px 1fr` | Valid. The second track exists but has no named area in it. Items placed by name fall into the first column. |
| Rows in the map have different numbers of names | The whole declaration is invalid and the browser ignores it. |
The last row is the rule from the earlier chapter on valid area definitions. Because the declaration is dropped as a whole, a typo in a media query map does not break the page by half: the browser keeps whatever map applied before. That can make the bug look like "my media query does nothing."
Adding a second breakpoint
Breakpoints can stack. Here a third layout moves the sidebar to the right side on very wide screens:
@media (min-width: 64rem) {
.page {
grid-template-columns: 1fr 240px;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
}The min-width: 48rem rule still applies at this width, because the viewport matches both queries. The 64rem rule comes later, so its grid-template-columns and grid-template-areas win. Declare queries in ascending order of width, or the narrower one will override the wider one.
Because the new rule replaces the columns too, you must write them again. Swapping only the map would leave 220px 1fr, and the sidebar would end up in the 1fr column on the right.
Using empty cells
A period (.) in a map marks an empty cell that no area occupies. It is useful when a breakpoint needs a spacer, such as a margin column on large screens:
@media (min-width: 80rem) {
.page {
grid-template-columns: 1fr 220px minmax(0, 60rem) 1fr;
grid-template-areas:
". header header ."
". sidebar main ."
". footer footer .";
}
}The two outer 1fr columns absorb extra width, which centres the content block. The rules for a valid area still apply. Each name must form a rectangle, and every row must have four entries. The . cells may repeat freely, and consecutive dots in one row can be written as ....
Complete example
This page runs as shown. It combines the base layout and the two breakpoints, with enough styling to see the areas:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive areas</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; }
.page {
display: grid;
gap: 1rem;
padding: 1rem;
min-height: 100vh;
grid-template-columns: minmax(0, 1fr);
grid-template-rows: auto 1fr auto auto;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
.page > header { grid-area: header; background: #cfe8ff; padding: 1rem; }
.page > aside { grid-area: sidebar; background: #ffe9b8; padding: 1rem; }
.page > main { grid-area: main; background: #d9f5d3; padding: 1rem; }
.page > footer { grid-area: footer; background: #e6e6e6; padding: 1rem; }
@media (min-width: 48rem) {
.page {
grid-template-columns: 220px minmax(0, 1fr);
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}
@media (min-width: 64rem) {
.page {
grid-template-columns: minmax(0, 1fr) 240px;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
}
</style>
</head>
<body>
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>
</body>
</html>Two details differ from the earlier snippets. grid-template-rows is also redefined in each query, because the stacked layout has four rows and the wide layouts have three. The base also has no grid-template-rows count to match, since the map decides the number of rows, but listing sizes gives main the spare height. minmax(0, 1fr) is used instead of plain 1fr because a plain 1fr track will not shrink below the minimum width of its content, so a long unbroken string in main can force horizontal overflow.
Pitfalls
- Units in queries. In a media query,
remandemrefer to the browser's initial font size, usually 16px, not to afont-sizeyou set onhtml. So48remmeans 768px at the default setting, and it moves if a user raises their browser's default text size. That is a benefit for accessibility. - Hard-to-read maps. Align the names in columns, as in the examples. A visual grid in the source makes row-length mistakes easy to spot.
- Resetting. To remove the map at some width, set
grid-template-areas: none. Children keep theirgrid-areanames, but with no matching area they are auto-placed instead. - Too many breakpoints. Each additional map is a full copy of the layout to maintain. For card grids, prefer
repeat(auto-fit, minmax(...)), which needs no queries. Use area maps for the page structure, where regions really do change position.
Source order and visual order
The base map places main above sidebar, but the HTML has aside before main. At narrow widths, a sighted user sees the main content first, while a keyboard user tabbing through links or a screen reader user hears the sidebar first, because both follow the DOM order, not the visual order.
Whenever a map puts elements in a different sequence than the HTML, check that the DOM order still makes sense on its own. The safest approach is to write the HTML in the order you want read, such as header, main, aside, footer, and use the maps to position the regions visually. The grid-area rules and the maps in this chapter work the same way with that HTML order.
Exercise
A page has a base rule with grid-template-areas: "header" "main" "footer" and grid-template-columns: 1fr. Inside @media (min-width: 48rem) you write grid-template-areas: "header header" "nav main" "footer"; and keep grid-template-columns: 1fr. The layout never changes at 48rem. Give two reasons it fails and write a corrected media query for a 200px nav column beside main.
Show answer
First, the new map is invalid: its rows have different numbers of names (2, 2, and 1), so the browser ignores the whole declaration and keeps the one-column map. Second, even with a valid map, grid-template-columns: 1fr lists only one track, so the second column would be sized by grid-auto-columns (auto), not as intended. A corrected query is @media (min-width: 48rem) { .page { grid-template-columns: 200px minmax(0, 1fr); grid-template-areas: "header header" "nav main" "footer footer"; } }. The query must also come after the base .page rule in the stylesheet.
Exercise
You need the sidebar on the left between 48rem and 64rem and on the right at 64rem and wider, using the same HTML. Which rules change between the two ranges, and which stay the same?
Show answer
Only the container's grid-template-columns and grid-template-areas change. The map goes from "sidebar main" to "main sidebar", and the column list goes from 220px 1fr to 1fr 240px (or similar) so the fixed width follows the sidebar. The grid-area rules on the children stay the same, and the 64rem query must come after the 48rem query so it wins at wide widths.