Skip to the main content

Image Gallery with Lightbox

FreeUses XON Media

A thumbnail grid whose items open a native dialog with Previous, Next, arrow keys, Escape and a caption; the artwork is six local SVG drawings.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="gal" aria-labelledby="gal-title">
  <header class="gal__head">
    <h2 class="gal__title" id="gal-title">Image gallery</h2>
    <p class="gal__lede">Six drawings made for this snippet. Open one to see it larger, then use the arrow keys.</p>
  </header>

  <ul class="gal__grid" data-each="gal.items" data-each-key="id">
    <li class="gal__cell">
      <button class="gal__thumb" type="button" data-index="{$index}" data-on-click="openAt" data-open="gal-dialog">
        <img class="gal__img" data-attr-src="{src}" alt="" width="400" height="300">
        <span class="gal__name">{title}</span>
      </button>
    </li>
  </ul>

  <dialog class="gal__dialog" id="gal-dialog" aria-labelledby="gal-caption" data-on-click="backdrop" data-on-keydown="keys">
    <div class="gal__panel">
      <div class="gal__bar">
        <p class="gal__count" role="status" data-text="gal.counter"></p>
        <button class="gal__close" type="button" data-close>Close<span class="gal__sr"> the large image</span></button>
      </div>
      <figure class="gal__figure">
        <div class="gal__frame"><img class="gal__big" data-attr-src="gal.src" data-attr-alt="gal.alt" alt="" width="400" height="300"></div>
        <figcaption class="gal__caption"><strong id="gal-caption" data-text="gal.title"></strong><span data-text="gal.note"></span></figcaption>
      </figure>
      <div class="gal__nav">
        <button class="gal__step" type="button" data-on-click="previous"><span aria-hidden="true">&larr;</span> Previous</button>
        <button class="gal__step" type="button" data-on-click="next">Next <span aria-hidden="true">&rarr;</span></button>
      </div>
    </div>
  </dialog>
</section>

CSS

