How to align left last row/line in multiple line flexbox [duplicate]

Got it. (I think)(this is my first contribution here!)

Imagine a layout which needs to have 4 images per row. w:205 h:174
Problem: Using justify-content:space-around, if the last row doesn´t have 4 images (has 3, 2 or 1), they would not respect the grid, they would spread.
So.

Create in the html 3 divs with the class “filling-empty-space-childs” like this.

.filling-empty-space-childs {
    width:205px; /*the width of the images in this example*/
    height:0; /*Important! for the divs to collapse should they fall in a new row*/
}

The flexbox container has display:flex / flex-wrap:wrap; / justify-content:space-around

The last row can have 4, 3, 2, 1 images.
4 images: no problem, this three divs would collapse in a new row since they have no height.
3 images: no problem, one div is going to be in the same row, invisible, and the other two would wrap to a new row, but will collapse since they have no height.
2 images: no problem, two divs are going to be in the same row, invisibles, the rest… collapsed
1 image: no problem, the three divs are going to fill in the space.

Leave a Comment