Right now I am using flexbox to create a gallery of images with three columns. (I am not sure if this is better achieved by flexbox or grid, but flexbox makes sense to me as I don’t want fixed grids).
However, I cannot seem to figure out a way to make items stick to each other in the cross axis. For example, this is how my gallery looks
Image E and F are staying in their respective places, when the behaviour I want is
, where Image E and F stick to B and C respectively.
How can this be achieved by flexbox? Or if not, is it possible with grid?
2
Answers
This is not achievable not with grid nor flex (grid masonry does this but not implemented yet). You must use a grid with 3 columns and each column is a flex container. (this could be done differently)
It can be done with flexbox alone but it’s more trouble than it’s worth and you need to rearrange img order, code snippet below. Css code can be a lot shorter, this is just to show you it can be done. You can also customize percentages/margins/whatever makes it suit your project needs. It also has the advantage that you may add additional blocks to your column; and it’s responsive + I added padding/borders so you may better understand how it works.