DEV Community

Cover image for The proper way to stack card items in CSS
Somnath Paul
Somnath Paul

Posted on • Originally published at blogs.somnathpaul.in on

The proper way to stack card items in CSS

Here is the story, So I was building my portfolio website and I wanted to create some blogs post cards to show case my recent articles. But while I built the cards and fetched the articles, I found out that the brief about the articles were of different lengths and thus let to cards of different heights. It was creating an extra gap between rows. Like this 👇

Using Flexbox or Grid

image.png

Well, you can make an argument why not use align-items: stretch;? Well, I don't want empty space in the cards either.

So let me get to the main part! Why not customize each item with flexbox or grid here? Well, you can when each item sizes are known and the number of items is static, but in my case, each card had different length and the height of the card was only found after the API call is made.

So, I googled a lot but most Stack overflow post ended up with 'You cannot do that with vanilla CSS'. But after a lot of digging, I found out about multi-Column layout and that was a big pain relief 😮💨.

Now look how the cards are stacked 👇

Using Multi Column layout

image.png

.blog_container{
  column-count: 2;
}

Enter fullscreen mode Exit fullscreen mode

Now look at the gap between the cards. This single line of code saved me from so much headache. Learn more about CSS columns 👉 here.

If you have a better solution, feel free to comment below!

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay