Sub topic 8
Inheriting and Overriding Gaps
This chapter assumes you know CSS Grid basics: display: grid, grid-template-columns and grid-template-rows, fr units, gap, and placing items with grid-column and grid-row. You should also know the subgrid keyword and how a subgrid takes its tracks from its parent. Here the topic is the space between those tracks.
What a gap is in a grid
A gap (also called a gutter) is the space between two adjacent tracks. It is set with row-gap, column-gap, or the gap shorthand. The gap is not part of any track. It sits between tracks, and the grid container adds it to its total size.
A subgrid shares tracks with its parent, so it also has to decide what to do about the space between them. The rule is simple:
- By default, a subgrid uses the gap of its parent grid.
- If you set
gap,row-gap, orcolumn-gapon the subgrid itself, that value replaces the inherited gap for the subgrid's own items.
The details of the override are the part people get wrong, so the rest of this chapter looks at them closely.
Default behavior: the gap is inherited
Take a parent with a 1rem gap and a subgrid that spans three rows.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}The .card sets no gap of its own. Its three rows are the parent's three rows, and the gap between them is the parent's 1rem. If the card has a title, a text paragraph and a link in those rows, each sits 1rem from the next.
This is often the right behavior, but it can surprise you. Rows inside a card now follow the spacing between cards. If you make the parent gap larger to separate cards, the spacing inside each card grows too.
Overriding the gap on the subgrid
To change the spacing inside the card only, set a gap on the subgrid.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}The parent's rows are still the card's rows, but the card now treats the space between them as 0.5rem.
This raises a question. The parent grid reserved 1rem between those rows, and the card now claims 0.5rem. What happens to the difference?
The answer is that the subgrid's gap does not change the parent's tracks. Instead, the browser adjusts for the difference by giving the subgrid's edge a negative margin-like effect on its content. In practice, a gap smaller than the parent's gap lets the items in the subgrid sit closer together than the parent's tracks would suggest, and the subgrid's own content area is extended into the parent's gap space. A gap larger than the parent's gap works the other way: the extra space is added as padding-like space inside the subgrid, and the tracks keep their parent sizes.
The specification describes this by saying the subgrid's gap, relative to the parent's gap, is an adjustment applied to the tracks it uses. You do not need to calculate this by hand, but you should know two consequences:
- Tracks stay aligned with the parent. Overriding the gap never moves a subgrid track out of line with the parent track. Alignment across cards is preserved.
- Item positions inside the subgrid can shift. An item placed on a track can end up with different space around it than the parent grid gives its own items.
A worked comparison
The table below shows what happens to three card rows with different gap settings. The parent gap is 1rem in each case.
| Subgrid gap setting | Space between title and text | Tracks aligned with other cards |
|---|---|---|
| Not set | `1rem` (inherited) | Yes |
| `row-gap: 0.5rem` | `0.5rem` (overridden) | Yes |
| `row-gap: 0` | No space | Yes |
| `row-gap: 2rem` | `2rem` (overridden) | Yes |
The last column is the useful one. The override changes how the content spaces itself. It does not break the shared track structure.
Overriding in one axis only
The gap shorthand sets both axes. When the subgrid inherits only one axis, you can still set gaps on both, but only the inherited axis uses the parent's tracks. Consider a card that is a subgrid in rows but defines its own columns.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
grid-template-columns: 1fr auto;
row-gap: 0.5rem;
column-gap: 1rem;
}Here row-gap adjusts the inherited rows. The column-gap applies to the card's own, independent columns, just as in any regular grid. Using the two longhand properties makes it clear which axis you mean. With gap: 0.5rem 1rem, the first value is row-gap and the second is column-gap.
Using the `normal` value
The initial value of gap properties is normal. For a regular grid container, normal computes to 0. For a subgrid, normal means "use the parent's gap". That is why leaving the gap unset inherits the parent's value.
This gives you two different ways to write the same intent, and they differ:
- Leaving
row-gapunset inherits the parent's value. - Writing
row-gap: 0explicitly overrides it to zero.
If you want to return to inheritance after a broader rule sets a gap, use row-gap: normal.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
.card.tight {
row-gap: 0;
}
.card.tight.reset {
row-gap: normal;
}The last rule restores the inherited parent gap.
Gaps and spanning
The number of gaps a subgrid contains depends on how many tracks it spans. A subgrid that spans three rows contains two row gaps. A subgrid that spans one row contains none, because there is only one track and so no space between tracks.
This matters when you size a card. If a card spans three rows with a parent row gap of 1rem, the card's height includes the two inherited gaps (2rem) plus its three tracks. Changing the number of spanned rows changes the number of gaps. If you change span 3 to span 4, the card gains a track and a gap.
.card {
grid-row: span 4;
grid-template-rows: subgrid;
}The extra row exists in the parent. Make sure the card has a child for it, or the track will be empty. An empty track still takes up its sized height and its gap.
Gaps in the form pattern
Form layouts show a useful use of gap overrides. The parent form has one gap between fields and the field subgrids share its columns.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}The parent column gap is 1rem, and each .field inherits it between the label column and the input column. That means the label-to-input distance is the same for every field. If you only want to adjust that distance in one kind of field, override it there.
.field.compact {
column-gap: 0.5rem;
}The label column and input column still line up with the other fields. Only the spacing between the label and the input in that field changes.
A runnable example
This page lets you compare the inherited and overridden gaps. Open it in a browser that supports subgrid. The first card inherits the parent gap, and the second overrides it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Subgrid gaps</title>
<style>
.cards {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 1.5rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
border: 1px solid #888;
padding: 0.5rem;
}
.card.tight {
row-gap: 0.25rem;
}
.card > * {
margin: 0;
background: #eef;
}
</style>
</head>
<body>
<div class="cards">
<article class="card">
<h2>Inherited</h2>
<p>Rows are 1.5rem apart.</p>
<a href="#">Link</a>
</article>
<article class="card tight">
<h2>Overridden</h2>
<p>Rows are 0.25rem apart.</p>
<a href="#">Link</a>
</article>
</div>
</body>
</html>The shaded blocks make the space between rows visible. The three rows of each card still start at the same heights, even though the spacing differs.
Common mistakes
- Expecting the parent gap to disappear. A subgrid gap of
0does not remove the space the parent reserved between tracks. The tracks keep their positions. - Setting
gapwhen only one axis is a subgrid. The shorthand also sets the gap for the independent axis, which may not be what you intended. Userow-gaporcolumn-gap. - Using
marginbetween subgrid rows. A gap is the right tool for space between tracks. Margins on items add space inside a track and change its size. - Forgetting that the parent gap grows the subgrid. When you raise the parent gap, every spanning subgrid gets taller as well.
Summary of rules
- A subgrid's gap is
normalby default, which means it uses the parent's gap. - Setting
gap,row-gaporcolumn-gapon the subgrid overrides the inherited value for that subgrid's items. - The override never changes the parent's tracks, so alignment with sibling subgrids is kept.
- A subgrid contains one fewer gap than the number of tracks it spans.
- Only the axes declared as
subgriduse the parent's tracks. The other axis uses its own gap as in any regular grid.
Exercise
A parent grid has gap: 2rem. A .card spans three rows with grid-template-rows: subgrid and sets no gap. You then add row-gap: 0.5rem to .card. Which rule applies to the space between the card's title and text before and after the change, and do the card's rows stay aligned with the neighboring cards?
Show answer
Before the change, the row-gap of the card is normal, so it uses the parent's 2rem. After the change, the card uses 0.5rem between its rows. In both cases the rows stay aligned with the neighboring cards, because a gap override never changes the parent's tracks.
Exercise
A form uses grid-template-columns: max-content 1fr with gap: 0.75rem 1rem, and each .field is a column subgrid. How do you reduce the space between label and input to 0.5rem for fields with the class compact, and which other fields are affected?
Show answer
Add .field.compact { column-gap: 0.5rem; }. Only the fields with the compact class change. The label and input columns of all fields remain aligned, because the column tracks still come from the parent.