DEV Community

Sourav Pan
Sourav Pan

Posted on

Add Image Lightbox in Your Wordpress Theme Without a Plugin

An image lightbox is a user interface element commonly used to display images in a modal or popup window, providing an immersive viewing experience. It is a popular design pattern for presenting images in a more focused and visually appealing manner.

When implemented, a lightbox typically appears when a user clicks on a thumbnail image or a larger preview image. The lightbox overlay dims the background content and displays the selected image front and center, allowing the user to view it in detail without navigating away from the current page.

The lightbox often includes additional features, such as navigation arrows to switch between images in a gallery, a close button to exit the lightbox, and options for zooming, panning, or scrolling through the image if it exceeds the viewport size.

Image lightboxes are commonly used in various contexts, including photo galleries, portfolios, e-commerce product showcases, and article pages with inline images. They provide a visually pleasing and user-friendly way to showcase images while maintaining the context of the surrounding content.

*CSS:
*

`/* Lightbox container */

lightbox {

display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 9999;
overflow: auto;
}

/* Lightbox image */

lightbox img {

display: block;
max-width: 90%;
max-height: 90%;
margin: 5% auto;
}

/* Close button */

lightbox .close {

color: #fff;
position: absolute;
top: 10px;
right: 10px;
font-size: 24px;
font-weight: bold;
cursor: pointer;
}`

*JavaScript:
*

`// Get all the images
var images = document.querySelectorAll('.wp-block-image figure img');

// Create the lightbox container
var lightbox = document.createElement('div');
lightbox.id = 'lightbox';
document.body.appendChild(lightbox);

// Create the image element inside the lightbox container
var lightboxImage = document.createElement('img');
lightbox.appendChild(lightboxImage);

// Create the close button
var closeButton = document.createElement('span');
closeButton.className = 'close';
closeButton.innerHTML = '×';
lightbox.appendChild(closeButton);

// Attach event listeners to each image
images.forEach(function (image) {
image.addEventListener('click', function () {
// Show the lightbox with the clicked image
lightbox.style.display = 'block';
lightboxImage.src = this.src;
});
});

// Close the lightbox when the close button is clicked
closeButton.addEventListener('click', function () {
lightbox.style.display = 'none';
});
`

This code will create a lightbox container when any of the images with the class gb-block-image is clicked. The clicked image will be displayed in the lightbox, and there will be a close button to close the lightbox.

You can add this CSS code to the tag or in a separate CSS file, and the JavaScript code can be placed within a <script> tag or in an external JavaScript file. Make sure to include the JavaScript code after the HTML elements have been loaded on the page.</p>

Top comments (0)