I've got an unknown number of elements in a container that need to wrap with no margins on the outside, but minimum margins between them.
I also need these to be justified with space-between
and the last row left aligned.
I'm trying to do this with flexbox like so:
.outside {
border: 1px solid black;
}
.container {
margin: -5px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.container:after {
content: '';
flex: auto;
}
.box {
background: red;
width: 50px;
height: 50px;
margin: 5px;
}
<div class="outside">
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
This works correctly, except that the spacing on the last row is off, as you can see in the screenshot:
Is there any way to get this to work if we don't know how many columns there will be (using flexbox or something else other than javascript)?