Skip to content

Gallery with Lightbox

Tailwind CSS mosaic image gallery that opens an accessible lightbox with captions, arrow-key and swipe navigation, focus trap and preloading.

Also known as: lightbox gallery, image gallery lightbox, photo gallery popup, masonry image gallery, mosaic photo grid

Gallery with Lightbox live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A responsive image gallery with a built-in lightbox, made with Tailwind CSS and plain JavaScript. Thumbnails sit in a mosaic grid with featured tiles, and clicking any of them opens a full-screen viewer with captions, a counter, previous and next buttons, keyboard shortcuts and swipe support. It works well for portfolios, case studies, event photos and product shots.

How it works

The root is section.tgl-gallery. The grid is a ul using grid-cols-2 md:grid-cols-4, fixed auto-rows-[9rem] and grid-flow-dense, so tiles with col-span-2 or row-span-2 pack without gaps. Each tile is a button.tgl-item holding a thumbnail img.tgl-img and a .tgl-hover caption bar. The large version and caption live in data-tgl-full and data-tgl-caption on the button.

The .tgl-lightbox dialog starts with the hidden attribute and contains a dark .tgl-backdrop, a counter (data-tgl-index / data-tgl-count), a close button, a figure with the data-tgl-image element and caption, and data-tgl-prev / data-tgl-next buttons.

On DOMContentLoaded the script initializes every .tgl-gallery. A click on a tile stores the focused element, calls show() to load the full image, removes hidden, locks page scrolling and focuses the close button. While a new file loads, data-tgl-loading fades the image out; the load event fades it back in. show() also preloads the neighbouring images. A keydown listener handles Escape, ArrowLeft, ArrowRight, Home and End and traps Tab inside the dialog. A horizontal touchend swipe of more than 50 pixels changes the image. Clicking the backdrop or anything marked data-tgl-close closes the viewer and returns focus to the tile that opened it.

Customization

  • data-tgl-full and data-tgl-caption on each .tgl-item: point to your high-resolution file and set the caption; without data-tgl-full the thumbnail is reused.
  • col-span-2 and row-span-2 on the li elements: choose which tiles are featured, or remove them for a uniform grid.
  • md:grid-cols-4 and auto-rows-[9rem] sm:auto-rows-[11rem] on the ul: change the column count and tile height.
  • bg-zinc-950/95 on .tgl-backdrop: lower the opacity to let the page show through.
  • from-violet-950/80 on .tgl-hover and hover:bg-violet-500 on the arrows: swap violet for your brand color.
  • transform: scale(1.06) in style.css: increase or remove the hover zoom.

Use cases

  • Portfolios: a mosaic grid with featured tiles opens each work in a full-screen viewer
  • Case studies: captions and a counter guide visitors through project screenshots
  • Event photos: swipe or use arrow keys to move through pictures in the lightbox
  • Product shots: thumbnails load a larger image on click and preload the neighbouring ones

Accessibility

Every tile is a native button with aria-haspopup="dialog" and a descriptive alt text. The lightbox has role="dialog", aria-modal="true" and aria-labelledby pointing to the caption. Focus moves to the close button on open, stays trapped inside while open and returns to the tile on close. The counter is a polite live region, so each change is announced. Icon buttons have sr-only labels and decorative icons are aria-hidden. Focus outlines are clearly visible, hover captions are always shown on touch screens, and animations are disabled under prefers-reduced-motion: reduce.

Usage notes

Several galleries can share a page: each .tgl-gallery has its own lightbox, but give each copy a unique caption id.

Gallery with Lightbox at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile
Category
Galleries
License
Free for personal and commercial use

Questions about this template

Can I use the Gallery with Lightbox code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Gallery with Lightbox – free Tailwind CSS gallery template
Gallery with Lightbox · free Tailwind CSS gallery template

More Galleries

CSS Grid Bento Photo Gallery – free Pure CSS gallery template

CSS Grid Bento Photo Gallery

Bento-style photo gallery with mixed tile sizes from CSS Grid spans, hover captions and a keyboard-friendly dialog lightbox.

Polaroid Photo Gallery – free Pure CSS gallery template

Polaroid Photo Gallery

Pure CSS polaroid gallery on a cork board: tilted photo frames with tape and handwritten captions that straighten and lift on hover or focus.

Masonry Image Gallery – free Pure CSS gallery template

Masonry Image Gallery

Pure CSS masonry image gallery using multi-column layout, with hover and focus captions that slide up over each photo. No JavaScript needed.

Before and After Image Slider for Shopify – free Shopify gallery template

Before and After Image Slider for Shopify

Drag-to-compare before and after image slider for Shopify with vertical or horizontal divider, touch and keyboard control, and optional side text.

Filterable Portfolio Gallery – free Bootstrap 5 gallery template

Filterable Portfolio Gallery

Dark Bootstrap 5 portfolio gallery with pill filter buttons, automatic category counts, fade-in tiles and a screen reader status message.