Sub topic 4
Placing elements with grid-area
This chapter assumes you can write basic CSS selectors, know the semantic elements header, main, aside and footer, and have already seen display: grid with grid-template-columns. The previous chapter showed how grid-template-areas draws a map of named regions. Here you connect real elements to those regions.
What the map does on its own
A grid-template-areas value only names regions. It does not move anything. Until an element says which region it belongs to, the browser places it by the default auto-placement rules: one item per cell, row by row, in source order.
Here is the HTML used throughout this chapter.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>Here is the map and the placement rules together.
.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; }The container defines the map. Each child declares its own grid-area, and the browser looks up that name in the map and places the child in the matching cells.
The name is an identifier, not a selector or a string
The value of grid-area is a custom identifier. Three details cause most early mistakes.
- Do not quote it.
grid-area: sidebar;is valid.grid-area: "sidebar";is not valid, so the browser ignores the declaration. Quotes belong only ingrid-template-areas, where each quoted string is one row. - It is case-sensitive.
Sidebarandsidebarare different names. - It does not have to match the element. An
asidecan be placed in an area callednavorleft. Choose names that describe the role of the region, not the tag, so the CSS still makes sense if you change the markup.
The same name appears in two places, so a typo in either one fails silently. If grid-area names a region that does not exist in the map, the browser does not report an error. As the next section explains, the item falls back to a different kind of placement.
What happens to items without a match
Suppose you add a navigation element to the page and forget to give it an area.
<div class="page">
<header>Header</header>
<nav>Links</nav>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>With the CSS above, nav has no grid-area. The browser places the four items that have a definite area first. The six cells of the map are then all taken, so the unplaced nav goes into a new implicit row below the footer, in the first column. That row is sized automatically. Its height comes from its content, not from any grid-template-rows value.
The same thing happens when you misspell a name. If the rule says grid-area: sidebr, the browser finds no matching name, so the declaration does not point at any region. The sidebar then behaves like any other unplaced item. Symptoms to look for are an element sitting below the whole layout, or one that overlaps nothing but appears in the wrong cell.
You can check this in your browser's developer tools. Turn on the grid overlay for the container. Named areas are shown, and you can see which cells each item occupies.
Two items in one area
Nothing stops two elements from sharing an area. They are both placed in the same cells and stack on top of each other, in source order unless z-index says otherwise.
.page > header,
.page > .banner {
grid-area: header;
}
.page > .banner {
z-index: 1;
align-self: end;
}This is sometimes useful, for example to put a caption over an image. It is more often a mistake, such as copying a rule and forgetting to change the name. If two items overlap unexpectedly, search your CSS for a repeated area name.
The reverse also works. One item can fill a region that spans many cells, and that is the reason to repeat a name in the map. In the layout above, header appears twice in its row, so the header element is stretched across both columns. You do not need to set grid-column: span 2 on the header.
grid-area is a shorthand for four lines
The name form is only one way to use grid-area. The property is a shorthand for grid-row-start, grid-column-start, grid-row-end and grid-column-end, written in that order and separated by slashes.
.page > aside {
grid-area: 2 / 1 / 3 / 2;
}This means the item starts at row line 2 and column line 1, and ends at row line 3 and column line 2. That is the cell in the second row, first column, which is where the map placed the sidebar. Grid lines are numbered starting at 1, and the end line is not part of the item. A cell in row 2 sits between line 2 and line 3.
The two forms differ in what they depend on.
| Form | Example | Depends on |
|---|---|---|
| Area name | `grid-area: sidebar;` | The names in `grid-template-areas` |
| Line numbers | `grid-area: 2 / 1 / 3 / 2;` | The count and order of rows and columns |
Names keep working when you rearrange the map. Line numbers break when you insert a row or column, because every number after the insertion point shifts. For page layouts, prefer names. Line numbers are better for one-off placements, such as making a single card span the full width of a card grid.
You can also mix the forms in a longhand property, for example grid-column: 1 / -1;. The value -1 counts from the last explicit line, so the item spans every explicit column.
Placement does not change source order
grid-area moves an element visually. It does not move it in the DOM. The tab order, the reading order for screen readers, and the order of text when styles are turned off all still follow the HTML.
In the example above the HTML order is header, sidebar, main, footer, and the visual order is the same. Now imagine a design that shows the sidebar to the right of the main content. You could reorder the map without touching the markup, and keyboard focus would still visit the sidebar before main content. That can confuse people who navigate by keyboard, because focus jumps around the screen in a different order from the one they see.
Specificity and where to put the rules
Because grid-area is an ordinary declaration, normal cascade rules apply. The selector .page > header has specificity of one class and one element. If another rule with higher specificity sets grid-area on the same element, it wins and your placement seems to ignore the map. Keep placement rules at one level of specificity, close to the map they refer to.
The child combinator > in these examples limits each rule to direct children of .page. This matters because grid placement only applies to direct children of a grid container. A header nested inside an article is not a grid item of .page, and grid-area: header on it does nothing. If you need nested elements to participate, make the nested element a grid container too, which is how the final chapter of this tutorial combines layouts.
Common errors, in one place
- The value is quoted (
grid-area: "main"). Remove the quotes. - The name differs from the map by case or spelling. Compare the two character by character.
- The rule targets an element that is not a direct child of the grid container.
- A later rule with higher specificity overrides the placement.
- Two elements share a name and overlap.
- An element with no
grid-arealands in a new implicit row below the layout. - Line numbers were used and a row or column was added afterward.
Exercise
A page uses grid-template-areas with the names "header", "sidebar", "main" and "footer". A developer writes .page > aside { grid-area: Sidebar; } and the sidebar appears in a new row under the footer. Give two facts about grid-area that explain this, and say how to fix it.
Show answer
Area names are case-sensitive, so Sidebar does not match sidebar in the map. Because no region matches, the aside has no definite position, and the browser auto-places it in the first free cell, which is in a new implicit row after the footer because all the explicit cells are taken. Change the value to sidebar so that it matches the map exactly.
Exercise
Write a grid-area declaration using line numbers that places an item in the second row and second column of a grid, covering exactly one cell. Then state one reason to prefer a name over line numbers for a page layout.
Show answer
grid-area: 2 / 2 / 3 / 3; (row-start / column-start / row-end / column-end). Names keep working when rows or columns are added or reordered in the map, while line numbers all shift and must be rewritten.