The best free JavaScript image viewing libraries in 2026 are PhotoSwipe, GLightbox, Fancybox, lightGallery, Viewer.js and baguetteBox.js. Pick a modern, dependency-free library for new projects, and treat older jQuery plugins such as Magnific Popup, Colorbox and Swipebox as legacy options for existing sites.
This guide compares ten libraries by licence, dependencies and use case, shows a working lightbox example, and explains when the native <dialog> element is enough.
An image viewer (often called a lightbox) opens a thumbnail in a large overlay, usually with zoom, swipe, keyboard navigation and a close button. Good ones also handle focus, captions, responsive images and touch gestures. When you evaluate a library, check these points:
aria labels and reduced-motion handling.Always read the current licence on the project page before shipping, because terms can change between versions.
| Library | Dependencies | Best for | Notes |
|---|---|---|---|
| PhotoSwipe | None | Touch-friendly galleries with pinch zoom | Mature, widely used on mobile and desktop |
| GLightbox | None | Lightweight lightbox for images, video and inline content | Pure JavaScript, simple setup |
| Fancybox | None in recent versions | Rich media: images, video, iframes | Free for non-commercial use, commercial use needs a paid licence |
| lightGallery | None | Feature-rich galleries with plugins | Check licence terms for commercial projects |
| Viewer.js | None | Image zoom, rotate and flip in a viewer window | Good for admin and document-style tools |
| baguetteBox.js | None | Minimal lightbox with a tiny footprint | Pure JavaScript, easy to style |
| SimpleLightbox | None in recent versions | Simple galleries with captions | Older versions required jQuery |
| Lightbox2 | jQuery | Existing sites that already use jQuery | Classic and stable, not modern |
| Magnific Popup | jQuery | Legacy jQuery projects | Largely unmaintained |
| Colorbox | jQuery | Legacy jQuery projects | Older plugin, limited modern support |
Two items from older lists are not general-purpose choices. Swipebox is a jQuery plugin that has seen little maintenance, and FooBox is a WordPress plugin, not a standalone JavaScript library.
This example uses GLightbox from a CDN with no build step. Replace the image URLs with your own, and consider self-hosting the files for production.
Each thumbnail is a normal link to the large image, so the page still works without JavaScript. The data-gallery attribute groups images so users can move between them, and alt, width and height keep the page accessible and stable while loading.
For a single image preview you may not need a library. The HTML <dialog> element handles the overlay, focus management and the Escape key.
Choose a library when you need galleries, swipe gestures, pinch zoom or video. Choose <dialog> when you only need a simple preview.
srcset, so the viewer opens quickly.PhotoSwipe and GLightbox are strong general choices because they have no dependencies and good touch support. The best option depends on your licence needs and features.
No. Most modern libraries are written in plain JavaScript. Use jQuery plugins only to maintain older sites.
Recent versions are free for non-commercial use and need a commercial licence for business use. Check the licence on the official site.
Yes, for simple cases the native <dialog> element with a little JavaScript is enough.
For new projects choose a dependency-free library such as PhotoSwipe, GLightbox, Viewer.js or baguetteBox.js, and verify the licence before using Fancybox or lightGallery commercially. Keep older jQuery plugins only for legacy sites, and use the native <dialog> element for simple previews. Whatever you choose, keep images optimised and test keyboard and touch behaviour.