webdevpuneet.com

Bootstrap Image Lightbox Gallery Modal — Free Snippet

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

Copy & paste

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>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

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.

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.