Sub topic 4
Placing elements with grid-area
This chapter assumes you know basic CSS selectors, the box model, and HTML semantic elements such as header, main, aside, and footer. It also assumes you have already seen how grid-template-areas names regions on a grid container. Here you will connect those names to real elements.
What grid-area does
A grid container that declares grid-template-areas only defines named regions. It does not decide which element goes where. That is the job of the grid-area property, which you set on each child (a grid item).
The value of grid-area is a custom identifier. It must match a name that appears in the container's grid-template-areas. When the names match, the browser places the item so that it covers every cell carrying that name.
A worked example
Start with a plain semantic structure.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>Next, define the grid on the container, then assign each child to an area.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }Read the area map one quoted string at a time. Each string is a row, and each name is a cell. The name header appears in both cells of row one, so the header spans both columns. The same is true of footer in row three. Row two holds sidebar in the 220px column and main in the flexible 1fr column.
Notice that the area names do not have to match the element names. aside is assigned to sidebar, because the name is a label you chose in the map.
How the matching works
The match is by name only. The order of children in the HTML does not matter for placement, and you can write the four rules above in any order.
Common errors
- A typo in the name. If
grid-area: sidbarhas no matching name in the map, the browser does not place the item in any named region. Instead, it creates implicit lines with that name and the item falls back to auto-placement, so it usually lands in an unexpected cell. - Quoting the name. In the child rule, write
grid-area: main, notgrid-area: "main". Quotes belong only insidegrid-template-areas. - Forgetting
display: grid. Without it on the container,grid-template-areasandgrid-areahave no effect. - Selecting the wrong elements. The
.page > headerselector uses the child combinator, so only direct children are placed. Aheadernested deeper insidemainis not affected.
Unplaced items and shorthand
Any child you do not assign to an area is auto-placed into the next free cell. That can be useful for extra content, but it can also push things around, so assign every child that matters.
grid-area is also a shorthand for line-based placement. With numbers it takes row-start / column-start / row-end / column-end, for example grid-area: 1 / 1 / 2 / 3. This tutorial uses the named form because it is easier to read, and the area map shows the layout at a glance.
Exercise
A container has grid-template-areas: "nav nav" "side content";. You write .menu { grid-area: navigation; } on a child, and the menu does not appear in the top row. What is wrong, and how do you fix it?
Show answer
The name navigation does not appear in the area map, which uses nav. Change the rule to grid-area: nav; (or rename the area in the map to navigation). With the matching name, the menu spans both cells of the top row.
Exercise
Using the page example above, which single change moves the sidebar to the right of the main content without editing the HTML?
Show answer
Edit the map so row two reads "main sidebar", and change grid-template-columns to 1fr 220px so the wide column is on the left. The grid-area rules stay the same, because they refer to names, not positions.