I want to arrange differently sized boxes in a "newspaper-style", but with elements arranged left-to-right first, and then top-to-bottom. So the goal is to get something that looks like this:
I know that the column module was built for news-paper-style layouts, but it arranges the elements top-to-bottom first, and then left-to-right:
Here's the most important CSS, a full example is in this Codepen:
.container {
column-count: 3;
max-width: 960px;
width: 100%;
}
.box {
width: 300px;
break-inside: avoid;
height: fit-content;
}
Using a flexbox with wrapping will arrange the elements left-to-right first, but it will create gaps below the shorter elements (Codepen):
.container {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
max-width: 960px;
width: 100%;
}
Is there a way to achieve the desired layout using plain CSS?



The idea is quite simple.
flex-flow: columnwithwrapcan do precisely whatcolumn-countdoes. You'll need two conditions to make it work: 1. The flexbox container needs to have a fixed height and it needs to be taller than your tallest column. 2. Flex children need width, 50% for 2-column layout, 33% for 3-column layout, etc.This approach has the same problem as
column-count: the elements are ordered by column, top-down. But since we're using flexbox, now we get access to theorderproperty.Using :nth-child() and order we can override the default order.
Items with
order: 1will go in the first column,order: 2will go in the second column, andorder: 3in the third column.In
(an + b)formulaarepresents a cycle size,nis a counter (starts at 0), andbis an offset value. So(3n+1)selects every third item starting with the first one.(3n+2)selects every third item but starting with the second item. And(3n)selects every third item starting with thethirditem, since there's nothing at index 0.In certain layouts, columns might merge. To solve this issue,insert pseudo-elements between columns: Read more about the method
This is Another Post about Horizontal Line Masonry
I have also changed
margin-bottom: 0px;on.box& usedgap:5pxon the flex-container/your .container