I have a CSS grid of "cards" that can have some optional content in the middle. Within the cards I'm using grid-template-rows: subgrid so that the rows of content in each card can align with other cards. I'm using a row-gap: 50px to space the cards apart from each other but then I use row-gap: 0 in the card because I don't want to inherit the gap within the card.
The problem is I now have a space in the middle of each card that I can't seem to collapse. It is always tied to the parent grid's row-gap. Can someone explain what I'm missing?
Reproduced below or here: https://codesandbox.io/p/sandbox/jft9cp
.grid {
display: grid;
grid-template-columns: 150px 150px 150px;
grid-auto-rows: min-content;
column-gap: 15px;
row-gap: 50px;
}
.card {
border: 1px solid black;
display: grid;
grid-template-rows: subgrid;
grid-row: span 3;
row-gap: 0;
}
.topRow {
grid-row: 1;
}
.middleRow {
grid-row: 2;
}
.bottomRow {
grid-row: 3;
}
body {
font-size: 26px;
}
<div class="grid">
<div class="card">
<div class="topRow">Top Row</div>
<div class="middleRow">Middle Row</div>
<div class="bottomRow">Bottom Row</div>
</div>
<div class="card">
<div class="topRow">Top Row</div>
<div class="middleRow">Optional</div>
<div class="bottomRow">Bottom Row</div>
</div>
<div class="card">
<div class="topRow">Top Row</div>
<div class="bottomRow">Bottom Row</div>
</div>
<div class="card">
<div class="topRow">Top Row</div>
<div class="bottomRow">Bottom Row</div>
</div>
<div class="card">
<div class="topRow">Top Row</div>
<div class="bottomRow">Bottom Row</div>
</div>
<div class="card">
<div class="topRow">Top Row</div>
<div class="bottomRow">Bottom Row</div>
</div>
</div>
