GLightbox
GLightbox is a lightweight, pure JavaScript lightbox library designed for displaying various media types including images, videos (YouTube, Vimeo, self-hosted), iframes, and inline content. It is currently stable at version `3.3.1`, with an active beta release stream (`4.0.0-beta.x`) indicating ongoing development towards a new major version. The library is known for its small footprint (11KB gzipped), responsiveness across devices, and comprehensive feature set including gallery support, responsive images, keyboard and touch navigation, zoomable images, and a themeable API. It differentiates itself by being a feature-rich solution without external dependencies, offering a complete lightbox experience out-of-the-box, making it a robust choice for modern web projects requiring media presentation. While minor bug fixes and improvements are released frequently for the v3 branch, the v4 branch represents a significant refactor and enhancement effort.
Common errors
-
Uncaught ReferenceError: GLightbox is not defined
cause The GLightbox JavaScript file has not been loaded or is loaded incorrectly, or you are trying to use it before it's available.fixEnsure `glightbox.min.js` is linked via a `<script>` tag in your HTML (preferably at the end of `<body>`), or properly imported via ESM/CommonJS if using a module bundler. If using a script tag, make sure your initialization script runs after `glightbox.min.js` has loaded. -
Lightbox opens but content (image/video) is blank or shows a loading spinner indefinitely.
cause The linked media URL is incorrect, inaccessible (e.g., CORS issues for iframes/local files), or GLightbox cannot determine its content type.fixDouble-check the `href` attribute of your `<a>` tag. For external content, ensure it's publicly accessible. If linking to URLs without clear file extensions (e.g., a dynamic image URL or a generic API endpoint), explicitly set the `data-type` attribute (e.g., `data-type="image"` or `data-type="video"`) on the `<a>` tag.
Warnings
- breaking GLightbox `v4.x` is currently in beta and introduces significant changes and refactors. Upgrading from `v3.x` will likely require modifications to your implementation due to API changes and a shift towards a plugin-based architecture.
- gotcha The GLightbox CSS stylesheet must be explicitly included in your project. The JavaScript bundle does not automatically inject the styles, leading to an unstyled or non-functional lightbox if omitted.
- gotcha When initializing GLightbox, ensure the `selector` option accurately targets the elements you intend to trigger the lightbox. Mismatched selectors are a common cause of the lightbox not opening.
Install
-
npm install glightbox -
yarn add glightbox -
pnpm add glightbox
Imports
- GLightbox
import { GLightbox } from 'glightbox';import GLightbox from 'glightbox';
- GLightbox
import GLightbox from 'glightbox'; // after script tag load
<script src="https://cdn.jsdelivr.net/npm/glightbox/dist/js/glightbox.min.js"></script> <script>const lightbox = GLightbox();</script>
- CSS
/* No CSS import */
import 'glightbox/dist/css/glightbox.min.css';
Quickstart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GLightbox Quickstart</title>
<!-- Essential: Link the GLightbox CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css" />
</head>
<body>
<h1>GLightbox Gallery Example</h1>
<div class="gallery">
<!-- Simple image with title and description -->
<a href="https://picsum.photos/id/1015/1200/800" class="glightbox" data-gallery="mygallery" data-glightbox="title: Forest Path; description: A peaceful path through the woods.">
<img src="https://picsum.photos/id/1015/300/200" alt="Forest Path" style="width:150px; height:auto;">
</a>
<!-- Another image in the same gallery -->
<a href="https://picsum.photos/id/1018/1200/800" class="glightbox" data-gallery="mygallery" data-glightbox="title: Mountain Lake; description: Serene lake in the mountains.">
<img src="https://picsum.photos/id/1018/300/200" alt="Mountain Lake" style="width:150px; height:auto;">
</a>
<!-- YouTube video example -->
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" class="glightbox" data-gallery="mygallery" data-glightbox="title: Rick Astley; description: Never Gonna Give You Up - official music video.">
<img src="https://img.youtube.com/vi/dQw4w9WgXcQ/mqdefault.jpg" alt="Rick Roll Thumbnail" style="width:150px; height:auto;">
</a>
</div>
<!-- Essential: Link the GLightbox JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/glightbox/dist/js/glightbox.min.js"></script>
<script type="text/javascript">
// Initialize GLightbox. The selector targets all elements with the 'glightbox' class.
const lightbox = GLightbox({
selector: '.glightbox',
loop: true, // Allow looping through the gallery
autoplayVideos: true // Automatically play videos when opened
});
// You can access the GLightbox instance for programmatic control if needed.
// console.log('GLightbox initialized:', lightbox);
</script>
</body>
</html>