In a CSS grid, you can use grid-column: 1 / -1
to stretch an element across the entire explicit grid. However, if you add new elements and the grid automatically has more columns than explicitly stated, this doesn't have the same effect.
Is there any value I can put instead of -1
that will force the item to stretch all the way to the end of the grid?
In the following example, because grid-auto-flow
is set to column
, the extra elements add new columns. I want the .stretch
element to stretch the entire width of the grid, not just the width of the specified 3x3 grid.
.grid {
display: grid;
grid-template-rows: 1fr 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-flow: column;
width: 300px;
}
.item {
border: 1px solid red;
padding: 15px;
}
.stretch {
grid-column: 1 / -1;
}
<div class="grid">
<div class="stretch item">
Stretched
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
<div class="item">
.
</div>
</div>