Back to Tutorials
Web Development
25 min read
Sahasransu Satpathy
2/28/2026

How to Create Carousels / Sliders

Step-by-step guide to building responsive image carousels and content sliders using HTML, CSS, and JavaScript

Introduction

Carousels and sliders enhance website interactivity by displaying multiple items in a compact, rotating layout. This tutorial shows how to build responsive carousels using HTML, CSS, and JavaScript.


Step 1: HTML Structure

Create the basic structure for the slider:

<div class="carousel">
  <div class="slides">
    <div class="slide"><img src="image1.jpg" alt="Image 1"></div>
    <div class="slide"><img src="image2.jpg" alt="Image 2"></div>
    <div class="slide"><img src="image3.jpg" alt="Image 3"></div>
  </div>
  <button class="prev">&#10094;</button>
  <button class="next">&#10095;</button>
</div>

Step 2: CSS Styling

Style the carousel container and slides:

.carousel {
  position: relative;
  max-width: 600px;
  margin: auto;
  overflow: hidden;
  border: 2px solid #ccc;
  border-radius: 10px;
}

.slides {
  display: flex;
  transition: transform 0.5s ease-in-out;
}

.slide {
  min-width: 100%;
}

.slide img {
  width: 100%;
  display: block;
}

.prev, .next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0,0,0,0.5);
  color: white;
  padding: 10px;
  border: none;
  cursor: pointer;
  border-radius: 50%;
}

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

Step 3: JavaScript Functionality

Make the carousel interactive:

const slides = document.querySelector('.slides');
const slide = document.querySelectorAll('.slide');
const prev = document.querySelector('.prev');
const next = document.querySelector('.next');

let index = 0;

function showSlide(i) {
  if(i < 0) index = slide.length - 1;
  else if(i >= slide.length) index = 0;
  else index = i;
  
  slides.style.transform = `translateX(-${index * 100}%)`;
}

prev.addEventListener('click', () => showSlide(index - 1));
next.addEventListener('click', () => showSlide(index + 1));

// Optional: Auto-play
setInterval(() => showSlide(index + 1), 5000);

Step 4: Responsive Adjustments

Ensure the carousel works on all screen sizes:

@media (max-width: 768px) {
  .prev, .next {
    padding: 8px;
  }
}

Step 5: Mini Project Example

  • Add multiple slides with text and images.
  • Include indicators or thumbnails for navigation.
  • Customize auto-play speed and animation effects.

Conclusion

You now know how to build responsive carousels/sliders with HTML, CSS, and JavaScript. Combine these techniques to showcase images, products, or content dynamically on your website.


SEO Suggestions:

  • Main keywords: carousel tutorial, slider tutorial, responsive slider, JavaScript carousel, HTML CSS JS carousel
  • Meta description: Learn how to create responsive image carousels and content sliders with HTML, CSS, and JavaScript. Step-by-step guide for web developers.
  • Catchy title suggestions: "Build Responsive Carousels & Sliders – Step-by-Step Guide", "Create Interactive Image Sliders with HTML, CSS & JS"

Previous Tutorial

Browse All Tutorials