Bootstrap Carousel
Learn to create interactive slideshows with navigation controls, indicators and automatic sliding using Bootstrap 5.
Introduction
A Bootstrap Carousel is a slideshow component that displays one slide at a time. Users can move between slides using Previous and Next controls or slide indicators. A carousel can contain text, images, cards or other HTML content.
What You Will Learn
- Understand Bootstrap Carousel.
- Create carousel slides.
- Add Previous and Next controls.
- Add slide indicators.
- Enable automatic sliding.
- Control carousel behavior using Bootstrap attributes.
How Carousel Works
Step 1
The carousel displays the active slide.
Step 2
The user moves to the next slide.
Step 3
The next slide becomes active.
1. Basic Carousel
The carousel class creates the carousel
container. The carousel-inner element
contains slides, and carousel-item defines
each slide. Add active to the first slide
so that it is visible when the page loads.
Program Code
<div id="basicCarousel"
class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="bg-primary text-white p-5 text-center">
<h3>First Slide</h3>
<p>Welcome to Bootstrap Carousel</p>
</div>
</div>
<div class="carousel-item">
<div class="bg-success text-white p-5 text-center">
<h3>Second Slide</h3>
<p>Learn responsive web design</p>
</div>
</div>
</div>
<button class="carousel-control-prev"
type="button"
data-bs-target="#basicCarousel"
data-bs-slide="prev">
<span class="carousel-control-prev-icon"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next"
type="button"
data-bs-target="#basicCarousel"
data-bs-slide="next">
<span class="carousel-control-next-icon"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
Program Output
2. Carousel with Indicators
Indicators are small buttons that represent individual slides. Users can click an indicator to jump directly to the corresponding slide.
Program Code
<div id="indicatorCarousel"
class="carousel slide">
<div class="carousel-indicators">
<button type="button"
data-bs-target="#indicatorCarousel"
data-bs-slide-to="0"
class="active"
aria-current="true"
aria-label="Slide 1"></button>
<button type="button"
data-bs-target="#indicatorCarousel"
data-bs-slide-to="1"
aria-label="Slide 2"></button>
</div>
<div class="carousel-inner">
<div class="carousel-item active">
<div class="bg-primary text-white text-center p-5">
First Slide
</div>
</div>
<div class="carousel-item">
<div class="bg-success text-white text-center p-5">
Second Slide
</div>
</div>
</div>
</div>
Program Output
Click the small indicators at the bottom to change slides.
3. Automatic Carousel
Use data-bs-ride="carousel" to start a
carousel automatically. The
data-bs-interval attribute sets the time
between slides in milliseconds. For example,
3000 means three seconds.
Program Code
<div id="autoCarousel"
class="carousel slide"
data-bs-ride="carousel"
data-bs-interval="3000">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="bg-primary text-white text-center p-5">
First Slide
</div>
</div>
<div class="carousel-item">
<div class="bg-success text-white text-center p-5">
Second Slide
</div>
</div>
</div>
</div>
Program Output
This example automatically changes slides approximately every three seconds.
Important Classes and Attributes
| Class / Attribute | Purpose |
|---|---|
carousel |
Creates the carousel component. |
slide |
Enables slide transition effects. |
carousel-inner |
Contains all carousel slides. |
carousel-item |
Defines an individual slide. |
active |
Marks the initially visible slide. |
carousel-indicators |
Creates buttons for selecting slides. |
data-bs-slide="prev" |
Moves to the previous slide. |
data-bs-slide="next" |
Moves to the next slide. |
data-bs-ride="carousel" |
Enables automatic carousel initialization. |
data-bs-interval |
Sets the automatic slide interval in milliseconds. |
Real-Life Example
An institute website can use a carousel on its home page to highlight available courses, upcoming batches, student projects and important announcements. Each slide can contain a heading, description and call-to-action button.
Common Mistakes
-
Forgetting to add the
activeclass to the first carousel item. -
Using the wrong ID in
data-bs-target. - Forgetting to load the Bootstrap JavaScript bundle.
- Reusing the same carousel ID for multiple components.
- Setting the interval in seconds instead of milliseconds.
Summary
- A carousel displays one slide at a time.
- Use
carousel-itemto define slides. - Use Previous and Next controls to navigate.
- Indicators allow direct navigation to a slide.
- Automatic sliding can be configured using data attributes.