Breakdance4fun Logo

Easily Convert CSS Grids to Masonry Layouts

Mason-it is a lightweight JavaScript library that instantly transforms a standard CSS grid into a responsive masonry layout while fully respecting your existing grid-template-columns, gap, and other styles.

The example below doesn't come from the gallery element but features a simple Div containing images. This Div is set up as a CSS grid and is transformed into a masonry layout using Mason-it.

Important: If your grid contains images, do not enable the lazy-mode option. If you have enabled it, make sure to set the width and height HTML attributes for each image.

 

Performance gaming PC with RGB lighting, powerful components, and a sleek case, set against a mountain landscape background. Perfect for gaming, streaming, and creative work.Laptop with pixelated green leaves on screen, connected by a pixel chain, placed on a tree stump in a forest setting, symbolizing digital nature or eco-friendly technology.Custom gaming PC with pixel art design surrounded by wildlife including deer, birds, and penguins in a natural outdoor setting. Perfect for gaming and digital art enthusiasts.Retro pixel art gaming computer setup with pixelated skull and heart designs, accompanied by a cat resting on a wooden desk near a potted plant, in a bright room.A digitally enhanced forest scene with a flowing stream, surrounded by trees, overlaid with colorful glitch effects and circuit-like graphics, emphasizing a fusion of nature and technology.Vintage computer monitor displaying vibrant pixel art with a black and white background, on a wooden desk.Dynamic digital artwork featuring the Breakdance4Fun logo overlaid on a black-and-white forest landscape with colorful circuit-like patterns. Perfect for promoting urban dance and street culture event.Pixel art wolf with digital circuit background and mountain landscape, blending nature and technology themes.

How to transform a grid into a masonry

The setup is quite straightforward. Simply load the main library from a CDN or copy the file to your server. Next, create a CSS grid and add the data-mason-it attribute to it.

<script src="https://cdn.jsdelivr.net/npm/mason-it@1.0.1/dist/mason-it.min.js"></script>
Code block mason it

Example with a Div Element

First, set the layout to grid and ensure that the Advanced option "Item Vertical Alignment" is set to Start. In CSS, this corresponds to `align-items: flex-start`.

Next, add the `data-mason-it` attribute.

That's it! Your grid will be transformed into a masonry layout!

 

Grid masonry div settingsGrid masonry div attribute

Example with the Post Loop Builder, Repeater & Post List elements

The Post Loop Builder, Repeater, and Post Lists elements all have a masonry option. However, this feature can be unstable and may even break the layout in some cases.

To use Mason-it with these elements, simply select the Grid layout and add an ID. This allows us to apply the necessary CSS and initialize the script. Note that we cannot use the data attribute method here, as the CSS grid is not directly applied to these elements but rather to their children.

IMPORTANT: In the Post Loop Builder, this masonry option does not work when the filter bar option is enabled, as that option already utilizes a different masonry library.

Grid masonry post loop settingsGrid masonry post loop id

Finally, in the initial Code Block, add the following CSS and JavaScript snippets:

#myCoolMasonry .bde-loop {
align-items: start;
}
// When the DOM is ready:
MasonIt.init('#myCoolMasonry .bde-loop-grid');
Grid masonry post loop code block

More info in the official Mason-it page : https://druh.in/mason-it/