Gallery & lightbox
A grid of thumbnails, each a link to the big picture. Click one and it opens large in a lightbox, with arrows, a counter and a caption; arrow keys and swipes move through them. You can zoom in, pull the picture down to close it on a phone, show a strip of thumbnails and mix in videos. Without JavaScript the links just open the pictures.
Gallery
<div class="gallery">
<a class="gallery-item" href="../examples/img/sample-1.svg"><img src="../examples/img/sample-1.svg" alt="Yellow hills at noon" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-2.svg"><img src="../examples/img/sample-2.svg" alt="Violet dusk over the hills" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-3.svg"><img src="../examples/img/sample-3.svg" alt="A green valley" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-4.svg"><img src="../examples/img/sample-4.svg" alt="Pink sunrise over the sea" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-5.svg"><img src="../examples/img/sample-5.svg" alt="A blue lake between mountains" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-6.svg"><img src="../examples/img/sample-6.svg" alt="An orange desert" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-7.svg"><img src="../examples/img/sample-7.svg" alt="Night hills under the moon" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-8.svg"><img src="../examples/img/sample-8.svg" alt="A small town at dusk" loading="lazy"></a>
</div>Each link points at the large picture; the <img> inside can be a smaller copy. The caption in the lightbox comes from data-caption on the link, else a .gallery-caption inside it, else the picture's alt.
A big first picture, with captions
.gallery-feature makes the first one twice as big; --gallery-ratio changes the shape (square by default).
<div class="gallery gallery-feature" style="--gallery-ratio: 4 / 3">
<a class="gallery-item" href="../examples/img/sample-8.svg" data-caption="The town square, an hour before the lights came on."><img src="../examples/img/sample-8.svg" alt="A small town at dusk"><span class="gallery-caption">The town</span></a>
<a class="gallery-item" href="../examples/img/sample-5.svg"><img src="../examples/img/sample-5.svg" alt="A blue lake between mountains"><span class="gallery-caption">The lake</span></a>
<a class="gallery-item" href="../examples/img/sample-6.svg"><img src="../examples/img/sample-6.svg" alt="An orange desert"><span class="gallery-caption">The desert</span></a>
<a class="gallery-item" href="../examples/img/sample-2.svg"><img src="../examples/img/sample-2.svg" alt="Violet dusk over the hills"><span class="gallery-caption">Dusk</span></a>
<a class="gallery-item" href="../examples/img/sample-7.svg"><img src="../examples/img/sample-7.svg" alt="Night hills under the moon"><span class="gallery-caption">Night</span></a>
</div>Columns
.gallery-columns keeps each picture's own shape and flows them into columns, for photos of mixed sizes.
<div class="gallery gallery-columns">
<a class="gallery-item" href="../examples/img/sample-3.svg"><img src="../examples/img/sample-3.svg" alt="A green valley" width="1200" height="800"></a>
<a class="gallery-item" href="../examples/img/sample-clip-poster.png"><img src="../examples/img/sample-clip-poster.png" alt="Sun rising over yellow hills" width="960" height="540"></a>
<a class="gallery-item" href="../examples/img/sample-4.svg"><img src="../examples/img/sample-4.svg" alt="Pink sunrise over the sea" width="1200" height="800"></a>
<a class="gallery-item" href="../examples/img/sample-1.svg"><img src="../examples/img/sample-1.svg" alt="Yellow hills at noon" width="1200" height="800"></a>
<a class="gallery-item" href="../examples/img/sample-7.svg"><img src="../examples/img/sample-7.svg" alt="Night hills under the moon" width="1200" height="800"></a>
</div>Zoom
Every lightbox zooms, with nothing to set up. Open any picture on this page and try it:
- Mouse: double click the picture, turn the wheel, or use the two zoom buttons at the top. Drag to look around.
- Touch: pinch with two fingers or double tap; drag with one finger to look around.
- Keyboard: + zooms in, - zooms out, 0 goes back to the whole picture.
The picture goes up to four times its size and never slides off the screen. Moving to another picture starts it whole again.
Pull down to close
On a touch screen, drag the picture down: it follows your finger, the dark fades, and letting go far enough closes the lightbox. Let go early and it springs back. A sideways swipe still moves to the next picture.
Thumbnails
Add data-lightbox-thumbs to the gallery and the lightbox shows a strip of small pictures along the bottom. The one on screen is marked, and a click jumps straight to any other.
<div class="gallery" id="g-thumbs" data-lightbox-thumbs style="--gallery-ratio: 4 / 3">
<a class="gallery-item" href="../examples/img/sample-1.svg" data-caption="Day 1: the hills above the village, just after noon."><img src="../examples/img/sample-1.svg" alt="Yellow hills at noon" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-3.svg" data-caption="Day 2: down into the valley."><img src="../examples/img/sample-3.svg" alt="A green valley" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-5.svg" data-caption="Day 3: the lake, cold enough to count as brave."><img src="../examples/img/sample-5.svg" alt="A blue lake between mountains" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-6.svg" data-caption="Day 4: the desert road."><img src="../examples/img/sample-6.svg" alt="An orange desert" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-4.svg" data-caption="Day 5: sunrise from the ferry."><img src="../examples/img/sample-4.svg" alt="Pink sunrise over the sea" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-7.svg" data-caption="Day 6: the last night out."><img src="../examples/img/sample-7.svg" alt="Night hills under the moon" loading="lazy"></a>
</div><div class="gallery" data-lightbox-thumbs>
<a class="gallery-item" href="big-1.jpg"><img src="small-1.jpg" alt="Yellow hills at noon"></a>
<a class="gallery-item" href="big-2.jpg"><img src="small-2.jpg" alt="A green valley"></a>
</div>Videos
A link to a video file (.mp4, .webm) plays in the lightbox, with the thumbnail as its poster. The thumbnail gets a play mark by itself. For YouTube or Vimeo, keep the link pointing at the video's page and put the embed address in data-video.
<div class="gallery" id="g-video" data-lightbox-thumbs style="--gallery-ratio: 16 / 9">
<a class="gallery-item" href="../examples/img/sample-clip.webm" data-caption="Sunrise over the hills, 6 seconds."><img src="../examples/img/sample-clip-poster.png" alt="Sun rising over yellow hills" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-2.svg"><img src="../examples/img/sample-2.svg" alt="Violet dusk over the hills" loading="lazy"></a>
<a class="gallery-item" href="../examples/img/sample-8.svg"><img src="../examples/img/sample-8.svg" alt="A small town at dusk" loading="lazy"></a>
</div><!-- a video file -->
<a class="gallery-item" href="clip.mp4"><img src="poster.jpg" alt="Sun rising over yellow hills"></a>
<!-- YouTube: the link still works without JavaScript -->
<a class="gallery-item" href="https://www.youtube.com/watch?v=VIDEO_ID"
data-video="https://www.youtube-nocookie.com/embed/VIDEO_ID"><img src="poster.jpg" alt="How Momo works"></a>From a script
Momo.lightbox.open('#photos', 2); // open a group at its third picture
Momo.lightbox.zoom(2); // 1 = the whole picture, up to 4
Momo.lightbox.close();Classes and options
| Class / attribute | Does |
|---|---|
.gallery | The grid: as many as fit, each at least 176px (--gallery-min), --gallery-gap 12px |
a.gallery-item | A thumbnail link (square by default: --gallery-ratio); zooms a touch on hover |
.gallery-caption | A small caption over the bottom of a thumbnail |
.gallery-feature / .gallery-columns | Big first picture / natural shapes in columns |
data-caption | The caption shown in the lightbox |
data-lightbox-thumbs | On the group: a strip of thumbnails in the lightbox |
data-video | On a link: the embed address to play (a link to an .mp4 or .webm needs nothing) |
data-momo-lightbox | Turns any group of picture links into a lightbox set (not only .gallery); ="false" turns it off |
Accessibility
- Give every picture a real
alt: it is read on the thumbnail and in the lightbox. - The lightbox is a dialog: focus moves into it, Esc closes it and focus goes back to the thumbnail. ← → move, Home and End jump, and the counter ("3 / 8") is read out.
- Zoom works without a mouse or a pinch: two labelled buttons, and + - 0.
- The thumbnails are buttons with names ("Picture 3: A blue lake between mountains"); the one on screen has
aria-current. - Closing the lightbox stops a video that is playing.
- Ctrl or ⌘ + click still opens a picture in a new tab.
