The original lightbox script. Now with fluid animations and touch‑first gestures.
Getting started
A lightbox is a helper which displays enlarged, almost screen-filling versions of images while dimming the remainder of the page. The technique, introduced by Lightbox Version 2, gained widespread popularity thanks to its simple style. The term lightbox has been employed since then for Javascript libraries to support such functionality.
The new version of Lightbox V2 (by Lokesh Dhakar) with fluid animations and touch‑first gestures.
Markup
Add data-lightbox to any <a> element. The href is the full-size image URL.
<a href="photo.jpg" data-lightbox>
<img src="thumb.jpg">
</a>To create a gallery, give each link the same data-lightbox value.
<a href="photo-1.jpg" data-lightbox="gallery">
<img src="thumb-1.jpg">
</a>
<a href="photo-2.jpg" data-lightbox="gallery">
<img src="thumb-2.jpg">
</a>Script tag
Load the CSS and JavaScript directly. Lightbox3 will auto-initialize on all [data-lightbox] elements.
<link rel="stylesheet" href="lightbox3.css">
<script src="lightbox3.min.js"></script>Coming from Lightbox2
Lightbox3 uses the same markup pattern you already know — mostly the same HTML, way better UX.
| What changed | Details |
|---|---|
No jQuery | Lightbox3 is zero-dependency. Remove jQuery and the Lightbox2 plugin script. |
Same | Your existing |
| Lightbox3 reads |
New features | Pinch-to-zoom, swipe navigation, swipe-to-dismiss, spring animations — all built in, no configuration needed. |
API reference
Data attributes
| Attribute | Description |
|---|---|
| Add to an |
| Group links into a navigable gallery. All links sharing the same value form a set. |
| Caption text displayed below the image. Also accepts |
| Alt text for the full-size image. Falls back to the thumbnail’s |
Initialization
| Method | Description |
|---|---|
| Initialize and return a singleton instance. Scans the DOM for |
Options
| Option | Type | Default | Description |
|---|---|---|---|
|
|
| CSS selector for trigger elements. |
|
|
| Spring config for open and zoom-in animations. |
|
|
| Spring config for close and zoom-out animations. |
|
|
| Viewport padding in pixels around the opened image. |
|
|
| Show debug overlay with spring values and state. Also enabled via |
Methods
| Method | Description |
|---|---|
| Programmatically open an image. |
| Close the lightbox. |
| Navigate to the next image in the gallery. |
| Navigate to the previous image in the gallery. |
| Remove all event listeners, cancel animations, and tear down the instance. |
Events
| Event | Description |
|---|---|
| Fired when the open animation starts. |
| Fired when the open animation completes. |
| Fired when the close animation starts. |
| Fired when the close animation completes and the overlay is removed. |
| Fired when navigating to a different image in a gallery. |
| Fired when zoom-in starts (tap or double-click). |
| Fired when zoom-out starts. |
Subscribe with lb.on(event, callback) and unsubscribe with lb.off(event, callback). Callbacks receive a { src, triggerEl, index, total } detail object.
CSS custom properties
Override these on :root or any parent element to customize the lightbox appearance.
| Property | Default | Description |
|---|---|---|
|
| Overlay backdrop color. |
|
| Border radius of the opened image. |
|
| Padding between the image and viewport edges. |
|
| Background color of the caption bar. |
|
| Text color in the caption bar. |
|
| Font size in the caption bar. |
|
| Padding around the caption bar and nav arrows. |
|
| Border radius of the caption bar. |
|
| Z-index of the lightbox overlay. |
Examples
Mixed aspect ratios
Portrait, landscape, square. Thumbnails can be cropped to any aspect ratio and will seamlessly transition to the full-size image.
HTML in captions
Captions support links and line breaks.
<a
href="photo.jpg"
data-lightbox
data-caption="Photo by <a href='https://example.com'>Jane Doe</a>">
<img
src="thumb.jpg"
alt="Caption text">
</a>Text links
Links without thumbnails fade in instead of morphing from a source element.
HTML pattern:
<a
href="https://images.unsplash.com/photo-1542224566-6e85f2e6772f?w=2400&q=80"
data-lightbox="">
Cherry blossoms in bloom
</a>| Because of the missing <img> tag, no captions are available. |
Programmatic open
Open images from code. Without a trigger element, the image fades in from center.
Open image via API:
const lb = Lightbox3.Lightbox.init();
lb.open('https://example.com/photo.jpg');Events
Interact with any image above to see events logged here.
const lb = Lightbox.init();
lb.on('open', (e) => console.log('open', e.detail));
lb.on('closed', (e) => console.log('closed', e.detail));
lb.on('navigate', (e) => console.log('navigate', e.detail));
lb.on('zoomIn', (e) => console.log('zoomIn', e.detail));
lb.on('zoomOut', (e) => console.log('zoomOut', e.detail));