Sub topic 11
Working with Line Names in Subgrids
This chapter assumes you know how to name grid lines with square brackets, such as [content-start], and how to place items with grid-column and grid-row. It also assumes you have seen grid-template-columns: subgrid in earlier chapters. Here you will learn how a subgrid gets line names, how to add your own, and how to avoid name collisions.
Two sources of names
A subgrid's lines come from the parent grid, so a subgrid line can carry names from two places.
- Inherited names. Each subgrid line is a parent line. Every name on that parent line is copied onto the matching subgrid line automatically.
- Local names. You can add names to the subgrid's lines in the
subgriddeclaration. These exist only inside the subgrid.
Local names never travel upward. The parent grid and the parent's other children cannot see them. This keeps a component's naming private, so a component can label its own lines without affecting the page layout.
Adding local names
Write the subgrid keyword, then one bracketed name list for each line, starting at the subgrid's first line.
.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 it has four lines. The subgrid spans all three columns, so it also has four lines. The four name lists map to those lines in order.
| Subgrid line | Local name |
|---|---|
| 1 | `edge-start` |
| 2 | `main-start` |
| 3 | `main-end` |
| 4 | `edge-end` |
.main is placed from main-start to main-end, which is line 2 to line 3. It therefore sits in the middle 2fr column.
Here is markup you can use with this CSS.
<div class="layout">
<section class="section">
<p>Intro text</p>
<div class="main">Main content in the wide column</div>
</section>
</div>Note that <p> has no placement rule. It is auto-placed into the first subgrid column. Only .main uses the names.
Rules for the name lists
- Names are assigned by position. The first bracket group names the first line, the second names the second line, and so on.
- You can name fewer lines than the subgrid has. The remaining lines get no local names.
- Extra groups beyond the number of subgrid lines are ignored. The subgrid does not grow to fit them.
- A single bracket group can hold several names, for example
[main-end sidebar-start]. - You can use
repeat()with names, for examplesubgrid repeat(3, [col]). This gives lines 1 to 3 the namecol.
The same syntax works for rows.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid [title] [text] [link] [card-end];
}
.card > h2 {
grid-row: title / text;
}Inheriting parent names
Often you do not need local names at all. If the parent already names its lines, the subgrid items can use those names directly. This is the common pattern for page layouts where a full-width section contains content that must follow the page's content column.
.page {
display: grid;
grid-template-columns:
[full-start] 1fr
[content-start] minmax(0, 40rem)
[content-end] 1fr
[full-end];
}
.page > section {
grid-column: full-start / full-end;
display: grid;
grid-template-columns: subgrid;
}
.page > section > * {
grid-column: content-start / content-end;
}
.page > section > .bleed {
grid-column: full;
}<main class="page">
<section>
<h2>Section title</h2>
<p>Text stays in the content column.</p>
<img class="bleed" src="wide.jpg" alt="A wide photograph">
</section>
</main>The section declares subgrid with no name lists, and it still sees content-start, content-end, full-start and full-end. This works because the section spans all four parent lines.
grid-column: full is a shorthand that works because of the naming convention. When a name X has lines called X-start and X-end, you can place an item with grid-column: X. That means full stands for full-start / full-end. Keep the -start and -end suffixes on your names so this shorthand stays available.
Names follow the spanned lines
The subgrid does not inherit all the parent's names. It inherits the names of the lines it actually overlays. If a subgrid spans only part of the parent, it sees only that part.
.page {
display: grid;
grid-template-columns:
[a] 1fr [b] 1fr [c] 1fr [d];
}
.inner {
grid-column: 2 / 4;
display: grid;
grid-template-columns: subgrid;
}.inner spans parent lines 2 to 4, so it has three lines. Those lines carry the names b, c and d. The names a is not available inside .inner. Placing an item with grid-column: a / b there does not match the name a.
Numeric line numbers are also local. Inside .inner, line 1 is the subgrid's first line. In the parent it is line 2. If you move .inner to a different parent span, numbers still count from its own first line, while names move with the parent lines. For this reason, names are usually the safer choice.
When names collide
A single line can hold several names, and a name can appear on several lines. If a local name matches an inherited one on a different line, the subgrid now has that name on two lines.
.page {
display: grid;
grid-template-columns: [start] 1fr [mid] 1fr [end];
}
.box {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid [start] [mid] [start];
}Inside .box, line 1 is named start, line 2 is named mid, and line 3 is named start and end. An item with grid-column: start resolves to the first matching line, which is line 1. To reach the later one, use a count: grid-column: start 2 uses the second line named start. Counts are how you pick among repeated names, and they are the right tool for a deliberately repeated name such as col in a repeat().
Duplicated names usually come from accidents. If a placement lands on the wrong line, check whether a local name repeats an inherited one.
Names and automatic placement
Line names affect only items that you place. Auto-placed items ignore them and flow into the next free cell, as in any grid. If an item has grid-column: main-start / main-end and the subgrid has no line with those names, the placement is not an error. The browser treats an unmatched name as an implicit line created after the last explicit line. This is one of the more confusing failures, because the item appears in an unexpected extra track instead of producing a warning. When an item lands far from where you expect, inspect the grid in your browser's developer tools and turn on line name overlays for the grid.
Practical guidance
- Name lines in the parent when many descendants need them, as in the page layout above.
- Add local names only for lines that matter to one component, such as
title,textandlinkrows in a card. - Keep the
-startand-endsuffix convention sogrid-column: nameandgrid-row: namestay available. - Prefer names to numbers inside subgrids, because numbers depend on how the subgrid spans the parent.
- Avoid reusing a parent name locally unless you intend to give one name to several lines.
Exercise
A parent grid has grid-template-columns: [a] 1fr [b] 1fr [c] 1fr [d];. A child has grid-column: 2 / 4; display: grid; grid-template-columns: subgrid [x] [y];. Which names does the child's line 2 carry, and which line does grid-column: b / d select for an item inside the child?
Show answer
The child spans parent lines 2, 3 and 4, so its lines carry the inherited names b, c and d. Its line 2 has the inherited name c plus the local name y. The local names x and y are added to lines 1 and 2. Inside the child, b / d selects subgrid lines 1 to 3, which covers both subgrid columns.
Exercise
You have a card subgrid with grid-template-rows: subgrid [title] [text] [link] [card-end]; and you want the card's h2 to sit in the first row. Write the placement rule, and explain why a rule in the parent's stylesheet using grid-row: title on a sibling of the card would not work.
Show answer
Use .card > h2 { grid-row: title / text; }. The names title and text are local to the card subgrid and are not added to the parent, so a sibling of the card cannot resolve them.