DEV3 Technologies home

Product gallery with thumbnails

A product image gallery with a large view, a thumbnail strip, arrow key navigation and an accessible selected state.

Tailwind CSSCardsgallerythumbnailsproductimages

Live preview

Code

How it works

What it solves

Shoppers want to inspect a product from several angles. A gallery needs one large picture, small selectors, and behaviour that works from the keyboard. This pattern swaps a single img rather than stacking slides, which keeps it light.

How it works

Thumbnails are buttons inside a labelled role="group": each button wraps an img and carries data-full and data-alt attributes. A click copies those values to the main image, sets aria-current="true" on the chosen thumbnail and clears it from the rest. The main image sits in a fixed aspect-square frame with object-contain, so changing pictures never shifts the layout. Left and Right arrow keys choose the previous or next item and focus it. A short opacity fade runs on swap through a class toggled for one frame. The strip scrolls horizontally with overflow-x-auto and snap-x on narrow screens.

Adapting it

Add loading="lazy" to thumbnails and preload the first large image. For zoom, wrap the main image in a container with overflow-hidden and scale on hover. Keep thumbnails to 4 to 6 so the strip stays scannable.

Pitfalls

  • Always update alt along with src, or assistive tech reads the old description.
  • Thumbnails must be buttons, not clickable images.
  • Reserve the main frame size to avoid layout shift.

How to use it

  1. Paste gallery.html into your product page and replace the image URLs; the thumbnails can reuse smaller versions.
  2. Keep each button's data-full and data-alt in sync with its picture.
  3. Load gallery.js with defer.
  4. Mark the first thumbnail with aria-current="true" in the markup for the initial state.

Licence: DEV3 Snippet Licence. Free to use, change and ship in personal and commercial projects. Attribution is appreciated, not required. Please do not redistribute snippets as a collection.

Related snippets