Sub topic 14
Naming grid lines
This chapter assumes you can write grid-template-columns and grid-template-rows, place items with line numbers such as grid-column: 1 / 3, and use fr, repeat() and minmax(). You should also know how grid-template-areas works. Here you will give names to the grid lines themselves and place items by those names.
Why name lines
Line numbers are fragile. If you add a column to the start of a grid, every grid-column: 3 / 5 in your stylesheet points at the wrong place. Negative numbers such as -1 help for the last line, but they do not help for a line in the middle.
A named line is a label attached to a grid line. You can move or add tracks and the names still point at the right edges. Names also make the CSS easier to read, because grid-column: content-start / content-end says what it means.
Declaring line names
Write a name in square brackets between the track sizes. Each bracket sits on the grid line at that position.
.page {
display: grid;
grid-template-columns:
[full-start] 1fr
[content-start] minmax(0, 60ch)
[content-end] 1fr
[full-end];
}This grid has three column tracks and four column lines. The diagram shows how names attach to lines.
Naming does not remove the numbers. Line 2 is still 2, and you can mix names and numbers in the same declaration.
Several names on one line
A line can carry more than one name. Separate the names with spaces inside one pair of brackets.
.dashboard {
display: grid;
grid-template-columns:
[sidebar-start] 240px
[sidebar-end main-start] 1fr
[main-end];
}The line between the two tracks is both sidebar-end and main-start. You do not need to declare the same edge twice.
Placing items by name
Use a name anywhere you would use a line number.
.toolbar {
grid-column: main-start / main-end;
}The -start and -end suffixes are a convention, and the next section shows that the browser gives them special meaning. Names are case-sensitive, so Main-start and main-start are different. The words span and auto cannot be used as names.
The -start and -end shortcut
If your lines are named something-start and something-end, the browser treats something as an area name. You can then place an item with a single word.
<article class="article">
<h1>Grid line names</h1>
<p>Body text stays in the readable column.</p>
<figure class="full-bleed">A wide banner that reaches both edges.</figure>
<p>More body text.</p>
</article>.article {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] minmax(0, 60ch)
[content-end] minmax(1rem, 1fr)
[full-end];
}
.article > * {
grid-column: content;
}
.article > .full-bleed {
grid-column: full;
background: #dde8f5;
}grid-column: content expands to content-start / content-end, and grid-column: full expands to full-start / full-end. Every child sits in the middle track by default. Only the elements that opt in stretch across the whole width. This "full-bleed" article layout is one of the most common uses of named lines. It is also easier to maintain than a grid of numbered lines.
Because the grid has one column and many rows, the items fall into implicit rows automatically. You only named the column lines.
The link to grid-template-areas
The shortcut works in both directions.
- Each named area in
grid-template-areascreates implicit lines calledname-startandname-end, in both axes. - Lines you name with
-startand-endcreate an implicit area, sogrid-area: nameworks.
Use this to combine the two techniques. In the layout from the earlier chapter on template areas, you could add a toolbar that spans only the sidebar and main areas.
.layout {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.layout > .note {
grid-column: sidebar-start / main-end;
grid-row: footer-start;
}sidebar-start and main-end exist because those areas exist, even though you never wrote the names in brackets.
Repeated names and counting
Several lines can share one name. To pick one, put a number after the name. col 2 means the second line named col.
.grid {
display: grid;
grid-template-columns: repeat(4, [col] 1fr);
gap: 1rem;
}
.wide {
grid-column: col 2 / span 2;
}Here repeat() stamps the name [col] on the line before each of the four tracks. That gives four lines named col. The final line, at the right edge, has no name. The .wide item starts at the second col line and spans two tracks.
You can also span by name. span col 2 means "span until you have crossed two lines named col." This is useful in a table-like layout where you want to say "cover two groups" and not "cover seven tracks".
| You write | The browser reads it as |
|---|---|
| `grid-column: main-start / main-end` | From the line named `main-start` to the line named `main-end` |
| `grid-column: main` | The same as above, if both `-start` and `-end` lines exist |
| `grid-column: col 2 / span 2` | From the second `col` line, covering two tracks |
| `grid-column: col 3 / -1` | From the third `col` line to the last explicit line |
| `grid-column: span col 2` | Cover tracks until two lines named `col` have been crossed |
Common errors
A misspelled name fails quietly
CSS does not report an error when a named line does not exist. The browser resolves the reference against the implicit grid, so the item does not land where you intended. It may also create extra tracks. If an item jumps to an odd place after you rename a line, search your stylesheet for the old name.
Brackets in the wrong place
Line names go between track sizes, not inside them. This is correct:
.ok {
grid-template-columns: [a] 100px [b] 1fr [c];
}With minmax(), put the name outside the function: [content-start] minmax(0, 60ch). A name inside the parentheses is invalid and the whole declaration is dropped.
Names with repeat() and auto-fit
Named lines are allowed inside repeat() with a fixed count, as shown above. Inside auto-fill they are allowed too, but with auto-fit and auto-fill the number of lines depends on the container width. Avoid placing items by a repeated name's number in those grids, because the count changes.
When to use names
Use named lines when the grid is shared by many items, as in the full-bleed article, or when the same edge appears in many rules. Use line numbers for small, one-off grids where the numbers are obvious. For page skeletons with a few large regions, grid-template-areas is often clearer, and you can still reach into it with the -start and -end names when you need a finer placement.
Exercise
A grid is declared as grid-template-columns: [a] 100px [b] 1fr [c] 1fr [d];. Write a rule that places an item so it covers both 1fr columns, using names. Then say what grid-column: b / span 2 selects.
Show answer
Use grid-column: b / d;. This starts at line b and ends at line d, so the item covers the two 1fr tracks. grid-column: b / span 2 selects the same two tracks, because it starts at b and spans two tracks to d.
Exercise
You write grid-template-columns: [content-start] 60ch [content-end]; and .post { grid-column: contnt; }. What happens to .post, and how do you fix it?
Show answer
The name contnt matches no line, and CSS gives no error, so the browser falls back to implicit placement and .post ends up in the wrong place. Fix the spelling to grid-column: content;, which expands to content-start / content-end.