Sub topic 8
Inheriting and Overriding Gaps
This chapter assumes you know CSS Grid basics (gap, grid-template-columns, grid-row) and that you have seen the subgrid keyword. Here you will learn how a subgrid treats the gaps of its parent, and how to change that spacing without breaking alignment.
The default: the subgrid uses the parent's gap
A subgrid does not define its own tracks, so by default it does not define its own gaps either. It uses the row-gap and column-gap of the parent grid in the subgridded axis.
.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;
}Here each .card spans three parent rows. Its three rows are separated by 1rem, the same gap that separates the parent's rows. Nothing else is needed.
Overriding the gap
You can set gap, row-gap or column-gap on the subgrid. The new value replaces the inherited one for that subgrid only. The parent and the other subgrids keep their own gaps.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}The parent's row gap is still 1rem. Inside each card, the gaps between title, text and link are now 0.5rem.
How the tracks stay aligned
The tracks themselves are still the parent's tracks. Only the space between the lines changes. The browser handles the difference this way:
- The subgrid's gap is compared with the parent's gap.
- The difference is added to the subgrid items as extra margin-like space on each side of the line, half on each side.
- The parent track sizes do not change, so a subgrid on one card cannot move the rows of its neighbours.
In practice, a smaller subgrid gap makes the items inside the subgrid sit slightly farther into the tracks. A larger gap pushes them toward the middle of the tracks. You can think of the gap override as padding applied around the items at each inner line.
A worked example
This example uses the card markup from earlier chapters. Both cards have three children, so the subgrid has three rows.
<div class="cards">
<article class="card">
<h2>Short title</h2>
<p>Brief text.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h2>A much longer title that wraps onto two lines</h2>
<p>Longer body text that takes more room than the first card.</p>
<a href="#">Read more</a>
</article>
</div>.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 2rem 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.25rem;
}The parent uses 2rem between rows and 1rem between columns. A card is a subgrid in the row axis only. Its row-gap is 0.25rem, so the title, text and link sit close together. The 2rem gap between one row of cards and the next still comes from the parent.
The column gap of .card is not inherited, because the card is not a subgrid in the column axis. The 1rem column gap belongs to the parent and separates cards from each other.
Common errors
- Setting a gap on the subgrid and expecting the parent to change. The override is local. To change the spacing between cards, change the parent's gap.
- Forgetting that the gap is inherited only in the subgridded axis. If you write
grid-template-rows: subgridand setcolumn-gapon the card, that column gap applies to the card's own columns, which are not inherited. - Using
gap: 0to remove spacing, then seeing content touch. Without a gap, the items are flush against the track edges. Addpaddingto the items if you need breathing room. - Using margins to fake a gap. Margins on subgrid items add to the track size contribution and can make parent rows taller. A
row-gapoverride does not do that.
Exercise
A parent grid has gap: 1.5rem. A card is a subgrid in the row axis with row-gap: 0.5rem. What gap separates two neighbouring cards in the same column, and what gap separates the rows inside one card?
Show answer
Neighbouring cards in the same column are separated by the parent's 1.5rem row gap, because the card's own gap applies only inside the card. The rows inside one card are separated by 0.5rem, the overridden value.