DEV Community

Product image galleries: thumbnails are buttons, and other things carousels forget

Small parts are where product photos earn their keep. Someone looking for a replacement faucet aerator wants to see the thread, the gasket, the side profile next to a coin.

So the gallery on a product page matters more than it looks, and it is usually built as a carousel copied from somewhere and never touched again. Here are the bits I check whenever I review one.

Thumbnails are buttons, not links and not divs

A thumbnail that swaps the main image does not navigate anywhere. That makes it a <button>, not an <a href="#"> and definitely not a <div onclick>.

<div class="gallery">
  <img id="main-photo" src="aerator-front.jpg" alt="Chrome faucet aerator, front view showing the mesh screen" width="800" height="800">
  <ul class="thumbs" aria-label="Product photos">
    <li>
      <button type="button" aria-pressed="true" data-src="aerator-front.jpg" data-alt="Chrome faucet aerator, front view showing the mesh screen">
        <img src="aerator-front-thumb.jpg" alt="" width="80" height="80">
        <span class="visually-hidden">Photo 1 of 3: front view</span>
      </button>
    </li>
    <li>
      <button type="button" aria-pressed="false" data-src="aerator-thread.jpg" data-alt="Side view of the aerator showing the outer thread and rubber gasket">
        <img src="aerator-thread-thumb.jpg" alt="" width="80" height="80">
        <span class="visually-hidden">Photo 2 of 3: thread and gasket</span>
      </button>
    </li>
  </ul>
</div>

You get keyboard focus, Enter and Space, and a correct role for free. aria-pressed tells screen reader users which photo is showing.

The thumbnail image itself gets an empty alt because the button already has a name from the hidden text; otherwise you end up reading the same description twice.

Swap the alt text with the image

The most common bug I see: the main src changes, the alt stays on "front view" forever. If you swap one, swap both.

document.querySelectorAll(".thumbs button").forEach((btn) => {
  btn.addEventListener("click", () => {
    const main = document.getElementById("main-photo");
    main.src = btn.dataset.src;
    main.alt = btn.dataset.alt;
    document.querySelectorAll(".thumbs button").forEach((b) => b.setAttribute("aria-pressed", String(b === btn)));
  });
});

Do not move focus to the main image after the click. The person chose a thumbnail; leave them on it so they can keep going through the list.

Don't autoplay it

Auto-rotating product photos are the carousel habit I would drop first. Someone is zooming into the thread pitch and the image slides away.

If it really has to rotate, it needs a visible pause button and it has to stop on hover and on focus. WCAG 2.2.2 (Pause, Stop, Hide) is the rule people point to, but honestly the bigger reason is that it's annoying.

Swipe is extra, not the only way

On a phone, swiping the main image is expected. Fine, add it. Just keep the thumbnails (or visible previous/next buttons) so the gallery still works with a keyboard, a switch device, or a mouse on a desktop.

A gallery that only responds to touch gestures fails WCAG 2.5.1 for anyone who can't do the gesture.

If you use CSS scroll snap for the swipe, you get a lot for free:

.slides { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; }
.slides > img { flex: 0 0 100%; scroll-snap-align: center; }

Native scrolling means momentum, no JS gesture library, and it doesn't fight the browser's back-swipe.

Zoom without traps

Hover-to-zoom lenses don't exist on touch screens and are hard to use with a keyboard. Two patterns that hold up better:

  • A button on the main image ("View larger") that opens the full-size photo in a <dialog>. Use showModal(), so focus is trapped inside and Escape closes it, and return focus to the button on close.
  • Let pinch-zoom work. Never ship user-scalable=no or maximum-scale=1 in the viewport meta. People zoom product photos with their fingers all the time.

Layout shift and loading

Give the main image and thumbnails width and height (or an aspect-ratio) so the page does not jump when they load.

The first main photo is often the largest element on the page, so it should not get loading="lazy"; the other gallery photos and the hidden slides can.

Quick review list

  • Thumbnails are <button> elements with a name and aria-pressed
  • Main image alt changes together with src
  • No autoplay, or a real pause control
  • Swipe plus a non-gesture way to change photos
  • Zoom in a <dialog>, pinch-zoom not blocked
  • Dimensions set, first image not lazy

None of this needs a library. Most of it is choosing the right element and remembering that the image and its text belong together.

If you want a feel for how much the photos matter on tiny parts, scroll through a category like faucet parts and accessories and notice which listings you trust just from the side view. That's what the gallery is for.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.