How to Add Image Carousel in Nextjs?

Spread the love


An image carousel is a popular feature on modern websites, allowing users to browse through a collection of images interactively. Adding an image carousel in NextJS can significantly enhance the user experience by providing a dynamic and engaging way to display images. Carousels are commonly used in various sections of a website, such as galleries, portfolios, testimonials, or product showcases. They enhance user engagement by providing a visually appealing way to display multiple images without overwhelming the viewer.

In this tutorial, we will demonstrate how to create an image carousel in Next.js using the “react-responsive-carousel” library. Next.js is a powerful React framework that enables server-side rendering and static site generation, making it an excellent choice for building high-performance web applications.

By the end of this guide, you will have a fully functional image carousel in your Next.js application. We will cover everything from setting up a new Next.js project to styling the carousel for a polished, professional look. Whether you are a beginner or an experienced developer, this step-by-step tutorial will provide you with the knowledge and tools needed to implement an image carousel in NextJS efficiently.

Benefits of Using an Image Carousel in NextJS

  • Improved User Experience: Carousels allow users to easily browse through multiple images without having to navigate away from the current page.
  • Space Efficiency: They help in saving space on the webpage by displaying multiple images in a single section.
  • Interactive Elements: Adding interactivity with animations and transitions makes the webpage more engaging.
  • Showcase Multiple Items: Perfect for e-commerce sites to display products, or for portfolio websites to showcase work samples.

Setting Up Next.js Project

Ensure you have Node.js installed on your computer. Then, set up a new Next.js project. If you already have a Next.js project, you can skip this step.

Step 1: Create a New Next.js Project

Open your terminal and run the following command to create a new Next.js project:

npx create-next-app image-carousel
cd image-carousel

This will create a new directory called image-carousel with a basic Next.js setup.

Step 2: Start the Development Server

Navigate to the project directory and start the development server:

npm run dev

Your Next.js application should now be running at http://localhost:3000.

Installing Required Packages

To add an image carousel in NextJS, we need to install the react-responsive-carousel package.

Step 3: Install react-responsive-carousel

Run the following command to install react-responsive-carousel:

npm install react-responsive-carousel

Creating the Image Carousel Component

Now that we have the necessary package installed, let’s create the component of image carousel in NextJS.

Also, Read: Next.js Image Optimization: A Step-by-Step Guide

Step 4: Create Carousel Component

Create a new file called Carousel.js in the components directory:

mkdir components
touch components/Carousel.js

Step 5: Add Carousel Code

Open Carousel.js and add the following code:

import React from 'react';
import { Carousel } from 'react-responsive-carousel';
import 'react-responsive-carousel/lib/styles/carousel.min.css';

const ImageCarousel = () => {
  return (
        <img src="/images/image1.jpg" alt="Image 1" />
        <p className="legend">Image 1</p>
        <img src="/images/image2.jpg" alt="Image 2" />
        <p className="legend">Image 2</p>
        <img src="/images/image3.jpg" alt="Image 3" />
        <p className="legend">Image 3</p>

export default ImageCarousel;

In this code, we import the Carousel component from react-responsive-carousel and define an ImageCarousel component that renders three images.

Adding the Carousel to Your Page

Next, we’ll add the ImageCarousel component to a page in our Next.js application.

Step 6: Import and Use Carousel Component

Open pages/index.js and modify it as follows:

import Head from 'next/head';
import ImageCarousel from '../components/Carousel';

export default function Home() {
  return (
        <title>Image Carousel in Next.js</title>
        <meta name="description" content="Learn how to add an image carousel in Next.js." />
        <h1>Image Carousel in Next.js</h1>
        <ImageCarousel />

In this code, we import the ImageCarousel component and include it in the main section of our page.

Styling the Carousel

To ensure the image carousel looks good, we can add some custom styles.

Step 7: Add Custom Styles

Create a new CSS file called carousel.css in the styles directory:

touch styles/carousel.css

Step 8: Add CSS Code

Open carousel.css and add the following styles:

Also, Read: How to Import External CSS Files in Next.js

.carousel .slide img {
  width: 100%;
  height: auto;

Step 9: Import CSS in Carousel Component

Open components/Carousel.js and import the CSS file:

import 'react-responsive-carousel/lib/styles/carousel.min.css';
import '../styles/carousel.css';

Now, your image carousel should be styled properly.


Adding an image carousel in NextJS is straightforward with the help of the react-responsive-carousel library. By following the steps outlined in this article, you can easily implement an image carousel in your NextJS application. This feature not only enhances the visual appeal of your website but also improves user interaction.

Leave a Comment