CSS Keep all flexbox children elements the same size
You have 2 ways of doing this:
flex-grow: 0 (fiddle)
If you set flex-grow
to 0 you are telling the items to keep their width and expand the empty space. Using flex-grow: 1
the items will expand the width to fit the space.
invisible items (fiddle)
Add some invisible items after the normal items with the same flex properties. You will get a left aligned look.
In this solution there will be the same number of items in all visible rows so make sure to add enough invisible items to work properly on larger screens.
I can't reply to Igor's answer to suggest an improvement to his solution, but his fiddle did not work entirely for a responsive layout.
The first solution with flex-grow: 0;
doesn't stretch the items to fill the containers. The second solution with invisible items works, but expands the height of the container. Removing the top/bottom margins did the trick though.
I edited the jsfiddle, to make it both responsive and fix the container-expanding invisible items, using comments to explain what I changed. It still doesn't work if the container is larger than the row of items though: (fiddle)
I also made my own simple solution. I was trying to do this myself, and Igor's solution helped immensely. (fiddle)
.container {
display: flex;
flex-flow: row wrap;
/*style*/
padding: 0.5em;
background-color: royalblue;
}
.item {
flex: 1 0 80px;
/*style*/
margin: 0.5em;
padding: 1em;
text-align: center;
background-color: lightblue;
}
.filler {
flex: 1 0 80px;
height: 0px;
/*style*/
margin: 0 0.5em;
padding: 0 1em;
}
<div class="container">
<div class="item">sm</div>
<div class="item">medium</div>
<div class="item">large item</div>
<div class="item">medium</div>
<div class="item">medium</div>
<div class="item">large item</div>
<div class="item">sm</div>
<div class="item">sm</div>
<div class="item">medium</div>
<div class="item">large item</div>
<div class="item">medium</div>
<div class="item">medium</div>
<div class="item">large item</div>
<div class="item">sm</div>
<div class="filler"></div>
<div class="filler"></div>
<div class="filler"></div>
<div class="filler"></div>
<div class="filler"></div>
<div class="filler"></div>
<div class="filler"></div>
<div class="filler"></div>
</div>