Skip to content

Masonry gallery in WordPress

A masonry grid of photos with varying heights, with no layout script, where each photo opens large with keyboard navigation.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

galerie-maconnerie.php
<?php
/**
 * Plugin Name: Forge, Galerie en mosaïque
 * Description: Une mosaïque de photos à hauteurs variables, sans script de mise en page, où chaque photo s’ouvre en grand avec navigation au clavier. Ajoute la composition « Galerie en mosaïque » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-galerie-maconnerie
 *
pattern.html
<!-- wp:html -->
<section class="gm" data-gm aria-labelledby="gm-titre">
        <p class="gm-eyebrow">Maçonnerie Duval · réalisations</p>
        <h2 class="gm-title" id="gm-titre">Nos chantiers en images</h2>
        <p class="gm-lead">Touchez une photo pour l’agrandir ; dans la vue agrandie, les flèches du clavier passent d’une photo à l’autre.</p>
        <ul class="gm-grid" role="list">
          <li class="gm-item">
            <figure class="gm-figure">
              <button class="gm-tile" type="button" data-gm-open data-gm-alt="Façade en briques rénovée, Lille" aria-label="Agrandir : Façade en briques rénovée, Lille">
                <span class="gm-art" data-gm-art style="--gm-ratio: 3/4; --gm-bg: repeating-linear-gradient(0deg, #e8e0d4 0 3px, transparent 3px 24px), repeating-linear-gradient(90deg, #e8e0d4 0 3px, transparent 3px 48px), linear-gradient(135deg, #b5523b, #8e3b2a)"></span>
                <span class="gm-zoom"><svg class="gm-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"/></svg></span>
              </button>
script.js
(() => {
  if (!document.querySelector(".gm")) return;
  document.querySelectorAll("[data-gm]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const dialog = root.querySelector("[data-gm-dialog]");
    const photo = root.querySelector("[data-gm-photo]");
    const caption = root.querySelector("[data-gm-caption]");
    const count = root.querySelector("[data-gm-count]");
    const tiles = Array.from(root.querySelectorAll("[data-gm-open]"));
    let index = 0;

    const show = (next) => {
style.css
:root {
  --gm-card: #ffffff;
  --gm-text: #24201b;
  --gm-muted: #5b5248;
  --gm-accent: #a3461f;
  --gm-chip: rgb(255 255 255 / 0.92);
  --demo-galerie-maconnerie-bg: #ece6dd;
  --demo-galerie-maconnerie-text: #24201b;
}

@media (prefers-color-scheme: dark) {
  :root.forge-follows-system:not([data-theme="light"]) {

Free component: copy it freely, including for commercial projects.

Galleries and carousels components in WordPress
  • A product photo that zooms in under the pointer, and from the keyboard with Enter and the arrow keys, to inspect the details.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Building site photos sorted by dated step on a vertical timeline, with the current step highlighted.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • YouTube videos shown as thumbnails: the player only loads on click, to respect privacy and keep the page light.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A slideshow that fills the whole screen, browsed with the keyboard or a finger swipe, and closed with Escape.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component