I'm trying to create 2 column masonry layout using css grid layout. All items in grid have variable heights and it could have any n
number of items. So I can't define grid-row for each item. Is it possible to start each new item right after end of previous?
Also, I am not looking for solution using column-count
as I would like the items to be left and right. Using column-count
it will stack the items in columns, instead I want it besides each other and keeping the order/sequence of items as per the HTML markup.
I am able to achieve most of the part, but it swaps the column items for alternate rows.
Is there a way to fix this without giving grid row heights?
If giving grid row heights is the only way, I open to JS/jQuery solution, but I don't have clue what should be the heights for each of the items, the calculation of which I am not sure about.
Below is the snippet:
/*Reset*/
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
/*CSS MASONRY*/
.grid-wrapper {
padding-left: 23px;
padding-right: 23px;
}
.grid-row {
display: grid;
grid-gap: 35px;
grid-template-columns: repeat(auto-fill, minmax(calc(50% - 18px), 1fr));
}
.grid-row > .grid-col:nth-child(2n+1) {
width: 100%;
grid-row: span 2;
}
<div class="blog-wrapper">
<div class="grid-row">
<div class="grid-col">
<div class="grid-img">
<img src="https://placehold.jp/308x400.png?text=1" width="308" height="400" alt="1" />
</div>
<div class="grid-detail">
<h3>Title goes here...</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
<div class="grid-col">
<div class="grid-img">
<img src="https://placehold.jp/307x252.png?text=2" width="308" height="252" alt="2" />
</div>
<div class="grid-detail">
<h3>Some long title goes here...</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<div class="grid-col">
<div class="grid-img">
<img src="https://placehold.jp/308x400.png?text=3" width="308" height="400" alt="3" />
</div>
<div class="grid-detail">
<h3>Some really really long title goes here</h3>
<p>Lorem ipsum dolor sit amet, Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
</div>
</div>
<div class="grid-col">
<div class="grid-img">
<img src="https://placehold.jp/307x252.png?text=4" width="308" height="252" alt="4" />
</div>
<div class="grid-detail">
<h3>Some really really long title goes here, and goes on new line</h3>
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.</p>
</div>
</div>
<div class="grid-col">
<div class="grid-img">
<img src="https://placehold.jp/307x400.png?text=5" width="308" height="400" alt="5" />
</div>
<div class="grid-detail">
<h3>Long long long long long title goes here, and goes on new line</h3>
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.</p>
</div>
</div>
</div>
</div>
Note:
- In the above snippet you can see the main issue, the items are seen 1, 2, 4, 3, 5 instead it should be 1, 2, 3, 4, 5.
- In the snippet above there is no fixed amount of content so height of the items will be different for most of the items in grid.
- I am also open to custom JS/jQuery solution to this.
Appreciate your help in advance!