TExcelTAD
Home › Bootstrap › BS5 Carousel

Bootstrap 5 Carousel

A carousel is a slideshow: one slide is visible at a time, and it cycles through the others automatically or when the user clicks the arrows or indicators. It needs Bootstrap's JavaScript. In this lesson the "images" are gradients and emoji so the examples work offline.

Structure

Class / attribute Purpose
.carousel + .slide Wrapper (.slide adds the sliding animation)
data-bs-ride="carousel" Start cycling automatically on page load
.carousel-inner Holds the slides
.carousel-item One slide — exactly one must also have .active
.carousel-control-prev / -next Arrow buttons (data-bs-target + data-bs-slide="prev"/"next")
.carousel-indicators Dots (data-bs-slide-to="0", "1", …)
.carousel-caption Text over a slide
.carousel-fade Cross-fade instead of slide
data-bs-interval="ms" Per-slide delay (default 5000)

A carousel with controls and indicators

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .slide-box { height: 220px; display: flex; align-items: center; justify-content: center; font-size: 4rem; }
  </style>
</head>
<body class="p-2">
  <div id="demo" class="carousel slide" data-bs-ride="carousel">
    <div class="carousel-indicators">
      <button type="button" data-bs-target="#demo" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
      <button type="button" data-bs-target="#demo" data-bs-slide-to="1" aria-label="Slide 2"></button>
      <button type="button" data-bs-target="#demo" data-bs-slide-to="2" aria-label="Slide 3"></button>
    </div>
    <div class="carousel-inner rounded-3">
      <div class="carousel-item active">
        <div class="slide-box" style="background: linear-gradient(135deg, #0d6efd, #6f42c1);">🌊</div>
        <div class="carousel-caption"><h5>Ocean</h5><p class="mb-0">Slide one</p></div>
      </div>
      <div class="carousel-item">
        <div class="slide-box" style="background: linear-gradient(135deg, #198754, #20c997);">🌲</div>
        <div class="carousel-caption"><h5>Forest</h5><p class="mb-0">Slide two</p></div>
      </div>
      <div class="carousel-item">
        <div class="slide-box" style="background: linear-gradient(135deg, #fd7e14, #dc3545);">🌋</div>
        <div class="carousel-caption"><h5>Volcano</h5><p class="mb-0">Slide three</p></div>
      </div>
    </div>
    <button class="carousel-control-prev" type="button" data-bs-target="#demo" data-bs-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="visually-hidden">Previous</span>
    </button>
    <button class="carousel-control-next" type="button" data-bs-target="#demo" data-bs-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="visually-hidden">Next</span>
    </button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Hovering the carousel pauses it; it also stops cycling while the browser tab is hidden. The captions are hidden on very small screens in many designs — add .d-none .d-md-block to a caption to do that.

Tip: With real photos, put an <img class="d-block w-100" alt="…"> inside each .carousel-item instead of the coloured div. Give all images the same aspect ratio, or the carousel height will jump between slides.

Fade, timing and dark controls

.carousel-fade cross-fades the slides. data-bs-interval on each item sets how long it stays. For light slides, data-bs-theme="dark" on the carousel makes the arrows, dots and captions dark so they remain visible.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>.s { height: 180px; display: grid; place-items: center; font-size: 1.5rem; }</style>
</head>
<body class="p-2">
  <div id="quotes" class="carousel slide carousel-fade" data-bs-ride="carousel" data-bs-theme="dark">
    <div class="carousel-inner border rounded-3">
      <div class="carousel-item active" data-bs-interval="1500"><div class="s bg-warning-subtle">“Fast one” (1.5 s)</div></div>
      <div class="carousel-item" data-bs-interval="4000"><div class="s bg-info-subtle">“Slow one” (4 s)</div></div>
      <div class="carousel-item" data-bs-interval="2500"><div class="s bg-success-subtle">“Middle one” (2.5 s)</div></div>
    </div>
    <button class="carousel-control-prev" type="button" data-bs-target="#quotes" data-bs-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span><span class="visually-hidden">Previous</span>
    </button>
    <button class="carousel-control-next" type="button" data-bs-target="#quotes" data-bs-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span><span class="visually-hidden">Next</span>
    </button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Controlling it with JavaScript

The bootstrap.Carousel API has next(), prev(), to(index), pause() and cycle(). The slid.bs.carousel event tells you which slide is now showing.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>.s { height: 140px; display: grid; place-items: center; font-size: 3rem; color: #fff; }</style>
</head>
<body class="p-2">
  <div id="steps" class="carousel slide">
    <div class="carousel-inner rounded-3">
      <div class="carousel-item active"><div class="s bg-primary">1️⃣</div></div>
      <div class="carousel-item"><div class="s bg-success">2️⃣</div></div>
      <div class="carousel-item"><div class="s bg-danger">3️⃣</div></div>
    </div>
  </div>
  <div class="d-flex gap-2 mt-2">
    <button class="btn btn-sm btn-outline-dark" id="prev">◀ Back</button>
    <button class="btn btn-sm btn-outline-dark" id="first">First</button>
    <button class="btn btn-sm btn-dark" id="next">Next ▶</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const el = document.getElementById('steps');
    const carousel = new bootstrap.Carousel(el, { ride: false, wrap: false });
    document.getElementById('prev').onclick = () => carousel.prev();
    document.getElementById('next').onclick = () => carousel.next();
    document.getElementById('first').onclick = () => carousel.to(0);
    el.addEventListener('slid.bs.carousel', (e) => console.log('now on step', e.to + 1));
  </script>
</body>
</html>

With ride: false the carousel never auto-plays, and wrap: false stops it from jumping from the last slide back to the first — useful for step-by-step wizards.

Note: Auto-playing content can distract or disorient some users. Keep intervals generous, and always provide visible controls to move or pause.

Test yourself

How many .carousel-items must have the .active class initially?

  1. None
  2. Exactly one
  3. All of them
  4. At least two

Which attribute makes a carousel start cycling automatically on page load?

  1. data-bs-autoplay="true"
  2. data-bs-ride="carousel"
  3. data-bs-cycle
  4. data-bs-interval="auto"

Which class replaces the sliding animation with a cross-fade?

  1. .fade
  2. .carousel-fade
  3. .slide-fade
  4. .carousel-dissolve