A module for a hexagonal masonry layout with CSS and JavaScript

posted 1 min read

This is an announcement post for a new npm module, hexagonal-masonry-placement, released and maintained by me, Ingo Steinke, creative web developer based in Berlin, Germany.

A hex layout looks something like this.

hex layout website screenshot

Maybe it looks familiar from board games or online worlds, as the honeycomb tiling seemt to be the most efficient way to place items apart from the usual square layouts that most web forms and elements default to.

The above screenshot comes from my side project web app at https://dev-ux-lesezeichen.de/

While the CSS community has working drafts and experimental implementations for so-called masonry layouts, that place rectangular items in a shifted pattern like a brick wall, we can use a simple flex layout to fake more complex structures like in the screenshot above.

CSS flex and grid layouts can place items automatically in a responsive way, trying to fill the available screen space without cutting or hiding anything. That's super useful, and the result looks like a table with rows, only that there is no official built-in way to determine or act on which row an item is placed in. There are no :first-row or nth-row(2) pseudo-selectors in CSS.

I have wrapped my head around mathematical calculations to determine an items' row and set a custom CSS class name accordingly, until I found a much simpler solution that just compares each item's vertical top position with that of its predecessor and increment the row count when they differ.

I decided to publish the code as an npm module so I can refine the code and concept in one central place to avoid further confusion and possible inconsistent states of duplicated code across projects, if I ever use it anywhere else.The utility requires HTML markup and CSS styles similar to the example code provided. You can use the provided base styles in index.css or provide your own.

https://www.npmjs.com/package/hexagonal-masonry-placement

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Nice work on turning a tricky hexagonal layout idea into something reusable through this npm module. It’s neat to see creative geometry meet front-end practicality. Could this approach inspire new ways to handle responsive layouts beyond grids and flex?

More Posts

Still.js - A way to leverage Vanilla JavaScript for Complex and Enterprise Level Web Development

Nakassony Bernardo - Jun 29

CSS Pseudo-Elements: Enhance Your Projects with Style!

Michael Larocca - Sep 29

CSS Styling Images: From Thumbnails to Polaroids – A Complete Guide

Raj Aryan - Jul 15

Fix common CSS Grid layout issues like overflowing content, misalignment, and unexpected row creation.

Maxime - Feb 2

Debouncing in JavaScript: A Must-Know Technique for Developers

Mubaraq Yusuf - Feb 15
chevron_left