I'm trying to make the last grid item fill all empty columns in a css grid that has auto-fit for wrapping items.
I've read Expand a div to fill the remaining width and Make last element take remaining width with wrapping (and with IE9 support), but they don't answer the case of the dynamically wrapping grid that is here the important part since they are made for float
and display: block
.
So for example if the grid-item is wrapped in a new line i want it to stretch for taking all the empty space.
If you look at the code then the div number 7 should always take all the empty space in the row. I've managed to hack something which dosen't seem robust enough, using 100vw
is there a better way?
For the following example: resize the window to see the items wrap into the next lines:
* {
box-sizing: border-box;
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.grid-element {
background-color: deepPink;
padding: 20px;
color: #fff;
border: 1px solid #fff;
}
.grid-element:last-of-type {
/*width: 100vw;*/ /* Is there a better way? */
}
body {
padding: 2em;
}
hr {
margin: 80px;
}
<div class="grid-container">
<div class="grid-element">
1
</div>
<div class="grid-element">
2
</div>
<div class="grid-element">
3
</div>
<div class="grid-element">
4
</div>
<div class="grid-element">
5
</div>
<div class="grid-element">
6
</div>
<div class="grid-element">
7
</div>
</div>