Menu

Stop Using a JS Masonry Library. CSS Grid Lanes Does It Natively.
๐Ÿ“ฐ

Stop Using a JS Masonry Library. CSS Grid Lanes Does It Natively.

DEV Community: tutorialยทParsa Jยทabout 24 hours ago
#GQw1wlJm
#devtotutorial#devto#dev#grid#masonry
Reading 0:00
15s threshold

Open any "masonry" npm package's GitHub issues and you'll find the same complaint, filed every few months in different words: items jump on resize, SSR renders everything in one column until JS hydrates, and the bundle is 15โ€“40KB for something that is, conceptually, "pack boxes into the shortest column." It's a layout problem. It has needed a JavaScript library to solve because CSS Grid โ€” for all its power โ€” has never been able to do it. Every row in a CSS grid has to be the same height across every column. A masonry layout is defined by not having that constraint.

So most teams land on one of two compromises. Neither is actually masonry.

The fix everyone reaches for first

The CSS-only version looks like this, and it's seductive because it needs zero JavaScript:

.gallery { columns : 3 ; column-gap : 16px ; }

.gallery > * { break-inside : avoid ; margin-bottom : 16px ; }

Enter fullscreen mode

Exit fullscreen mode

It renders a Pinterest-ish grid.โ€ฆ

Continue reading โ€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.