If anyone else is having issues with this and getting Javascript errors, I’ve made a simple alternative solution you can use that doesn’t use the native lightbox functionality.
Here is the read-only link where you can grab it. Webflow - Modal lightbox for figure images in a rich text field
And here is the published site: https://modal-lightbox-for-figure-images-in-a-r.webflow.io/
All you have to do is include an embed anywhere on your page that has the HTML for the modal, some CSS in the page settings inside the head tag, and the script inside the page settings before the closing body tag.
Just look at the home page and it should be self explanatory. This will work on static pages as well as CMS pages as long as you are using the figure element in a rich text field.
Here is how to implement it:
Add an embed anywhere on the page with this (you will only see it in the designer, not the published site):
<!-- Modal HTML markup -->
<div id="modal">
<span id="close">×</span>
<img id="modal-img" src="" alt="Modal Image">
</div>
Open your page settings and add this CSS inside the tag:
<style>
/* Style for the modal */
#modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 9999;
justify-content: center;
align-items: center;
}
#modal img {
max-width: 90%;
max-height: 90%;
}
/* Style for the close button */
#close {
position: absolute;
top: 20px;
right: 20px;
color: #fff;
cursor: pointer;
}
/* Change cursor to pointer when hovering over initial figure image */
.w-richtext-align-center img {
cursor: pointer;
}
/* Change cursor to pointer for entire body when modal is open */
body.modal-open {
cursor: pointer;
}
</style>
Then add this script before the tag:
<script>
document.addEventListener("DOMContentLoaded", function() {
var modal = document.getElementById("modal");
var modalImg = document.getElementById("modal-img");
var closeBtn = document.getElementById("close");
// Function to open modal with clicked image
function openModal(imgSrc) {
modalImg.src = imgSrc;
modal.style.display = "flex";
document.body.classList.add('modal-open'); // Add modal-open class to body
}
// Function to close modal
function closeModal() {
modal.style.display = "none";
document.body.classList.remove('modal-open'); // Remove modal-open class from body
}
// Event listener to open modal when image is clicked
document.querySelectorAll('.w-richtext-align-center img').forEach(function(img) {
img.addEventListener('click', function() {
openModal(img.src);
});
});
// Event listener to close modal when clicked outside of image or on the modal image
modal.addEventListener('click', function(event) {
if (event.target === modal || event.target === closeBtn || event.target === modalImg) {
closeModal();
}
});
// Optional: Close modal when pressing Esc key
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
closeModal();
}
});
});
</script>
That’s it! No Webflow lightboxes or external lightbox scripts needed! Just don’t ask me how to make it fade in and out - you’re on your own on that one!