By the end of this lesson you’ll be able to upgrade the native Webflow lightbox with infinite looping navigation — so once someone opens your gallery, they can keep clicking next or previous forever, with no dead ends and no awkward stops. It’s a small fix to one of the more annoying limitations of the native lightbox, and it makes a gallery feel the way a gallery should: smooth and seamless.
By default, once you reach the first or last item in a lightbox group you’re stuck — you can only move back in the opposite direction. That’s a strange wall to hit in something meant to feel continuous. The good news is that the whole enhancement comes down to one custom attribute, and it works with CMS galleries, with static lightbox galleries, on desktop, and on mobile.
The part I care about most, though, is that it stays fully accessible. This isn’t only about a nicer experience for mouse users — it’s about making sure people navigating with a keyboard or assistive technology can move through the entire gallery without ever getting blocked.
How it works
The gallery structure. The starting point is a simple CMS collection — call it Pictures — with just an image field and a plain-text field for each image’s alt text, alongside the default name and slug. Inside the usual section/container/content-wrapper structure sits a collection list bound to that collection. Each collection item holds a native Webflow lightbox element, and inside the lightbox live the image (connected to the CMS image field, with its alt text wired to the alt-text field) and a heading. Binding the alt text to the CMS means every image announces itself properly — accessibility starts here, before any script.
Grouping is the glue. In the lightbox’s element settings, the media is connected to the CMS image field, and every lightbox is assigned the same group name (for example, gallery). Because they all share that name, Webflow ties them into a single lightbox group automatically — that’s what lets you page through the whole set from any starting image. The same idea applies to a static gallery: give each static lightbox the same group name and it behaves identically.
What the script actually does. All the code lives in the page’s Before </body> section. It enhances the native lightbox by adding seamless infinite looping in both directions, but its most important job is accessibility. The script actively monitors the navigation arrows and prevents them from ever becoming disabled — so no one, keyboard or mouse, ever reaches a dead end or loses their place inside the gallery. The experience stays inclusive, smooth, and predictable.
Only one thing to configure. The code is commented throughout if you want to read it, but you really only need to know one thing to make it work: the custom attribute data-lightbox-trigger. It’s the selector the script uses to find every lightbox it should enhance, so it goes directly on the lightbox element. And because the lightbox sits inside a collection item, that attribute is applied to every lightbox in the collection list at once — the setup scales without extra effort. (The card hover effect you may notice in the demo is a separate CSS technique covered in its own lesson; it’s not part of this enhancement.)
How to use it
- Clone the project from the Webflow link in the resources below, so you have the working script and structure to start from.
- Build (or reuse) your gallery. Put a native lightbox element inside each item — a CMS collection list for a dynamic gallery, or a row of static lightboxes for a fixed one. Connect the lightbox media to your image field (and bind the alt text to a CMS text field so each image is described).
- Give every lightbox the same group name (e.g.
gallery) so they navigate as one set. For a CMS gallery this is set once on the item’s lightbox; for a static gallery, set the same group on each element. - Add the
data-lightbox-triggerattribute to the lightbox element. Inside a collection item it’s applied to every generated card automatically; for static galleries, add it to each lightbox. - Keep the script in place in Before
</body>(it comes with the cloneable — no edits needed). - Publish and test. In preview or on the live site, open the gallery and confirm you can keep clicking next or previous forever. Then test the accessibility: use the left and right arrow keys, and press Tab to move focus to a navigation arrow and Enter to keep moving. Everything should loop smoothly, with the arrows never going disabled.