I have a simple grid using flex. Works fine but my issue is when there's not enough elements to fit the row, it spaces the elements out to the edges.
For example, when I have a row to fit 3 items per row and have a total of 5 items, the bottom row will have the two elements on the edges with a gap in the middle.
Here's my code:
.grid {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.grid div {
flex-basis: 26%;
height: 50px;
background-color: red;
margin-bottom: 20px;
}
<div class="grid">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
As you can see, there's a gap in the bottom row. Is there a flex method to push everything to the left while keeping the justify-content: space-between
?
I could just give margin right to everything but nth child 3n but I was wondering if there's a better flexy way to do it.