Sub topic 12
Implicit Named Lines Created by Areas
This chapter assumes you can already write a grid-template-areas map and place items with grid-area. It also assumes you know that a grid is made of numbered lines, with tracks between them. Here you will learn that every named area also creates named lines. You can use those lines for placement the area rectangle can't express.
Areas are made of lines
A grid with three columns has four vertical lines. Normally you refer to them by number: grid-column: 1 / 3. Numbers are fragile. If you add a column, every number after it shifts.
When you declare an area, the browser gives names to the lines around it. For an area called main, it creates these lines:
main-starton the column axis (the left edge in a left-to-right language).main-endon the column axis (the right edge).main-starton the row axis (the top edge).main-endon the row axis (the bottom edge).
The same name is used on both axes. The browser picks the right axis from the property you use. grid-column: main-start / main-end looks for column lines. grid-row: main-start / main-end looks for row lines.
You never write these names. They exist because the area exists.
Using the implicit lines
Start with the page layout from earlier chapters.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</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; }Suppose you want a tinted band behind the sidebar and the main content, including the gap between them. No single area covers that region. The implicit lines do.
.page::before {
content: "";
grid-column: sidebar-start / main-end;
grid-row: sidebar-start / main-end;
background: #eef2f7;
}The pseudo-element is a grid item. It starts at the left edge of sidebar and ends at the right edge of main. In the row axis, both areas occupy the same row, so the band covers that one row. The ::before item comes first in the tree. Grid items paint in source order when no z-index is set, so the sidebar and main content paint on top of it.
If you had written grid-column: 1 / 3, the band would work today and break the first time someone inserted a column.
Shorthand forms
You can also use the bare area name in grid-column and grid-row. For the start value the browser looks for name-start. For the end value it looks for name-end.
| Declaration | Resolves to |
|---|---|
| `grid-area: main` | All four edges of the `main` area |
| `grid-column: main` | Column start `main-start`, column end `main-end` |
| `grid-row: main` | Row start `main-start`, row end `main-end` |
| `grid-column: sidebar-start / main-end` | From the left edge of `sidebar` to the right edge of `main` |
| `grid-column: main-start / span 2` | From the left edge of `main`, across two column tracks |
The second and third rows are useful when an item should match an area's columns but have its own row. For example, a toolbar that lines up with main horizontally could use grid-column: main and be placed in an extra row.
.page {
grid-template-rows: auto auto auto auto;
grid-template-areas:
"header header"
"sidebar main"
"sidebar main"
"footer footer";
}
.toolbar {
grid-column: main;
grid-row: 4;
}Here main covers rows 2 and 3, and .toolbar sits in row 4. The areas map has four rows, so the fourth row exists. The footer rule would also need to use row 5 or share row 4, so choose your row numbers to match your map.
The reverse direction: lines create areas
The relationship works in the other direction. If a grid has a line named foo-start and a line named foo-end on the same axis, the browser treats foo as an area name. That holds even when you never wrote grid-template-areas.
Both axes need the pair of lines. The grid-area: foo shorthand sets all four edges, and each edge looks up its own axis.
.layout {
display: grid;
grid-template-columns:
[sidebar-start] 200px [sidebar-end main-start] 1fr [main-end];
grid-template-rows:
[sidebar-start main-start] 1fr [sidebar-end main-end];
gap: 1rem;
}
.layout > aside { grid-area: sidebar; }
.layout > main { grid-area: main; }Several names can sit on one line. The line between the first and second column is both sidebar-end and main-start. The row axis has one track, and its top line carries both sidebar-start and main-start.
This style gives you named areas without a text map. It is more verbose. Choose it when you want to name lines for other reasons, such as a full-bleed layout with [full-start] ... [content-start] ... [content-end] ... [full-end]. You then get content and full as area names for free.
What the implicit lines do not do
They exist only where the area exists
Implicit lines come from the explicit grid. If an area is not in the map, it has no lines. An item whose grid-area names something that doesn't exist is not an error. The browser treats the name as a line that doesn't exist and falls back to implicit tracks. The item typically ends up in a track the browser adds after the explicit grid, which looks like a layout bug with no warning. A misspelled area name is the usual cause. When an item lands in the wrong place, check the spelling against the map first.
Area edges win over same-named lines
If you define an explicit line named main and also have an area called main, grid-column: main matches the area's edges first. It uses main-start and main-end. It does not use your [main] line. Avoid naming a line the same as an area.
Names are not inherited by nested grids
A grid item that is itself a grid container starts its own line namespace. In the earlier chapter the main element held an auto-fit card grid. Inside it, main-start does not exist. Only subgrid passes the parent's tracks and line names to a child. Subgrid is outside this chapter, so keep in mind that implicit names are local to one grid.
Repeated names are counted
A name can appear on many lines. Writing grid-column: card-start 2 / span 1 picks the second line named card-start. For area names this rarely comes up, because each area name appears once per axis. It matters when you add explicit lines that reuse an area's name. Then main-start may refer to more than one line, and grid-column: main-start picks the first.
Choosing between areas and lines
| Need | Use |
|---|---|
| Place an item in exactly one named rectangle | `grid-area: name` |
| Match an area's columns but not its rows | `grid-column: name` |
| Span from one area's edge to another's | `grid-column: a-start / b-end` |
| Draw a backdrop or divider across several areas | Implicit lines on a pseudo-element or empty item |
| Get area names from a text map | `grid-template-areas` |
| Get area names while also controlling line names | Explicit `[name-start]` and `[name-end]` lines |
A worked check
Take this map and trace what the lines are:
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main";
}On the column axis there are three lines. The left line carries header-start and sidebar-start. The middle line carries sidebar-end and main-start. The right line carries header-end and main-end. On the row axis there are three lines. The top line carries header-start. The middle carries header-end, sidebar-start and main-start. The bottom carries sidebar-end and main-end.
So grid-column: header-start / sidebar-end is valid. It spans only the first column, since sidebar-end is the middle line. The browser does not require the start and end to come from the same area. It requires only that the lines exist on that axis.
Exercise
A grid has grid-template-areas: "nav content" "nav footer" and two columns. You want a decorative rule that starts at the left edge of content and stops at the right edge of footer, in the row where content sits. Which grid-column and grid-row values would you give it, and why does grid-row: content work for the row?
Show answer
Use grid-column: content-start / footer-end and grid-row: content. The content and footer areas occupy the same column, so the column span is that single column. The bare content in grid-row resolves to content-start for the row start and content-end for the row end, which are the top and bottom edges of the first row.
Exercise
An item has grid-area: sidbar, but the map defines sidebar. The page does not show an error, yet the item appears below the footer in a new row. What happened, and how do you fix it?
Show answer
No area or line named sidbar exists, so the browser cannot resolve the placement against the explicit grid. It places the item in an implicit track added after the explicit tracks. Fix the spelling to grid-area: sidebar.
Exercise
Given grid-template-columns: [a-start] 1fr [a-end] 1fr and grid-template-rows: [a-start] auto, what is missing before grid-area: a places an item in the first column? Write the corrected row declaration.
Show answer
The row axis has a-start but no a-end. Each axis needs both lines for the area to exist there. Use grid-template-rows: [a-start] auto [a-end];.