Source Code

<div class="container py-5">
  <div class="row g-2" id="bslightGrid">
    <div class="col-4"><div class="bslight-thumb" style="--h:230" data-idx="0"></div></div>
    <div class="col-4"><div class="bslight-thumb" style="--h:20" data-idx="1"></div></div>
    <div class="col-4"><div class="bslight-thumb" style="--h:150" data-idx="2"></div></div>
    <div class="col-4"><div class="bslight-thumb" style="--h:300" data-idx="3"></div></div>
    <div class="col-4"><div class="bslight-thumb" style="--h:60" data-idx="4"></div></div>
    <div class="col-4"><div class="bslight-thumb" style="--h:180" data-idx="5"></div></div>
  </div>
</div>

<div class="modal fade" id="bslightModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered modal-lg">
    <div class="modal-content bslight-modal-content">
      <button type="button" class="btn-close btn-close-white position-absolute top-0 end-0 m-3" style="z-index:2" data-bs-dismiss="modal" aria-label="Close"></button>
      <div class="bslight-stage">
        <button class="bslight-nav bslight-prev" id="bslightPrev" aria-label="Previous image">‹</button>
        <div class="bslight-full" id="bslightFull"></div>
        <button class="bslight-nav bslight-next" id="bslightNext" aria-label="Next image">›</button>
      </div>
      <div class="text-center py-2 text-white-50 small" id="bslightCounter">1 / 6</div>
    </div>
  </div>
</div>

Bootstrap Image Lightbox Gallery Modal — Free Snippet

Bootstrap Image Lightbox Gallery Modal · Modals · Plain HTML, CSS & JS · Live preview

What's included

Features

Real Bootstrap 5.3 Modal component reused as a single shared lightbox for the whole gallery
Opens directly to whichever thumbnail was clicked, via a shared index rather than per-image modals
One render() function keeps the image and the counter always in sync
Prev/next navigation wraps around at both ends of the gallery
Left/right arrow-key navigation while the lightbox is open
CSS-gradient images — no photography required to try the snippet

About this UI Snippet

Bootstrap Image Lightbox Gallery Modal — HTML, CSS & JavaScript

Screenshot of the Bootstrap Image Lightbox Gallery Modal snippet rendered live

A thumbnail grid needs a way to view any image full-size without leaving the page — this snippet builds that on real Bootstrap 5.3's Modal component, opened programmatically (modal.show()) with whichever thumbnail's index was clicked, rather than each thumbnail needing its own separate modal element.

One shared render() function updates both the full-size image and the "N / 6" counter from a single current index — the prev/next buttons and clicking a different thumbnail all just change that one number and call render(), which is what keeps the counter, the displayed image, and the navigation buttons' behavior all consistent no matter how you got there. A keydown listener scoped to the modal element adds left/right arrow-key navigation, a gallery-lightbox expectation Bootstrap's Modal doesn't provide by itself.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet's HTML, CSS, and JS to an AI coding assistant like Claude and ask it to add swipe gesture support for mobile, or to add a thumbnail strip inside the lightbox itself for quicker jumping between images. It's also a good exercise to ask the assistant to add a zoom-on-click feature for the full-size image.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a Bootstrap 5.3 image lightbox using a shared modal, using the real Bootstrap CDN framework (bootstrap.min.css and bootstrap.bundle.min.js), not custom CSS made to resemble Bootstrap.

Requirements:
- A grid of at least six clickable thumbnails (CSS-gradient placeholders are fine, no real images required).
- One single real Bootstrap modal (not one per thumbnail) that opens via JavaScript (modal.show()) to whichever thumbnail's index was clicked, displaying that image full-size along with a "current / total" counter.
- Prev/next buttons inside the modal that navigate the gallery, wrapping around at both ends (from the last image back to the first, and vice versa), updating both the displayed image and the counter from one shared index value.
- Left and right arrow key support for the same navigation while the modal is open, scoped so it doesn't interfere with arrow key usage elsewhere on the page.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Step by step

How to Use

  1. 1
    Load the snippetClick the snippet in the sidebar Library tab. The preview loads a 6-thumbnail grid.
  2. 2
    Click any thumbnailA real Bootstrap modal opens showing that image full-size, with a "N / 6" counter.
  3. 3
    Click the arrow buttonsNavigate to the next or previous image — the counter and image both update, wrapping around at either end.
  4. 4
    Use the arrow keysWith the modal open, press the left/right arrow keys on your keyboard for the same navigation.
  5. 5
    Close and reopen a different thumbnailThe lightbox opens directly to whichever thumbnail you clicked, not always the first image.

Real-world uses

Common Use Cases

Portfolio and product photo galleries
The standard click-to-enlarge lightbox pattern, built on one shared Bootstrap modal instead of a dedicated lightbox library.
Learning to reuse one modal for many items
A clear example of a single modal instance driven by a changing index, rather than generating a modal per gallery item.
Documentation screenshot galleries
Reuse the same pattern for a docs page's screenshot grid, letting readers step through images without leaving the page.
E-commerce product image galleries
Adapt the thumbnail grid and lightbox for a single product's multiple angle shots.

Got questions?

Frequently Asked Questions

No — there's exactly one Bootstrap modal for the whole gallery, and clicking a thumbnail sets a shared current index before opening it, which is what lets prev/next simply increment or decrement that one number.

Both are updated together inside one render() function, called whenever current changes — from a thumbnail click, a prev/next click, or an arrow-key press — so they can never show conflicting values.

Only while the lightbox modal is open — the keydown listener is attached to the modal element itself, so arrow key presses elsewhere on the page don't affect it.

It wraps around to the first image — the index calculation uses modulo arithmetic ((current + 1) % length), so navigation never dead-ends at either end of the gallery.

No — every image is a CSS gradient generated from a hue value, so the gallery works immediately. Replace .bslight-full and .bslight-thumb's backgrounds with real image URLs when ready.