Sub topic 11
Working with Line Names in Subgrids
This chapter assumes you know CSS Grid basics: grid-template-columns, fr units, gap, and placing items with line numbers and span. It also assumes you know how a subgrid inherits its parent's tracks. Here you will learn how named lines carry into a subgrid and how to add your own names.
Why line names matter
Grid lines can have names. You can place an item with grid-column: main-start / main-end instead of counting numbers. In a subgrid, names help for two reasons:
- A subgrid uses its own local line numbers, starting at 1. Names do not depend on that numbering.
- Placement code stays readable when the parent layout changes.
Names are inherited from the parent
When a grid item becomes a subgrid, it receives the line names the parent defined on the lines it spans. A child of the subgrid can then use those names directly.
.layout {
display: grid;
grid-template-columns:
[full-start] 1fr
[content-start] 2fr
[content-end] 1fr
[full-end];
}
.section {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid;
}
.section > .main {
grid-column: content-start / content-end;
}The .section element spans all three parent columns. It inherits the four lines and their names, so .main can use content-start and content-end even though those names were never written on .section.
Adding names inside a subgrid
You can also write names after the subgrid keyword. Each bracketed list names one line of the subgrid, in order. These names are added to the inherited ones and apply only inside the subgrid.
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
.section {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid [edge-start] [main-start] [main-end] [edge-end];
}
.section > .main {
grid-column: main-start / main-end;
}The parent has three columns, so the subgrid has four lines. The four bracketed names map to those four lines in order. If you write fewer names than lines, the remaining lines stay unnamed. Extra names beyond the line count are ignored.
Local scope
Names added in a subgrid are local. A sibling subgrid, or an item in the parent grid, cannot use main-start unless the parent also defines it. Names defined on the parent, however, are visible in the subgrid. Keep this in mind if a placement works in one section and fails in another.
A line can have several names. A name given in the subgrid does not replace the parent's name for the same line. Both resolve to that line, so you can mix them in one rule.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Writing more names than the subgrid has lines | Extra names are ignored | Count lines: tracks spanned plus one |
| Expecting a subgrid name to work in the parent | Placement falls back to automatic | Define the name on the parent instead |
| Forgetting that the subgrid spans only some parent tracks | Names from tracks outside the span are missing | Check the `grid-column` span on the subgrid |
Exercise
A parent grid has columns [a] 1fr [b] 1fr [c] 1fr [d]. A subgrid spans columns 2 and 3 of the parent and uses grid-template-columns: subgrid [x] [y] [z]. Which parent names are available to the subgrid's children, and how many lines does the subgrid have?
Show answer
The subgrid spans two columns, so it has three lines. These are parent lines b, c and d, which the subgrid inherits with their names. Line a is outside the span, so it is not available. The names x, y and z are added to the three lines in order, so line b also has x, line c also has y and line d also has z.
Exercise
You write grid-column: main-start / main-end on a child of a subgrid, but the child ignores it and is placed automatically. The names are defined only on a different subgrid in the same page. What is wrong?
Show answer
Names added in a subgrid are local to that subgrid. The child's subgrid does not define main-start or main-end, and the parent does not either. Define the names on the parent grid, or add them after the subgrid keyword on the subgrid that contains the child.