Mixed aspect ratios
Portrait, landscape, square. Thumbnails can be cropped to any aspect ratio and will seamlessly transition to the full-size image.
Zoom & pan
Click or tap to zoom in. Pinch to zoom on touch. Drag to pan when zoomed.
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">
</a>
Custom springs
Select a preset, then click the image to feel the difference.
Lightbox.init({
springOpen: { stiffness: 260, damping: 24 },
springClose: { stiffness: 300, damping: 28 },
});
Text links
Links without thumbnails fade in instead of morphing from a source element.
Programmatic open
Open images from code. Without a trigger element, the image fades in from center.
const lb = 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));
lb.on('closed', (e) => console.log('closed', e));
lb.on('navigate', (e) => console.log('navigate', e));
lb.on('zoomIn', (e) => console.log('zoomIn', e));
lb.on('zoomOut', (e) => console.log('zoomOut', e));
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 from a CDN. Lightbox3 will auto-initialize on all [data-lightbox] elements.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightbox3@1/dist/lightbox3.css">
<script src="https://cdn.jsdelivr.net/npm/lightbox3@1/dist/lightbox3.min.js"></script>
Prefer to self-host? Download lightbox3.min.js and lightbox3.css from the latest release and reference them the same way.
npm
Install the package, then import and initialize for more control.
npm install lightbox3
import { Lightbox } from 'lightbox3';
const lb = Lightbox.init();
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 data-lightbox |
Your existing data-lightbox and data-lightbox="gallery" attributes work as-is. |
data-title still works |
Lightbox3 reads data-title for captions, so your existing markup carries over. You can also use data-caption. |
| New features | Pinch-to-zoom, swipe navigation, swipe-to-dismiss, spring animations — all built in, no configuration needed. |
Data attributes
| Attribute | Description |
|---|---|
data-lightbox |
Add to an <a> element to enable lightbox. The href is used as the full-res image URL. |
data-lightbox="name" |
Group links into a navigable gallery. All links sharing the same value form a set. |
data-caption="text" |
Caption text displayed below the image. Also accepts data-title for Lightbox2 compatibility. |
data-alt="text" |
Alt text for the full-size image. Falls back to the thumbnail's alt attribute if not set. |
data-width="px" data-height="px" |
Full-res pixel dimensions. When set, the open animation targets the correct size immediately instead of guessing from the thumbnail — no reflow when the image loads. Both are required; otherwise the thumbnail's aspect ratio is used. |
Initialization
| Method | Description |
|---|---|
Lightbox.init(options?) |
Initialize and return a singleton instance. Scans the DOM for [data-lightbox] elements. Safe to call multiple times — returns the existing instance. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
selector |
string |
'[data-lightbox]' |
CSS selector for trigger elements. |
springOpen |
SpringConfig |
{ stiffness: 260, damping: 26 } |
Spring config for open and zoom-in animations. |
springClose |
SpringConfig |
{ stiffness: 500, damping: 38 } |
Spring config for close and zoom-out animations. |
padding |
number |
40 |
Viewport padding in pixels around the opened image. |
debug |
boolean |
false |
Show debug overlay with spring values and state. Also enabled via ?debug URL param. |
Methods
| Method | Description |
|---|---|
open(src, triggerEl?) |
Programmatically open an image. src is the full-res URL. Optional triggerEl enables the morph animation from that element; without it, the image fades in. |
close() |
Close the lightbox. |
next() |
Navigate to the next image in the gallery. |
prev() |
Navigate to the previous image in the gallery. |
destroy() |
Remove all event listeners, cancel animations, and tear down the instance. |
Events
| Event | Description |
|---|---|
open |
Fired when the open animation starts. |
opened |
Fired when the open animation completes. |
close |
Fired when the close animation starts. |
closed |
Fired when the close animation completes and the overlay is removed. |
navigate |
Fired when navigating to a different image in a gallery. |
zoomIn |
Fired when zoom-in starts (tap or double-click). |
zoomOut |
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 |
|---|---|---|
--lb-backdrop-color |
rgba(0, 0, 0, 0.95) |
Overlay backdrop color. |
--lb-image-border-radius |
24px |
Border radius of the opened image. |
--lb-image-padding |
40px |
Padding between the image and viewport edges. |
--lb-chrome-bg |
rgba(24, 24, 24, 0.8) |
Background color of the caption bar. |
--lb-chrome-text |
rgba(255, 255, 255, 0.9) |
Text color in the caption bar. |
--lb-chrome-font-size |
15px |
Font size in the caption bar. |
--lb-chrome-padding |
16px |
Padding around the caption bar and nav arrows. |
--lb-chrome-border-radius |
48px |
Border radius of the caption bar. |
--lb-z-index |
999999 |
Z-index of the lightbox overlay. |