A practical implementation of masonry layout using CSS Grid and minimal JavaScript that works across all browsers today. The technique uses grid-auto-rows set to 0px, row-gap of 1px, and dynamically calculates item heights with getBoundingClientRect to create proper spacing. The solution includes media loading detection, responsive behavior with ResizeObserver, and support for multi-column spanning items, all in just 66 lines of JavaScript code.

8m read timeFrom css-tricks.com
Post cover image
Table of contents
What in the magic is this?!Let’s start with a polyfillMasonry layout made simpleStep by stepWaiting for media to loadMaking it responsiveMasonry grid with Splendid Labz

Sort: