Add a masonry gallery to WordPress without a heavy plugin
Published Updated 3 min read
The WordPress gallery block lays images out in a grid, with controls for column count, spacing and cropping. Masonry, where images of different heights fill the space without padding each row to one height, is not an option. There are open tracking issues and it may arrive one day, but for now the block ships without it.
The usual answer has been a gallery plugin, which offers masonry as one layout inside a system that also does lightboxes, slideshows, albums, social sharing and shop-ready display modes. That is a lot of surface for a site that only wants images stacked sensibly in columns without cropping.
A plugin that adds one block to the inserter and renders a masonry layout is about a hundred lines across two files: short enough to build with AI from a plain-English prompt and read through before you install it.
Why the native gallery block does not have masonry
The gallery block was designed to be predictable, accessible and correct in every context, email clients and RSS readers included. In masonry one image's height moves the next, which takes either JavaScript reflow or CSS columns, and both sit outside the block's current scope; the block team has concentrated on consistent cropped grids. Requests exist, with no timeline.
So masonry has belonged to gallery plugins for years, and installing one for it brings lightboxes, album management, gallery post types, video support, watermarking and a settings page of options. For a site that only wants images at their natural height in columns, almost none of it is relevant.
One block, CSS columns, no JavaScript
The block lets you pick images from the media library, with an inspector panel for two to five columns and a gap in pixels. It stores image IDs as block attributes and renders from PHP, so nothing needs building and no JavaScript library runs on the front end. Every image gets loading=lazy.
The layout is CSS columns, a standard property every browser supports. Images keep their natural aspect ratio: a tall one takes more of its column, a short one less, and the next drops in underneath. A media query collapses it to one column on mobile. That stacking is the masonry effect, and in CSS it is three lines. The native block, by contrast, crops images to fill a uniform grid.
Each block keeps its own column and gap settings, so one page can have three columns and another four without touching code. The effect shows best when images vary in height; a set of identically sized images will look like a plain grid.
Who should stay on a gallery plugin
A layout block will not give you a lightbox, because a full-screen overlay on click needs JavaScript. A separate lightweight lightbox plugin that targets linked images across the site can provide it, once each image is wrapped in a link to its full-size file.
Galleries that mix video with images, or that visitors can filter by tag or sort by date, are a real scope that gallery plugins cover and a single block does not.
A client managing dozens of galleries on a content-heavy site can use the media library picker, but a full gallery plugin's drag-and-drop controls are friendlier for people who do not write code.
A note on captions
Alt text is used for screen readers, but no visible caption is shown. Captions are awkward in masonry: the columns break any shared row baseline, so a caption has to sit under each image or appear over it on hover.
A follow-up message can render the attachment's caption field either way, as a small paragraph under each image or as a hover overlay, both small changes to the render callback. The same goes for swapping CSS columns for Masonry.js if you want JavaScript reflow that places images precisely.
What you get
Included
- A Gutenberg block that selects multiple images from the WordPress media library
- CSS columns masonry layout with configurable column count (2 to 5) and gap size
- PHP render callback with no JavaScript build step required
- Responsive single-column layout on mobile via CSS media query
- Lazy loading on all images via the loading=lazy attribute
Not included
- Lightbox or image zoom on click: needs a separate JavaScript library
- Video files or mixed media in the same gallery
- Visible captions below or over each image (can be added via follow-up prompt)
- Drag-and-drop reordering of images within the block
- Gallery filtering or sorting by category, tag, or date from the front end
Envira Gallery does the things on the right. This build covers the part most sites use.
Questions
Will this block work inside Elementor or Divi?
No. Elementor and Divi use their own editors and do not recognise Gutenberg blocks; this works in the standard block editor. On a page-builder site, use that builder's own gallery or image grid widget.
Build it in Steem
The button opens the builder with this guide's prompt filled in, so you can change anything before it runs. Nothing is built, and nothing is charged, until you send it.
Read the prompt (255 words)
Build a WordPress plugin called Masonry Gallery that registers a Gutenberg block with namespace masonry-gallery/block. Write the editor JavaScript as plain uncompiled JS with no JSX and no build step, enqueued on enqueue_block_editor_assets. Block attributes: images (array of objects each with id, url, alt, width, height; default empty array), columns (integer default 3), gap (integer default 16). The edit function uses MediaUpload and MediaUploadCheck from wp.blockEditor to open the media library in multiple-select mode; when images are selected store them in the images attribute. Show selected images in the editor as a div using CSS column-count equal to the columns attribute with column-gap set to the gap attribute in pixels. Add an InspectorControls panel from wp.blockEditor with a PanelBody titled Layout containing a RangeControl for Columns (min 2, max 5, default 3) and a RangeControl for Gap in pixels (min 0, max 32, step 4, default 16). The save function returns null. Register the block in PHP with register_block_type passing the editor script handle and a render_callback. The render_callback outputs a div with inline style column-count equal to the columns attribute and column-gap equal to gap in pixels. Inside, render each image as a figure element with break-inside: avoid and margin-bottom equal to gap in pixels, containing an img with src, alt, width, height, loading=lazy, and style width: 100%; height: auto; display: block. Enqueue a separate front-end CSS file that adds a media query targeting max-width 600px where the gallery div gets column-count: 1. Do not use any external library, npm package, or build tool.
Steem