Bird
Raised Fist0
Bootsrapmarkup~5 mins

Carousel slides and controls in Bootsrap

Choose your learning style10 modes available
Introduction

A carousel lets you show multiple images or content in one place by sliding them left or right. It saves space and makes your page look neat.

You want to display a photo gallery on a website homepage.
Showing product images one by one on an online store.
Highlighting different features or offers in a small area.
Creating a slideshow of testimonials or quotes.
Making a news ticker with headlines that slide automatically.
Syntax
Bootsrap
<div id="carouselExample" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <!-- Slide content here -->
    </div>
    <div class="carousel-item">
      <!-- Slide content here -->
    </div>
    <!-- More slides -->
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" 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="#carouselExample" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>

The carousel-inner holds all slides.

Only one carousel-item should have the active class to show first.

Examples
This is a slide with an image. The active class shows it first.
Bootsrap
<div class="carousel-item active">
  <img src="image1.jpg" class="d-block w-100" alt="First slide">
</div>
This button moves the carousel to the next slide when clicked.
Bootsrap
<button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
  <span class="carousel-control-next-icon" aria-hidden="true"></span>
  <span class="visually-hidden">Next</span>
</button>
The main container with an ID and classes to activate the carousel behavior.
Bootsrap
<div id="carouselExample" class="carousel slide" data-bs-ride="carousel">
  <!-- slides and controls here -->
</div>
Sample Program

This example creates a simple carousel with three slides. Each slide shows a placeholder image with text. The left and right buttons let you move between slides. The carousel also starts sliding automatically.

Bootsrap
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap Carousel Example</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <main class="container mt-5">
    <h1>Simple Image Carousel</h1>
    <div id="carouselExample" class="carousel slide" data-bs-ride="carousel" aria-label="Image Carousel">
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img src="https://via.placeholder.com/800x400?text=Slide+1" class="d-block w-100" alt="Slide 1">
        </div>
        <div class="carousel-item">
          <img src="https://via.placeholder.com/800x400?text=Slide+2" class="d-block w-100" alt="Slide 2">
        </div>
        <div class="carousel-item">
          <img src="https://via.placeholder.com/800x400?text=Slide+3" class="d-block w-100" alt="Slide 3">
        </div>
      </div>
      <button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" data-bs-slide="prev" aria-label="Previous Slide">
        <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="#carouselExample" data-bs-slide="next" aria-label="Next Slide">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Next</span>
      </button>
    </div>
  </main>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
OutputSuccess
Important Notes

Always add alt text to images for accessibility.

Use aria-label and visually-hidden classes to help screen readers understand controls.

Make sure only one slide has the active class at a time.

Summary

A carousel shows multiple slides in one space by sliding them.

Use carousel-inner for slides and carousel-control-prev/next for navigation buttons.

Bootstrap handles the sliding animation and controls automatically.

Practice

(1/5)
1. What is the main purpose of the .carousel-control-prev and .carousel-control-next classes in a Bootstrap carousel?
easy
A. They automatically start the carousel animation.
B. They style the carousel indicators at the bottom.
C. They add captions to each slide.
D. They create buttons to move slides backward and forward.

Solution

  1. Step 1: Understand carousel controls

    The classes .carousel-control-prev and .carousel-control-next are used to create navigation buttons on the left and right sides of the carousel.
  2. Step 2: Identify their function

    These buttons allow users to manually move the slides backward (previous) or forward (next) in the carousel.
  3. Final Answer:

    They create buttons to move slides backward and forward. -> Option D
  4. Quick Check:

    Carousel controls = navigation buttons [OK]
Hint: Controls always move slides left or right [OK]
Common Mistakes:
  • Confusing controls with indicators
  • Thinking controls add captions
  • Assuming controls start animation automatically
2. Which of the following is the correct HTML structure to add a previous control button in a Bootstrap 5 carousel?
easy
A. Previous
B.
Prev
C. Prev
D. Back

Solution

  1. Step 1: Check Bootstrap 5 control button syntax

    The correct control button uses button with class carousel-control-prev, attributes data-bs-target and data-bs-slide="prev", and includes an icon span and a visually hidden label for accessibility.
  2. Step 2: Compare options

    Previous matches the official Bootstrap 5 syntax exactly. Other options use incorrect classes, tags, or attributes.
  3. Final Answer:

    Option A is the correct Bootstrap 5 previous control button structure. -> Option A
  4. Quick Check:

    Bootstrap 5 controls use button with data-bs-slide [OK]
