Get started with 33% off your first certification using code: 33OFFNEW

How to Build an Image Carousel in Gutenberg

3 min read
Published on 10th April 2024
How to Build an Image Carousel in Gutenberg

Gutenberg, the block editor for WordPress, has transformed how users create content, offering a more visual and intuitive approach to building posts and pages. While Gutenberg comes packed with a variety of blocks, creating an image carousel might not seem straightforward since there's no default carousel block. However, with a combination of the Gallery block, a little custom CSS, and optionally some JavaScript, you can craft a simple yet effective image carousel. This guide will walk you through creating an image carousel directly in Gutenberg without relying on plugins.

Step 1: Create a Gallery Block

  1. Add a New Block: In the Gutenberg editor, click on the "+" button to add a new block. Search for and select the "Gallery" block.

  2. Upload Images: Choose the images for your carousel by uploading them from your device or selecting existing ones from the media library.

  3. Configure the Gallery: After adding your images, you can configure the gallery settings in the right sidebar, such as the number of columns. For a carousel, you might start with a single column.

Step 2: Custom CSS for the Carousel

To transform the Gallery block into a carousel, you'll need to add some custom CSS. You can add this globally via your theme's "Additional CSS" section in the Customizer or locally by using a Custom HTML block.

  1. Basic Carousel CSS: Here's a simple CSS snippet to start with. This CSS hides all images except the first one and styles the navigation arrows:
.wp-block-gallery.columns-1 {
    position: relative;
    overflow: hidden;
}

.wp-block-gallery.columns-1 .blocks-gallery-item {
    display: none;
}

.wp-block-gallery.columns-1 .blocks-gallery-item:first-child {
    display: block;
}

.carousel-prev, .carousel-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background-color: #fff;
    border: none;
    cursor: pointer;
}

.carousel-prev { left: 10px; }
.carousel-next { right: 10px; }

Step 3: Adding Navigation Arrows

For navigation, you can use buttons or links. Here, we’ll use a Custom HTML block to add simple buttons for previous and next navigation. Place the Custom HTML block right after your Gallery block.

<button class="carousel-prev">Prev</button>
<button class="carousel-next">Next</button>

Step 4: Implementing Carousel Functionality with JavaScript

To make the carousel functional, you'll need to add some JavaScript to handle the click events on the navigation arrows. This can be added through a Custom HTML block or directly in your theme's files.

Here’s a basic example of JavaScript for carousel functionality. Note that this script assumes you have only one carousel per page. For multiple carousels, you'd need to adjust the code to target specific gallery instances.

<script>
document.addEventListener('DOMContentLoaded', function () {
    const prevButton = document.querySelector('.carousel-prev');
    const nextButton = document.querySelector('.carousel-next');
    const items = document.querySelectorAll('.wp-block-gallery.columns-1 .blocks-gallery-item');

    let currentIndex = 0;

    function showItem(index) {
        items.forEach((item, i) => {
            item.style.display = i === index ? 'block' : 'none';
        });
    }

    prevButton.addEventListener('click', () => {
        currentIndex = (currentIndex - 1 + items.length) % items.length;
        showItem(currentIndex);
    });

    nextButton.addEventListener('click', () => {
        currentIndex = (currentIndex + 1) % items.length;
        showItem(currentIndex);
    });
});
</script>

Step 5: Testing and Customization

After adding the CSS and JavaScript, preview your post or page to see the carousel in action. You can then adjust the CSS and JavaScript as needed to fit your site’s design and functionality requirements.

Creating an image carousel in Gutenberg is a fantastic way to showcase multiple images without overwhelming your visitors. By leveraging the Gallery block with some custom CSS and JavaScript, you can introduce dynamic, interactive elements to your WordPress site. Remember, the key to a successful carousel is not just in its visual appeal but also in its usability and responsiveness. Experiment with different styles and configurations to find the perfect carousel solution for your WordPress site.