Live Demo: https://masonry-css-js.netlify.com/
React: https://github.com/jessekorzan/masonry-css-js
JQuery: https://github.com/jessekorzan/css-masonry
The problem… column-count works great except for left-to-right scan reading
Masonry Layouts… popularized by sites like Pinterest. Loved by non-coding designers and holy-grailed in corporate boardrooms.
Naturally, many sharp front-enders have worked on solutions. We have several libraries, techniques and hacks to achieve this marvel of content display.
There are times when a masonry layout is an appropriate design decision. Depending on what else that design has to accommodate, these solutions might be a good fit.
For me, especially in exploratory efforts, a pure-CSS method is a flexible and fast way to iterate. I don’t want to rely on DOM manipulation or end up with a contrived and constrained JS solution. This often takes the design off course (opportunity costs) or produces an awkward handoff with the real engineers. “Dude, srsly? 213 lines in jQuery to display some boxes?”
My problem is relatively mundane.
Using the React demo here, https://github.com/jessekorzan/masonry-css-js, you can grok what’s going.
The main take away here... You can use CSS column-count “as is” for a masonry layout. If you want a nice left-to-right scan reading experience, you simply re-order the array of content before you render to the DOM.
Not intending this to be a tutorial, here’s the gist of what’s happening:
https://masonry-css-js.netlify.com/
Magic algo to re-order the array BEFORE output to DOM
Couple notes:
So there you have it. And the DOM is 100% CSS. Which is what I need to keep moving forward with my current effort on a feature.
—
I share this here for other “designers who code”. Maybe it helps in a prototype or reduce the cringing when the brief has that line… you know that line.
“A Pinterest-style layout is required because ____________ and __________.”
—
Live Demo: https://masonry-css-js.netlify.com/