Documentation
Everything you need to add Foldbox to a page, configure it and make it your own. For a quick look, try the demos first.
Install
Foldbox is one CSS file and one JavaScript file with no dependencies. Load them from jsDelivr:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/wpgenius/foldbox@1/dist/foldbox.min.css">
<script src="https://cdn.jsdelivr.net/gh/wpgenius/foldbox@1/dist/foldbox.min.js"></script>@1 follows the latest 1.x release; pin an exact version such as @ in production. Or download foldbox.min.js and foldbox.min.css from the latest release and host them yourself. The readable sources are in src/.
Or install it from npm and import it in your bundle:
npm install foldboximport Foldbox from 'foldbox';
import 'foldbox/dist/foldbox.min.css';import gets the ES module build (dist/foldbox.mjs). It also has named exports: import { open, bind, grid } from 'foldbox'. Loaded with a <script> tag, Foldbox sets window.Foldbox; it is a UMD module, so CommonJS (require( 'foldbox' )) and AMD loaders work too.
TypeScript types are included (Foldbox.Options, Foldbox.Item, Foldbox.Lightbox and more), so editors autocomplete options and TypeScript checks your calls:
import Foldbox from 'foldbox';
const items: Foldbox.Item[] = [ { src: 'photo.jpg', caption: 'River' } ];
const options: Foldbox.Options = { theme: 'auto', loop: false };
Foldbox.open( items, options );Lightbox
Open from JavaScript
Pass a list of items and, optionally, options. origin is the element the gallery grows out of, usually the thumbnail that was clicked.
Foldbox.open(
[
{ src: 'photo-1.jpg', thumb: 'photo-1-thumb.jpg', alt: 'Beach', caption: '<h4>Beach</h4><p>Summer 2026</p>' },
{ src: 'clip.mp4', poster: 'clip.jpg', thumb: 'clip-thumb.jpg' },
{ src: 'https://www.youtube.com/watch?v=aqz-KE-bpKQ' },
{ src: 'https://vimeo.com/1084537', thumb: 'vimeo-thumb.jpg' },
],
{ index: 0, origin: document.querySelector( '#cover' ) }
);Bind links
Foldbox.bind() turns the links inside a container into a gallery. Each href is an item; the link's image becomes the thumbnail and the opening origin. Links added later work too, because one click listener is set on the container.
<div id="gallery">
<a href="large-1.jpg" data-caption="First"><img src="small-1.jpg" alt=""></a>
<a href="large-2.jpg" data-caption="Second"><img src="small-2.jpg" alt=""></a>
<a href="https://youtu.be/aqz-KE-bpKQ">Video</a>
</div>
<script>
const unbind = Foldbox.bind( '#gallery', { download: true } );
// Later: unbind();
</script>The first argument is a selector or an element. bind() returns a function that removes the listener; it reads options once, so call it again to change them.
| Link attribute | Description |
|---|---|
href | The item: image, video file, YouTube or Vimeo URL. |
data-thumb | Thumbnail for the strip. Defaults to the link's image. |
data-caption | Caption, shown as plain text (see security). |
data-type | image, video, youtube or vimeo, when the URL doesn't show it. |
data-poster | Poster image for a video. |
data-download | Download URL, or false to hide the button for this link. |
Items
| Field | Description |
|---|---|
src | Image URL, video file URL, YouTube or Vimeo URL. The type is detected from it. |
type | Optional: image, video, youtube or vimeo. |
sources | Video files as [{ src, type }], to offer several formats. |
thumb | Thumbnail for the strip. Defaults to poster, and for YouTube to the video's thumbnail. |
poster | Poster image for videos. |
start | YouTube or Vimeo start time in seconds. Read from the link when it has one (?t=90, &t=1m30s, #t=90s). |
alt | Alternative text for the image. |
caption | Caption. Inserted as HTML unless htmlCaptions is false. |
srcset | Responsive image candidates, e.g. 'photo-1024.jpg 1024w, photo-2048.jpg 2048w'. |
download | Download URL (e.g. the original file), or false to hide the button for this item. |
Videos
Video files play in the browser's own player. Give several formats with sources:
{
type: 'video',
sources: [
{ src: 'clip.webm', type: 'video/webm' },
{ src: 'clip.mp4', type: 'video/mp4' },
],
poster: 'clip.jpg',
}YouTube (youtube.com/watch?v=, youtu.be/, /shorts/, /embed/) and Vimeo links show a poster with a play button. The player is loaded when the visitor presses it, and stops when they move to another slide or close the gallery. Add player parameters with youtubeParams and vimeoParams.
Options
The defaults are in Foldbox.defaults; change them there to affect every gallery on the page.
| Option | Default | Description |
|---|---|---|
index | 0 | Item to start with. |
loop | true | Wrap from the last item to the first. |
animation | 'slide' | 'slide' or 'fade'. |
speed | 400 | Transition time in ms. |
origin | null | Element the gallery grows out of. |
theme | 'dark' | 'dark', 'light', or 'auto' to follow the visitor's system setting. |
className | '' | Extra class on the lightbox, e.g. for your own theme. |
counter | true | Show "1 / 5". |
captions | true | Show captions. |
htmlCaptions | true | Insert captions as HTML. false shows plain text. bind() defaults to false. |
thumbnails | true | Show the thumbnail strip. |
hideThumbsInFullscreen | false | Hide the strip in fullscreen. |
zoom | true | Allow zooming images. |
maxZoom | 3 | Largest zoom factor. |
fullscreen | true | Show the fullscreen button. |
slideshow | true | Show the slideshow button. |
slideshowAutostart | false | Start the slideshow on open. |
slideshowInterval | 5000 | Time per slide in ms. |
download | false | Show the download button. Only http(s) and blob: URLs are used. Browsers download only files from the page's own domain; files on another domain open in a new tab. |
keyboard | true | Arrow, Home and End navigation. Escape always closes. |
swipe | true | Swipe between slides. |
swipeToClose | true | Swipe down to close. |
closeOnBackdrop | true | Click outside the media to close. |
youtubeNoCookie | true | Use youtube-nocookie.com. |
youtubeThumbnails | true | Load YouTube thumbnails from i.ytimg.com for items without thumb or poster. |
youtubeParams | { rel: 0 } | Extra YouTube player parameters. |
vimeoParams | {} | Extra Vimeo player parameters. |
labels | English | Texts for translation. |
onOpen, onChange, onClose | null | Callbacks. |
Methods
| Method | Description |
|---|---|
Foldbox.open( items, options ) | Open a gallery. Returns the lightbox object. |
Foldbox.bind( container, options ) | Open links inside the container in a gallery. Returns an unbind function. |
Foldbox.grid( element, options ) | Make a filterable grid from an element or a CSS selector. |
Foldbox.close() | Close whichever gallery is open. |
Foldbox.defaults | Default options, shared by every gallery. |
Foldbox.version | The version string, e.g. ''. |
The lightbox object returned by Foldbox.open():
| Method | Description |
|---|---|
next(), prev() | Go to the next or previous item. |
goTo( index ) | Go to an item by its position (from 0). |
play(), pause() | Start or stop the slideshow. |
toggleZoom() | Zoom the current image in or out. |
toggleFullscreen() | Enter or leave fullscreen. |
close() | Close the gallery. |
Only one gallery is open at a time; opening another closes the first.
Callbacks
onOpen, onChange and onClose receive the current index, the current item and the lightbox object.
Foldbox.open( items, {
onChange( index, item, lightbox ) {
history.replaceState( null, '', '#photo-' + ( index + 1 ) );
},
onClose() {
history.replaceState( null, '', location.pathname );
},
} );Keyboard and touch
| Input | Action |
|---|---|
| ← → | Previous / next item |
| Home End | First / last item |
| Esc | Close |
| Tab | Move between controls; focus stays inside the gallery |
| Double-click, pinch, Ctrl + wheel | Zoom; drag to pan |
| ← → ↑ ↓ while zoomed | Move around the image (zoom out to change slides again) |
| Swipe left / right | Next / previous item |
| Swipe down | Close |
When a video has focus, the arrow keys, Home and End control the video instead. With Alt, Ctrl or Cmd held, they are left to the browser (for example Alt + ← to go back).
Themes and styling
The gallery is dark by default. Choose a built-in theme with the theme option:
Foldbox.open( items, { theme: 'light' } ); // 'dark' (default), 'light' or 'auto'
Foldbox.bind( '#gallery', { theme: 'auto' } ); // follows the visitor's light/dark setting'auto' follows the visitor's system setting (prefers-color-scheme, which browsers such as Chrome take from the operating system or their own appearance setting). If your site has its own light/dark switch, pass the theme your page shows instead:
// Example: the site stores its theme in <html data-theme="dark">.
const dark = document.documentElement.getAttribute( 'data-theme' ) === 'dark';
Foldbox.open( items, { theme: dark ? 'dark' : 'light' } );CSS properties
Change colours and sizes with CSS custom properties on .foldbox:
.foldbox {
--foldbox-bg: rgba(0, 0, 0, 0.9);
--foldbox-accent: #ffcc00;
--foldbox-thumb-size: 72px;
--foldbox-top: 32px; /* leave room for a fixed header */
--foldbox-z: 99999;
}| Property | Used for |
|---|---|
--foldbox-bg | Backdrop |
--foldbox-color | Text and icons |
--foldbox-muted | Counter, captions, error text |
--foldbox-accent | Focus ring, slideshow progress, active thumbnail, spinner |
--foldbox-btn-bg, --foldbox-btn-hover | Toolbar and arrow buttons |
--foldbox-thumb-bg | Thumbnail placeholder |
--foldbox-spinner-track | Loading spinner track |
--foldbox-thumb-size | Thumbnail size |
--foldbox-top | Space above the gallery, e.g. for a fixed admin bar or header |
--foldbox-z | Stacking order (z-index) |
Your own theme
Put the properties on a class and pass it with className:
.foldbox.my-sepia {
--foldbox-bg: rgba(40, 30, 20, 0.96);
--foldbox-color: #f4e9d8;
--foldbox-muted: rgba(244, 233, 216, 0.7);
--foldbox-accent: #e0b872;
}Foldbox.open( items, { className: 'my-sepia' } );Translation
Every text in the gallery comes from labels. Pass only the ones you want to change; %1 and %2 in counter are the current position and the total.
Foldbox.open( items, {
labels: {
dialog: 'Galerie',
close: 'Fermer',
prev: 'Précédent',
next: 'Suivant',
zoomIn: 'Zoom avant',
zoomOut: 'Zoom arrière',
fullscreen: 'Plein écran',
exitFullscreen: 'Quitter le plein écran',
play: 'Lancer le diaporama',
pause: 'Mettre en pause',
download: 'Télécharger',
playVideo: 'Lire la vidéo',
counter: '%1 sur %2',
error: 'Impossible de charger cet élément.',
},
} );To translate every gallery on the page, set the labels once in Foldbox.defaults.labels. Labels you leave out stay in English:
Foldbox.defaults.labels = { close: 'Fermer', prev: 'Précédent', next: 'Suivant', counter: '%1 sur %2' };The gallery works on right-to-left pages too.
Filterable grid
Foldbox.grid() animates a grid between filters: items that stay glide to their new place, hidden items fade and shrink, new ones grow in. It works with any layout: CSS grid, flex or floats.
const grid = Foldbox.grid( '.grid', { itemSelector: ':scope > .card' } );
grid.filter( '.weddings' ); // CSS selector
grid.filter( '*' ); // everything
grid.filter( ( item ) => item.dataset.year === '2026' ); // function
grid.filter( '.portraits', true ); // no animationHidden items get the hidden attribute, so make sure your CSS hides them: .card[hidden] { display: none; }
| Option | Default | Description |
|---|---|---|
itemSelector | ':scope > *' | Which elements are items. |
duration | 400 | Animation time in ms. |
easing | 'ease' | CSS easing. |
A typical setup with filter buttons:
const grid = Foldbox.grid( document.querySelector( '.grid' ) );
document.querySelectorAll( '[data-filter]' ).forEach( ( button ) => {
button.addEventListener( 'click', () => grid.filter( button.dataset.filter ) );
} );Captions and security
With Foldbox.open(), captions are inserted as HTML so you can add headings and links. Escape anything that comes from users, or set htmlCaptions: false to show captions as plain text.
Foldbox.bind() reads captions from page markup that other people (such as post authors) may control, so it shows them as plain text. Pass htmlCaptions: true only if you trust everyone who can edit that markup. Download links accept only http(s) and blob: URLs.
Privacy
- YouTube and Vimeo players load only when the visitor presses play. Until then, no request goes to YouTube or Vimeo, except for thumbnails below.
- YouTube uses
youtube-nocookie.com(turn off withyoutubeNoCookie: false). - YouTube items without your own
thumborposterload their thumbnail fromi.ytimg.comwhen the gallery opens. SetyoutubeThumbnails: false, or give your own images, to avoid that.
Accessibility
- The gallery is a modal dialog (
role="dialog",aria-modal) with a label. - Focus moves into the gallery, stays there while it is open, and returns to the element that opened it.
- All controls are buttons with labels; slides that are not visible are hidden from screen readers.
- Keyboard navigation works without a mouse.
- Animations are turned off for visitors who ask for reduced motion.
Browser support
Current versions of Chrome, Edge, Firefox and Safari, on desktop and mobile.
Contributing
Bug reports, ideas and pull requests are welcome on GitHub.
git clone https://github.com/wpgenius/foldbox.git
cd foldbox
npm install
npm run build- Open
demo/index.htmlthrough a local web server to try your change. - In a pull request, change
src/(and docs) only. Don't commitdist/orCHANGELOG.md, and don't change version numbers: the release workflow does that when the PR is merged. - Link the PR to its issue (
Fixes #12); the changelog lists the issues closed in each release. - Releases are patch versions by default. Label the PR
release: minororrelease: majorfor a bigger bump.
Foldbox is released under the MIT licence.