The original lightbox script. Now with fluid animations and touch‑first gestures. A lightbox is a helper that displays enlarged, almost screen-filling versions of images.
5-10 Minutes to read
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.
Lightbox V3 is the successor of Lightbox V2 (by Lokesh Dhakar). The new version comes with fluid animations and touch‑first gestures built in.
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. Lightbox V3 will auto-initialize on all [data-lightbox] elements.
<link rel="stylesheet" href="Lightbox V3.css">
<script src="Lightbox V3.min.js"></script>Coming from Lightbox V2
Lightbox V3 uses the same markup pattern you already know — mostly the same HTML, way better UX.
| What changed | Details |
|---|---|
| Lightbox V3 is zero-dependency. Remove jQuery and the Lightbox V2 plugin script. |
| Your existing |
| Lightbox V3 reads |
| Pinch-to-zoom, swipe navigation, swipe-to-dismiss, spring animations — all built in, no configuration needed. |
API reference
The API (Application Programming Interface) is the set of commands the Lightbox V3 module offers to control it from your own code. For everyday use, you do not need the API at all: adding the data-lightbox attribute to a link is all that is required. The API becomes useful when a page should react to the lightbox — for example, to open an image from a button click, or to run some code after an image was closed. The following sections describe the available data attributes, options, methods, and events. All code examples are written in plain JavaScript and can be copied as a starting point for your own pages.
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
Before the lightbox can respond to clicks, the module has to be started once. This step is called initialization. In the J1 Template, the module is initialized automatically when the resource lightbox3 is enabled for a page, so no extra code is needed. Manual initialization is only required if custom options should be passed, or if the module is used outside of the J1 Template. Calling Lightbox.init() scans the page for all elements carrying the data-lightbox attribute and makes them clickable.
| Method | Description |
|---|---|
| Initialize and return a singleton instance. Scans the DOM for |
Options
Options change how the lightbox looks and behaves. They are passed as a JavaScript object to Lightbox.init() — for example, Lightbox.init({ padding: 20 }). Every option has a sensible default value, so passing options is entirely optional. The two spring settings control the physics of the animations: a higher stiffness makes the movement faster, while a higher damping reduces the bouncing at the end. The table below lists all supported options, their types, and their default values.
| Option | Type | Default | Description |
|---|---|---|---|
| string | data-lightbox | CSS selector for trigger elements. |
| object | { stiffness: 260, damping: 26 } | Spring config for open and zoom-in animations. |
| object | { stiffness: 500, damping: 38 } | Spring config for close and zoom-out animations. |
| number | 40 | Viewport padding in pixels around the opened image. |
| boolean | false | Show debug overlay with spring values and state. Also enabled via |
Methods
Methods are commands you can run on the lightbox instance returned by Lightbox.init(). They allow you to open, close, and navigate the lightbox from your own code instead of waiting for a user click. A typical use case is a custom button that opens a specific image, or a slideshow that steps through a gallery automatically. Store the instance in a variable first — for example, const lb = Lightbox.init() — and then call the methods on that variable. The table below describes all available 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
Events are notifications the lightbox sends out when something happens — for example, when an image is opened, closed, or zoomed. Your own code can listen to these events and react to them, such as pausing a video while the lightbox is open. Listening to an event does not change the behavior of the lightbox itself; it only informs your code. Each callback receives a detail object with useful information about the current image. The table below lists all events the lightbox fires.
Subscribe with lb.on(event, callback) and unsubscribe with lb.off(event, callback). Callbacks receive a { src, triggerEl, index, total } detail object.
| 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. |
CSS custom properties
Override these on :root or any parent element to customize the lightbox appearance.
| Property | Default | Description |
|---|---|---|
| rgba(0, 0, 0, 0.95) | Overlay backdrop color. |
| 24px | Border radius of the opened image. |
| 40px | Padding between the image and viewport edges. |
| rgba(24, 24, 24, 0.8) | Background color of the caption bar. |
| rgba(255, 255, 255, 0.9) | Text color in the caption bar. |
| 15px | Font size in the caption bar. |
| 16px | Padding around the caption bar and nav arrows. |
| 48px | Border radius of the caption bar. |
| 999999 | Z-index of the lightbox overlay. |
Examples
The following examples show the lightbox in action on real images. Each example demonstrates a common use case and includes the HTML markup needed to rebuild it on your own pages. All images are loaded from the Unsplash image service and can be replaced by your own image files. Click or tap any thumbnail to open the lightbox and try out the gestures. The examples work on desktop and mobile devices alike.
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 image thumbnails fade in instead of morphing from a source element.
| Because of the missing image tag ( |
HTML pattern
<a
href="https://images.unsplash.com/photo-1542224566-6e85f2e6772f?w=2400&q=80"
data-lightbox="">
Cherry blossoms in bloom
</a>Programmatic open
Open images from code. Without a trigger element, the image fades in from center. Open image via API.
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.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));