📖 Scenario: You are creating a simple image carousel for a website homepage. The carousel will show three images that users can slide through using controls.
🎯 Goal: Build a Bootstrap carousel with three slides and navigation controls to move between slides.
📋 What You'll Learn
Use Bootstrap 5 carousel structure
Create three carousel items with images
Add previous and next controls
Ensure the carousel is accessible with proper aria labels
Make the carousel responsive
💡 Why This Matters
🌍 Real World
Carousels are common on websites to showcase images, products, or promotions in a small space with user-friendly navigation.
💼 Career
Knowing how to implement accessible and responsive carousels is valuable for front-end developers working on modern websites and user interfaces.
Progress0 / 4 steps
1
Create the basic carousel structure with three slides
Create a div with class carousel slide and id myCarousel. Inside it, add a div with class carousel-inner containing three div elements with class carousel-item. The first carousel-item should also have the class active. Inside each carousel-item, add an img tag with src attributes set to image1.jpg, image2.jpg, and image3.jpg respectively, and alt attributes describing the images as Slide 1, Slide 2, and Slide 3.
Bootsrap
Hint
Remember the first slide needs the active class to show initially.
2
Add carousel controls for previous and next navigation
Below the carousel-inner div, add two button elements for controls. The first button should have classes carousel-control-prev and type="button", with data-bs-target="#myCarousel" and data-bs-slide="prev". Inside it, add a span with class carousel-control-prev-icon and aria-hidden="true", and a span with class visually-hidden containing the text Previous. The second button is similar but uses carousel-control-next, data-bs-slide="next", and text Next.
Bootsrap
Hint
Make sure the buttons have the correct data-bs-target and data-bs-slide attributes.
3
Add carousel indicators for slide navigation
Above the carousel-inner div, add a div with class carousel-indicators. Inside it, add three button elements. Each button should have type="button", data-bs-target="#myCarousel", and data-bs-slide-to attributes set to 0, 1, and 2 respectively. The first button should also have the class active and aria-current="true". All buttons should have aria-label attributes set to Slide 1, Slide 2, and Slide 3.
Bootsrap
Hint
Indicators help users jump to a specific slide. The first indicator must be active.
4
Add responsive and accessibility enhancements
Add the class d-block w-100 to all img tags inside the carousel items to make images responsive. Ensure all button controls and indicators have appropriate aria-label attributes for accessibility. Confirm the carousel has the role="region" attribute and aria-label="Image Carousel" on the main div with id myCarousel.
Bootsrap
Hint
Adding role="region" and aria-label helps screen readers understand the carousel area.
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
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.
Step 2: Identify their function
These buttons allow users to manually move the slides backward (previous) or forward (next) in the carousel.
Final Answer:
They create buttons to move slides backward and forward. -> Option D
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?
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.
Step 2: Compare options
Previous matches the official Bootstrap 5 syntax exactly. Other options use incorrect classes, tags, or attributes.
Final Answer:
Option A is the correct Bootstrap 5 previous control button structure. -> Option A
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?
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
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.
Step 2: Identify missing attribute in the code
The given button lacks data-bs-target="#carouselID", so it cannot control any carousel.
Final Answer:
Missing data-bs-target attribute to specify the carousel ID. -> Option C
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?