Implementing a Lightbox for ACF Images in Your WordPress Site

Easy

CSS, Javascript

Machine

Details

Divi Machine’s ACF Item module allows you to display ACF fields on the frontend of your WordPress Divi website. But how can you add a lightbox feature to an ACF image displayed through the ACF Item module? The solution involves CSS, JavaScript, and a few easy steps.

Thanks to Web Dev Simplified for this snippet.

Snippets Demo

Code

1

Description:
Navigate to the ACF Item module on your Single Post page (possibly in the Theme Builder). In the Advanced Tab, go to Custom CSS and add the following code. You can customize this CSS to suit your needs.

Language: CSS

Where to add: Divi Module Advanced Tab

                        /* Style for the lightbox container */
#lightbox {
  /* Fix the lightbox position to the viewport */
  position: fixed;
  /* Ensure the lightbox is above other elements */
  z-index: 1000;
  /* Set the top offset to 100px from the top */
  top: 100px;
  /* Make the lightbox take the full width of the viewport */
  width: 100%;
  /* Make the lightbox take the full height of the viewport */
  height: 100%;
  /* Set the background color to a semi-transparent black */
  background-color: rgba(0, 0, 0, .8);
  /* Hide the lightbox by default */
  display: none;
}

/* Style for the lightbox when it is active (visible) */
#lightbox.active {
  /* Display the lightbox as a flex container */
  display: flex;
  /* Center content horizontally */
  justify-content: center;
  /* Center content vertically */
  align-items: center;
}

/* Style for the image inside the lightbox */
#lightbox img {
  /* Set the image width to 60% of its container */
  width: 60%;
  /* Ensure the maximum width of the image is also 60% */
  max-width: 60%; 
}
                    
2

Description:
Add a Code Module to the page and insert the following JavaScript. Ensure that this Code Module is placed below the ACF Item module from Step One and within the same Divi row.

Language: Javascript

Where to add: Divi Code Module

                        // Create a new div element for the lightbox
const lightbox = document.createElement('div')
// Set the id of the new div to 'lightbox'
lightbox.id = 'lightbox'
// Append the lightbox div to the body of the document
document.body.appendChild(lightbox)

// Select all image elements on the page
const images = document.querySelectorAll('img')

// Loop through each image and add a click event listener
images.forEach(image => {
  image.addEventListener('click', e => {
    // Add the 'active' class to the lightbox when an image is clicked
    lightbox.classList.add('active')
    
    // Create a new image element to display in the lightbox
    const img = document.createElement('img')
    // Set the source of the new image to be the same as the clicked image
    img.src = image.src
    
    // Remove any existing children (images) from the lightbox
    while (lightbox.firstChild) {
      lightbox.removeChild(lightbox.firstChild)
    }
    
    // Append the new image to the lightbox
    lightbox.appendChild(img)
  })
})

// Add a click event listener to the lightbox
lightbox.addEventListener('click', e => {
  // If the target of the click is not the lightbox itself, return early
  if (e.target !== e.currentTarget) return
  
  // Remove the 'active' class from the lightbox to hide it
  lightbox.classList.remove('active')
})
                    

Related Snippets

Explore more from Divi Engine

Divi Form Builder

Divi Form Builder

From simple contact forms to complex frontend post or product creation, Divi Form Builder has you covered.

Divi Form Builder
Find out more
Divi Machine

Divi Machine

Build complex websites that displays dynamic fields you can filter, search and so much more with the Divi Builder.

Divi Machine
Find out more
Divi BodyCommerce

Divi BodyCommerce

A versatile toolkit for developers using Divi and WooCommerce together, designed to boost your e-commerce site and achieve greater conversion rates.

Divi BodyCommerce
Find out more
Divi Loop Extender

Divi Loop Extender

Unlock the Full Power of Divi 5 Loop Builder Add advanced sorting, filtering, and relationship logic right inside the Visual Builder.

Divi Loop Extender
Find out more
Divi Membership

Divi Membership

Monetize your Divi websites by transforming them into membership sites with seamless subscription management, user-friendly interfaces, and customizable membership tiers.

Divi Membership
Find out more
Divi Machine Accounts

Divi Machine Accounts

Build an account area for your customers to edit their details, access wishlist, submitted posts and more. *Note: Requires Divi Machine installed and active

Divi Machine Accounts
Find out more
Divi Ajax Filter

Divi Ajax Filter

Filter WooCommerce, Posts & Custom Posts without reloading the page.

Divi Ajax Filter
Find out more
Divi Mobile

Divi Mobile

Divi Mobile helps you create beautiful looking mobile menus without having to code.

Divi Mobile
Find out more
Divi Nitro

Divi Nitro

Give your Divi website that extra boost of speed with our Divi Nitro plugin to enhance your customer's experience.

Divi Nitro
Find out more
Divi Protect

Divi Protect

Password protect the content of your Divi website with our Divi Protect plugin. Keep unwanted eyes out!

Divi Protect
Find out more
Divi Mega Menu

Divi Mega Menu

Improve the user experience of your Divi website with our Divi Mega Menu plugin by creating dynamic menus using the Divi Builder.

Divi Mega Menu
Find out more