I'm aware can set the number of columns a grid item spans using:
grid-column: span 2;
But is it possible to have CSS grid do this automatically only if there is space available?
So in the case of the mockup below, if there are three items they each take up a full column, but if there are two, the last item will expand to fill up the empty column.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 25px;
}
.grid-item {
padding: 50px;
color: white;
background-color: lightpink;
}
<div class="grid">
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
</div>
<br />
<hr />
<br />
<div class="grid">
<div class="grid-item"></div>
<div class="grid-item">I should take up two columns</div>
</div>