Hint: Look for data-bs-slide="prev" and correct classes [OK]
Common Mistakes:
  • Using old Bootstrap 4 attributes like data-slide
  • Missing aria-hidden or visually-hidden spans
  • Using wrong element tags like <a> or <div>
3. Given this Bootstrap carousel snippet, what will happen when the user clicks the next control button?
<div id="carouselExample" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">Slide 1</div>
    <div class="carousel-item">Slide 2</div>
    <div class="carousel-item">Slide 3</div>
  </div>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>
medium
A. The carousel will move from Slide 1 to Slide 2.
B. The carousel will jump directly to Slide 3.
C. Nothing will happen because the next button is missing the target.
D. The carousel will restart from Slide 1.

Solution

  1. Step 1: Identify the active slide and next button action

    The active slide is Slide 1. The next button has data-bs-slide="next" and targets the carousel by ID.
  2. Step 2: Understand carousel behavior on next click

    Clicking next moves the carousel forward by one slide, so it moves from Slide 1 to Slide 2.
  3. Final Answer:

    The carousel will move from Slide 1 to Slide 2. -> Option A
  4. Quick Check:

    Next button moves one slide forward [OK]
Hint: Next button moves active slide forward by one [OK]
Common Mistakes:
  • Thinking next jumps to last slide
  • Assuming missing target disables button
  • Confusing next with restart behavior
4. This Bootstrap carousel control button does not work as expected. What is the error?
<button class="carousel-control-prev" type="button" data-bs-slide="prev">
  <span class="carousel-control-prev-icon" aria-hidden="true"></span>
  <span class="visually-hidden">Previous</span>
</button>
medium
A. The button tag should be an anchor <a> tag.
B. Wrong class name; should be carousel-prev-control.
C. Missing data-bs-target attribute to specify the carousel ID.
D. Missing aria-label attribute on the button.

Solution

  1. Step 1: Check required attributes for carousel controls

    Bootstrap carousel control buttons must have data-bs-target to link the button to the carousel's ID.
  2. Step 2: Identify missing attribute in the code

    The given button lacks data-bs-target="#carouselID", so it cannot control any carousel.
  3. Final Answer:

    Missing data-bs-target attribute to specify the carousel ID. -> Option C
  4. Quick Check:

    Controls need data-bs-target to work [OK]
Hint: Always include data-bs-target with carousel ID [OK]
Common Mistakes:
  • Omitting data-bs-target attribute
  • Using wrong class names
  • Confusing button with anchor tag requirement
5. You want to create a Bootstrap carousel with 4 slides and add controls that loop infinitely. Which combination of attributes and classes ensures the carousel loops and controls work correctly?
<div id="myCarousel" class="carousel slide" ... >
  <div class="carousel-inner">
    <div class="carousel-item active">Slide 1</div>
    <div class="carousel-item">Slide 2</div>
    <div class="carousel-item">Slide 3</div>
    <div class="carousel-item">Slide 4</div>
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#myCarousel" data-bs-slide="prev">...</button>
  <button class="carousel-control-next" type="button" data-bs-target="#myCarousel" data-bs-slide="next">...</button>
</div>
hard
A. Add data-bs-interval="false" to stop auto sliding; controls loop automatically.
B. Add data-bs-ride="carousel" to the main div; controls with correct data-bs-target enable infinite looping.
C. Add data-bs-wrap="false" to disable looping; controls will loop slides.
D. No extra attributes needed; controls loop slides by default.

Solution

  1. Step 1: Enable automatic cycling and looping

    Adding data-bs-ride="carousel" starts the carousel automatically and enables looping by default.
  2. Step 2: Ensure controls target the correct carousel

    Controls must have data-bs-target="#myCarousel" and data-bs-slide attributes to move slides properly.
  3. Step 3: Understand looping behavior

    Bootstrap carousels loop infinitely by default when data-bs-ride="carousel" is present, so no extra attribute is needed to enable looping.
  4. Final Answer:

    Add data-bs-ride="carousel" to the main div; controls with correct data-bs-target enable infinite looping. -> Option B
  5. Quick Check:

    data-bs-ride="carousel" + correct controls = infinite loop [OK]
Hint: Use data-bs-ride="carousel" for looping with controls [OK]
Common Mistakes:
  • Using data-bs-wrap="false" disables looping
  • Assuming controls loop without data-bs-ride
  • Setting data-bs-interval="false" stops auto sliding
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard