My target is to use CSS grid layout for a two-column masonry layout of boxes. Given an element with children:
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
bring the children to sort themselves alternately into columns. My idea was to have two grid areas left
and right
and telling the children to split into them:
section {
display: grid;
grid-template-areas: "left right";
}
div:nth-child(odd) {
grid-area: left;
}
div:nth-child(even) {
grid-area: right;
}
Here's a JSBin that illustrates my current state: https://jsbin.com/zaduruv/edit?html,css,output
Unfortunately, the elements react quite identical to as if they had position:absolute
set. That is, they all crowd towards the top and overlap one another.
Is there any possibility with the CSS grid layout properties to bring the children to line up in the columns, like they'd do normally with position: static
?
https://jsbin.com/tujuzumaca/edit?html,css,output
the only thing you need to make sure is not to give fixed height to the DIVs but to let it take height on its own according to the content. – Googolflex-direction: column
, but it has the disadvantage, that I need to set a fixed height on thesection
, otherwise the children wouldn't wrap into a second column. Hence my idea to try and use grid layout. – Piloting