Sub topic 12
Implicit named lines created by areas
This chapter assumes you can write a grid-template-areas map and place items with grid-area, as covered in the earlier chapters. You should also be comfortable with numbered grid lines such as grid-column: 1 / 3.
Areas also name lines
A grid area is a rectangle bounded by four grid lines. When you declare an area, the browser gives each of those lines a name automatically. For an area called sidebar, it creates:
sidebar-start, the line on the start edge of the area.sidebar-end, the line on the end edge of the area.
The same two names exist on both axes. Which line a name refers to depends on the property you use it in. In grid-column it means a vertical line, and in grid-row it means a horizontal line.
These lines are called implicit named lines. You never write them. They exist because the area exists.
Seeing the lines
Take the page layout from earlier chapters:
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
}This grid has three column lines and four row lines. The table shows which implicit names each line gets.
| Area | Column lines (start / end) | Row lines (start / end) |
|---|---|---|
| header | 1 / 3 | 1 / 2 |
| sidebar | 1 / 2 | 2 / 3 |
| main | 2 / 3 | 2 / 3 |
| footer | 1 / 3 | 3 / 4 |
A single line can carry several names. Column line 2 is both sidebar-end and main-start. Column line 1 is header-start, sidebar-start, and footer-start.
Using the names
You can use these names anywhere a line number is allowed. This lets you place an element relative to areas without counting lines. For example, you can cover the sidebar and main areas together with an overlay:
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
<div class="overlay">Maintenance notice</div>
</div>.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; }
.overlay {
grid-column: sidebar-start / main-end;
grid-row: sidebar-start / main-end;
background: rgb(255 255 255 / 0.9);
z-index: 1;
}The overlay spans from the left edge of the sidebar to the right edge of main, and from the top of the sidebar to the bottom of main. If you later change the column widths or add a row inside the middle band, the overlay still covers those two areas. Numbers like 2 / 4 would break in that case.
Because the overlay sits in the same cells as the sidebar and main, the items overlap. z-index: 1 puts the overlay on top.
A shortcut with one name
If you give grid-column or grid-row a bare name and no line has exactly that name, the browser looks for name-start and name-end. So these two declarations are equivalent:
.toolbar {
grid-column: main-start / main-end;
}
.toolbar {
grid-column: main;
}This is also why grid-area: sidebar works. It resolves sidebar to the area's four lines.
The reverse direction
The mechanism works the other way too. If you name lines yourself with the -start and -end suffixes, the browser creates an area from them:
.layout {
display: grid;
grid-template-columns: [content-start] 1fr [content-end];
grid-template-rows: [content-start] auto [content-end];
}
.layout > article {
grid-area: content;
}No grid-template-areas property appears here, yet grid-area: content works.
Common mistakes
- Using
sidebar-startand expecting a row position when the property isgrid-column. The name resolves on the axis of the property you used it in. - Placing the overlay in only one axis. The item then falls back to auto-placement for the other axis and can land in an unexpected cell.
- Forgetting that placed items overlap the areas they cross. Grid allows this, so you must manage stacking yourself.
Exercise
In the page layout above, which implicit line names does column line 2 have, and what does grid-column: sidebar-start / main-end span?
Show answer
Column line 2 is named both sidebar-end and main-start. The declaration grid-column: sidebar-start / main-end spans from column line 1 to column line 3, which covers both columns.