I would like to have a number of divs, all with equal width and height and a margin on either side of each, covering the full width of some container with equal space between. The divs should wrap down when the container width narrows enough that the div widths cannot be preserved (including their margins). The divs at the outer edges of each row should be flush with the full container width. I do know the exact number of elements.
Flexbox does this nicely with flex-wrap: wrap
and justify-content: space-between
, but what I don't like is that when the row wraps, I initially end up with a row containing 2 divs positioned at either side of the row. Then when the third div wraps it will be dead center. I understand this is what I've asked for with space-between
, but what I would really like is for the wrapping behavior to stack the divs on the bottom row left-aligned and vertically aligned with the justified divs above.
I'm aware of this solution here, which works great but I can't figure out a way to prevent the variable spacing at the bottom of the container without using media queries. The spacing is of variable height depending on the width of the container/screen, and I need it to be consistent.
This doesn't have to be accommplished with flexbox. I'm open to other solutions but I have to support IE10+ (and preferably IE9 also, which I know precludes flexbox). For example, I'm sort of looking for the benefits of both of these rows in this example:
.flex-container {
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
-ms-box-orient: horizontal;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -moz-flex;
display: -webkit-flex;
display: flex;
}
.wrap-1 {
justify-content: space-between;
}
.wrap {
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
}
.wrap-1 li {
background: tomato;
}
.wrap-2 li {
background: gold;
}
.flex-item {
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
<ul class="flex-container wrap wrap-1">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
<li class="flex-item">7</li>
<li class="flex-item">8</li>
</ul>
<ul class="flex-container wrap wrap-2">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
<li class="flex-item">7</li>
<li class="flex-item">8</li>
</ul>