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 foldbox
import 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 );

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/76979871', 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 attributeDescription
hrefThe item: image, video file, YouTube or Vimeo URL.
data-thumbThumbnail for the strip. Defaults to the link's image.
data-captionCaption, shown as plain text (see security).
data-typeimage, video, youtube or vimeo, when the URL doesn't show it.
data-posterPoster image for a video.
data-downloadDownload URL, or false to hide the button for this link.

Items

FieldDescription
srcImage URL, video file URL, YouTube or Vimeo URL. The type is detected from it.
typeOptional: image, video, youtube or vimeo.
sourcesVideo files as [{ src, type }], to offer several formats.
thumbThumbnail for the strip. Defaults to poster, and for YouTube to the video's thumbnail.
posterPoster image for videos.
startYouTube or Vimeo start time in seconds. Read from the link when it has one (?t=90, &t=1m30s, #t=90s).
altAlternative text for the image.
captionCaption. Inserted as HTML unless htmlCaptions is false.
srcsetResponsive image candidates, e.g. 'photo-1024.jpg 1024w, photo-2048.jpg 2048w'.
downloadDownload 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.

OptionDefaultDescription
index0Item to start with.
looptrueWrap from the last item to the first.
animation'slide''slide' or 'fade'.
speed400Transition time in ms.
originnullElement 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.
countertrueShow "1 / 5".
captionstrueShow captions.
htmlCaptionstrueInsert captions as HTML. false shows plain text. bind() defaults to false.
thumbnailstrueShow the thumbnail strip.
hideThumbsInFullscreenfalseHide the strip in fullscreen.
zoomtrueAllow zooming images.
maxZoom3Largest zoom factor.
fullscreentrueShow the fullscreen button.
slideshowtrueShow the slideshow button.
slideshowAutostartfalseStart the slideshow on open.
slideshowInterval5000Time per slide in ms.
downloadfalseShow 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.
keyboardtrueArrow, Home and End navigation. Escape always closes.
swipetrueSwipe between slides.
swipeToClosetrueSwipe down to close.
closeOnBackdroptrueClick outside the media to close.
youtubeNoCookietrueUse youtube-nocookie.com.
youtubeThumbnailstrueLoad YouTube thumbnails from i.ytimg.com for items without thumb or poster.
youtubeParams{ rel: 0 }Extra YouTube player parameters.
vimeoParams{}Extra Vimeo player parameters.
labelsEnglishTexts for translation.
onOpen, onChange, onClosenullCallbacks.

Methods

MethodDescription
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.defaultsDefault options, shared by every gallery.
Foldbox.versionThe version string, e.g. ''.

The lightbox object returned by Foldbox.open():

MethodDescription
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

InputAction
← →Previous / next item
Home EndFirst / last item
EscClose
TabMove between controls; focus stays inside the gallery
Double-click, pinch, Ctrl + wheelZoom; drag to pan
← → ↑ ↓ while zoomedMove around the image (zoom out to change slides again)
Swipe left / rightNext / previous item
Swipe downClose

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;
}
PropertyUsed for
--foldbox-bgBackdrop
--foldbox-colorText and icons
--foldbox-mutedCounter, captions, error text
--foldbox-accentFocus ring, slideshow progress, active thumbnail, spinner
--foldbox-btn-bg, --foldbox-btn-hoverToolbar and arrow buttons
--foldbox-thumb-bgThumbnail placeholder
--foldbox-spinner-trackLoading spinner track
--foldbox-thumb-sizeThumbnail size
--foldbox-topSpace above the gallery, e.g. for a fixed admin bar or header
--foldbox-zStacking 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 animation

Hidden items get the hidden attribute, so make sure your CSS hides them: .card[hidden] { display: none; }

OptionDefaultDescription
itemSelector':scope > *'Which elements are items.
duration400Animation 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

Accessibility

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

Foldbox is released under the MIT licence.