.gal {
  --gal-surface: #ffffff;
  --gal-ink: #1c2230;
  --gal-muted: #4f596d;
  --gal-line: #dbe0ea;
  --gal-wash: #f2f4f9;
  --gal-accent: #2850c8;
  --gal-accent-ink: #ffffff;
  --gal-ring: #1c2230;
  box-sizing: border-box;
  container-type: inline-size;
  width: min(100%, 44rem);
  padding: 1.5rem;
  color: var(--gal-ink);
  background: var(--gal-surface);
  border: 1px solid var(--gal-line);
  border-radius: 18px;
  box-shadow: 0 1px 2px rgb(28 34 48 / 6%), 0 14px 32px -14px rgb(28 34 48 / 24%);
  font: 1rem/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.gal *, .gal *::before, .gal *::after { box-sizing: border-box; }
.gal__sr { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.gal__title { margin: 0; font-size: 1.35rem; line-height: 1.25; }
.gal__lede { margin: 0.3rem 0 1.1rem; max-width: 32rem; font-size: 0.9rem; color: var(--gal-muted); }
.gal__grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem; margin: 0; padding: 0; list-style: none; }
.gal__thumb { display: block; width: 100%; padding: 0; font: inherit; text-align: left; color: inherit; background: var(--gal-wash); border: 1px solid var(--gal-line); border-radius: 14px; overflow: hidden; cursor: pointer; }
.gal__img { display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; object-fit: cover; transition: transform 0.25s ease; }
.gal__thumb:hover .gal__img { transform: scale(1.04); }
.gal__thumb:focus-visible { outline: 3px solid var(--gal-ring); outline-offset: 3px; }
.gal__name { display: block; padding: 0.55rem 0.75rem 0.65rem; font-size: 0.9rem; font-weight: 700; }

.gal__dialog { width: min(94vw, 52rem); max-width: none; max-height: 94vh; padding: 0; color: var(--gal-ink); background: var(--gal-surface); border: 1px solid var(--gal-line); border-radius: 18px; box-shadow: 0 30px 80px -20px rgb(0 0 0 / 55%); overflow: hidden; }
.gal__dialog::backdrop { background: rgb(12 14 22 / 72%); }
.gal__panel { display: grid; gap: 0.75rem; padding: 1rem; max-height: 94vh; overflow: auto; }
.gal__bar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.gal__count { margin: 0; font-size: 0.9rem; font-weight: 700; color: var(--gal-muted); }
.gal__close { min-height: 44px; padding: 0 1rem; font: inherit; font-weight: 700; color: var(--gal-ink); background: var(--gal-wash); border: 0; border-radius: 12px; cursor: pointer; }
.gal__figure { display: grid; gap: 0.6rem; margin: 0; }
.gal__frame { display: grid; place-items: center; background: #11141c; border-radius: 12px; overflow: hidden; }
.gal__big { display: block; width: auto; max-width: 100%; max-height: 52vh; height: auto; aspect-ratio: 4 / 3; }
.gal__caption { display: grid; gap: 0.1rem; }
.gal__caption span { font-size: 0.9rem; color: var(--gal-muted); }
.gal__nav { display: flex; justify-content: space-between; gap: 0.75rem; }
.gal__step { flex: 1; min-height: 46px; padding: 0 1rem; font: inherit; font-weight: 700; color: var(--gal-accent-ink); background: var(--gal-accent); border: 0; border-radius: 12px; cursor: pointer; }
.gal__step:hover, .gal__close:hover { filter: brightness(1.08); }
.gal__close:focus-visible, .gal__step:focus-visible { outline: 3px solid var(--gal-ring); outline-offset: 3px; }
@container (min-width: 30rem) {
  .gal__grid { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 26rem) {
  .gal { padding: 1rem; }
  .gal__panel { padding: 0.75rem; }
}
@media (prefers-color-scheme: dark) {
  .gal {
    --gal-surface: #171c2a; --gal-ink: #eceff8; --gal-muted: #adb6cb; --gal-line: #2d3652; --gal-wash: #212842;
    --gal-accent: #8fb0ff; --gal-accent-ink: #0d1530; --gal-ring: #eceff8;
    box-shadow: 0 1px 2px rgb(0 0 0 / 35%), 0 16px 34px -14px rgb(0 0 0 / 70%);
  }
}
@media (prefers-reduced-motion: reduce) {
  .gal__img { transition: none; }
}

JavaScript

(function () {
  'use strict';
  var gal = xon.data('gal', {
    index: 0,
    items: [
      {
        id: 'dune', src: 'assets/dune-lines.svg',
        title: 'Dune lines',
        note: 'Sand bands under a low orange sun.',
        alt: 'Layered sand dunes under a low orange sun'
      },
      {
        id: 'harbour', src: 'assets/harbour-grid.svg',
        title: 'Harbour grid',
        note: 'Three small boats on a night grid of water.',
        alt: 'Three small sailboats on dark blue water with a grid of lines'
      },
      {
        id: 'orchard', src: 'assets/night-orchard.svg',
        title: 'Night orchard',
        note: 'Four round trees beside a crescent moon.',
        alt: 'Four round green trees with orange fruit under a crescent moon'
      },
      {
        id: 'hills', src: 'assets/paper-hills.svg',
        title: 'Paper hills',
        note: 'Cut-paper hills in pink and plum.',
        alt: 'Layers of cut-paper hills in pink, rose and plum'
      },
      {
        id: 'tide', src: 'assets/tide-pattern.svg',
        title: 'Tide pattern',
        note: 'Rings in two blues repeated like scales.',
        alt: 'A repeating pattern of blue rings'
      },
      {
        id: 'bloom', src: 'assets/signal-bloom.svg',
        title: 'Signal bloom',
        note: 'A violet flower with a gold centre.',
        alt: 'A violet flower with eight petals and a gold centre'
      }
    ]
  });
  var dialog = document.getElementById('gal-dialog');

  ['src', 'title', 'note', 'alt'].forEach(function (field) {
    xon.calc('gal.' + field, function (s) { return s.items[s.index][field]; });
  });
  xon.calc('gal.counter', function (s) { return (s.index + 1) + ' of ' + s.items.length; });

  function go(step) { gal.index = (gal.index + step + gal.items.length) % gal.items.length; }
  function openAt(event, button) { gal.index = Number(button.dataset.index); }
  function previous() { go(-1); }
  function next() { go(1); }
  function keys(event) {
    if (event.key === 'ArrowLeft') { event.preventDefault(); go(-1); }
    if (event.key === 'ArrowRight') { event.preventDefault(); go(1); }
  }
  function backdrop(event) { if (event.target === dialog) dialog.close(); }
  xon.use(openAt, previous, next, keys, backdrop);
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • The lightbox is a native modal dialog, named by the picture title, so focus stays inside and Escape closes it.
  • Left and right arrow keys move to the previous and next picture.
  • Focus returns to the thumbnail that opened the dialog.
  • The position (3 of 6) is a status region and each picture has a text description.