How to implement a carousel/slider using JavaScript

Carousels, also known as image sliders or slideshows, are popular web components used to display multiple images or content in a rotating manner on a webpage. A carousel typically consists of a series of images, cards, or content panels displayed in sequence. Users can navigate through the items using navigation buttons or arrow indicators. Carousels are widely used in web design to display multiple images or content in a visually appealing and interactive manner.

We will follow a few steps to build our carousel. Let's start coding a carousel from scratch.

Setting up the HTML structure

First, let’s set up the basic HTML structure for our carousel. Open your text editor or IDE and create a new HTML file. Inside the <body> tag, add a container <div> to hold the carousel content, and inside the container, add a series of <div> elements, each representing a slide or item in the carousel.

Example

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="carousel-container">
<div class="carousel-item">
<img src="image1.jpg" alt="Image 1">
</div>
<div class="carousel-item">
<img src="image2.jpg" alt="Image 2">
</div>
<div class="carousel-item">
<img src="image3.jpg" alt="Image 3">
</div>
<!-- Add more carousel items as needed -->
</div>
<script src="script.js"></script>
</body>
</html>

Styling the carousel

Next, let’s add some CSS to style our carousel. Create a new file named styles.css and link it in the <head> section of your HTML file using the <link> tag. Here’s an example of some basic styling to get you started:

.carousel-container {
width: 100%;
height: 300px; /* Adjust height as needed */
overflow: hidden;
position: relative;
}
.carousel-item {
width: 100%;
height: 100%;
display: none;
}
.carousel-item img {
width: 100%;
height: 100%;
object-fit: cover;
}

Implementing JavaScript functionality

Now, let’s add JavaScript code to implement the carousel functionality. Create a new file named script.js and link it at the bottom of your HTML file just before the closing </body> tag. Here’s an example of the JavaScript code:

const carouselItems = document.querySelectorAll('.carousel-item');
let currentIndex = 0;
function showSlide(index) {
// Hide all carousel items
carouselItems.forEach(item => {
item.style.display = 'none';
});
// Show the slide at the specified index
carouselItems[index].style.display = 'block';
}
function nextSlide() {
currentIndex = (currentIndex + 1) % carouselItems.length;
showSlide(currentIndex);
}
function previousSlide() {
currentIndex = (currentIndex - 1 + carouselItems.length) % carouselItems.length;
showSlide(currentIndex);
}
// Show the first slide initially
showSlide(currentIndex);
// Set up event listeners for next and previous buttons
document.getElementById('nextBtn').addEventListener('click', nextSlide);
document.getElementById('prevBtn').addEventListener('click', previousSlide);

Adding navigation buttons

To make the carousel interactive, we can add navigation buttons to move between slides. Inside the HTML file, below the carousel container <div>, add two buttons with unique IDs, one for Next and one for Previous. For example:

<div class="carousel-container">
<!-- Carousel items go here -->
</div>
<button id="prevBtn">Previous</button>
<button id="nextBtn">Next</button>

Testing the carousel

Save all the files and open your HTML file in a web browser. You should see the carousel with the first slide displayed. Clicking the Next and Previous buttons should navigate through the slides accordingly.

  • HTML
  • CSS
  • JavaScript

Conclusion

In conclusion, creating a carousel using HTML, CSS, and JavaScript is an effective way to display a series of images or content in a rotating manner on a webpage. Carousels provide a visually engaging and interactive way to showcase multiple items within a limited space. In this Answer, we learned how to implement a basic carousel/slider using JavaScript, HTML, and CSS. By following the steps outlined above, you can build a carousel that can display multiple slides or items.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved