Image Lightbox

Tailwind lightbox gallery viewer with prev/next controls.

3 / 12

Sunset over the ridge, edited in Lightroom

Tailwind CSS
<div class="w-full max-w-xl mx-auto rounded-2xl bg-slate-950/95 p-4">
  <div class="flex items-center justify-between mb-3">
    <span class="text-white/70 text-xs font-semibold">3 / 12</span>
    <button class="w-8 h-8 rounded-full bg-white/10 text-white flex items-center justify-center hover:bg-white/20" aria-label="Close">
      <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" d="M6 6l12 12M18 6L6 18"/></svg>
    </button>
  </div>
  <div class="relative rounded-xl overflow-hidden aspect-video bg-gradient-to-br from-indigo-500 via-purple-500 to-pink-500">
    <button class="absolute left-3 top-1/2 -translate-y-1/2 w-9 h-9 rounded-full bg-black/40 text-white flex items-center justify-center hover:bg-black/60" aria-label="Previous">
      <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7"/></svg>
    </button>
    <button class="absolute right-3 top-1/2 -translate-y-1/2 w-9 h-9 rounded-full bg-black/40 text-white flex items-center justify-center hover:bg-black/60" aria-label="Next">
      <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/></svg>
    </button>
  </div>
  <p class="text-white/70 text-sm text-center mt-3">Sunset over the ridge, edited in Lightroom</p>
</div>

Free to use in personal and commercial projects. No attribution required.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Light

Bright viewer for portfolios and shops with light themes.

3 / 12

Sunset over the ridge, edited in Lightroom

Tailwind CSS

Minimal

No counter or caption for distraction free viewing.

Tailwind CSS

Studio

Teal gradient artwork with product photography caption for shops.

1 / 8

Ceramic vase, studio lighting

Tailwind CSS

About this image lightbox

A tailwind lightbox opens an image in a focused viewer with prev/next navigation, keeping visitors in the gallery instead of bouncing to new tabs. This lightbox gallery tailwind snippet shows the full viewer chrome: counter, close button, arrow controls, and caption. It is distinct from a generic modal because it is built for browsing a set of images.

The viewer is a dark panel with the image in an aspect-video frame. Arrow buttons sit on the image edges with translucent black backgrounds so they work over any photo. The counter up top orients the visitor within the set. In production, JavaScript swaps the image source and updates the counter on arrow clicks or keyboard arrows.

Always include keyboard support: arrows to navigate, Escape to close. Preload the next image so navigation feels instant. Keep captions short; the image lightbox html css pattern is about the photo, not the text.

Thumbnails are too small to judge anything: product details, portfolio work, property photos. Opening the full image in a new tab breaks the browsing flow and many visitors never come back. The lightbox honors the click's intent instantly, showing the large image in place while keeping the gallery one Escape away. That micro-commitment, I want a closer look, gets rewarded instead of punished.

The viewer is a fixed overlay holding the image in an aspect-video frame, with arrow buttons on the image edges and a counter up top. JavaScript tracks the current index in an image array, swapping the src and updating the counter text on each navigation. The arrows use translucent black backgrounds so they stay usable over any photo. The counter does quiet work: Image 3 of 12 tells visitors there is more worth seeing.

Preload the neighboring images so arrow navigation feels instant instead of fetching on demand. Add thumbnail dots under the caption for direct jumps to a specific image. Offer a fullscreen toggle for large monitors where the aspect-video frame leaves room. Keep captions to one short line pulled from the thumbnail's alt text, since the photo is the point, not the prose.

Serving the original full-resolution upload in the viewer is the performance trap. A 5MB photo per navigation step makes arrows feel laggy and burns through mobile data plans. Generate a sized large variant, around 1600px wide, specifically for the lightbox, and reserve the original for download links. Fast navigation is part of the experience; waiting on each image defeats the purpose of browsing.

Where to use it

  • ✓ Portfolio galleries
  • ✓ Real estate listings
  • ✓ E-commerce product images
  • ✓ Photography blogs

Customization tips

Swap the gradient placeholder for an img tag. Add thumbnail dots below the caption for direct jumps, or a fullscreen toggle for large monitors.

Design notes

When to use

A lightbox fits galleries where images deserve full attention: portfolios, product photos, and photography sites. Opening the image in a focused viewer with prev and next keeps visitors browsing instead of bouncing to new tabs. It works best with a real collection where moving between images is the point.

What to avoid

Do not open a lightbox for a single image with no siblings, because the navigation chrome is pointless theater. Avoid lightboxing images whose surrounding text is essential, since the viewer strips that context away. And never trap visitors without an obvious close control, because a viewer with no exit feels like a hijack.

Accessibility

Trap focus inside the lightbox, close on Escape, support arrow-key navigation, and label the position like Image 2 of 8 for screen readers. Return focus to the thumbnail that opened it when the viewer closes.

Frequently asked questions

How do I build a tailwind lightbox with prev and next buttons?

Render a fixed overlay with the image, arrow buttons, and a counter. JavaScript tracks the current index in an image array and updates the src and counter text on each arrow click.

What is the difference between a lightbox and a modal dialog?

A modal dialog hosts arbitrary content like forms. A lightbox is specialized for image sets: it adds prev/next navigation, a position counter, and keyboard controls.

How do I close an image popup modal with the Escape key?

Add a keydown listener that hides the overlay when the pressed key is Escape. Also close on backdrop click, but stop propagation on the image itself.

How do I open a larger image without leaving the page?

That is exactly what a lightbox does: clicking a thumbnail opens the full image in a fixed overlay above the page. The native dialog element with showModal gives you focus trapping and Escape-to-close for free. Set the enlarged image src from the thumbnail link's href on click.

Should the lightbox close when clicking the dark backdrop?

Yes, most users expect it. Add a click handler on the overlay that closes only when the click target is the backdrop element itself, not the image or buttons. Closing on any click inside the lightbox would frustrate users trying to use prev and next controls.

How do I show image captions in a lightbox?

Pull the caption from the thumbnail's alt text or a dedicated data-caption attribute when opening the lightbox. Display it below the enlarged image in a muted, centered style. Keep captions to one short line so they never compete with the image.