Carousel captions help explain what each slide shows. They make the carousel easier to understand.
Carousel captions in Bootsrap
<div class="carousel-caption"> <h5>Slide title</h5> <p>Some description text.</p> </div>
The carousel-caption class places text over the carousel image.
You can use headings and paragraphs inside for titles and descriptions.
<div class="carousel-caption"> <h5>First Slide</h5> <p>This is the first slide caption.</p> </div>
<div class="carousel-caption d-none d-md-block"> <h5>Second Slide</h5> <p>Visible only on medium and larger screens.</p> </div>
<div class="carousel-caption text-start"> <h5>Left Aligned Caption</h5> <p>Text aligned to the left side.</p> </div>
This example shows a Bootstrap carousel with three slides. Each slide has an image and a caption with a title and description. Captions are visible on medium and larger screens. The third slide's caption text is aligned to the left.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Carousel with Captions</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main> <div id="carouselExampleCaptions" class="carousel slide" data-bs-ride="carousel"> <div class="carousel-indicators"> <button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button> <button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="1" aria-label="Slide 2"></button> <button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="2" aria-label="Slide 3"></button> </div> <div class="carousel-inner"> <div class="carousel-item active"> <img src="https://via.placeholder.com/800x400.png?text=First+Slide" class="d-block w-100" alt="First slide image" /> <div class="carousel-caption d-none d-md-block"> <h5>First Slide Title</h5> <p>This is the caption for the first slide.</p> </div> </div> <div class="carousel-item"> <img src="https://via.placeholder.com/800x400.png?text=Second+Slide" class="d-block w-100" alt="Second slide image" /> <div class="carousel-caption d-none d-md-block"> <h5>Second Slide Title</h5> <p>This caption appears on medium and larger screens.</p> </div> </div> <div class="carousel-item"> <img src="https://via.placeholder.com/800x400.png?text=Third+Slide" class="d-block w-100" alt="Third slide image" /> <div class="carousel-caption d-none d-md-block text-start"> <h5>Third Slide Left Caption</h5> <p>Left aligned caption text for the third slide.</p> </div> </div> </div> <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleCaptions" 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="#carouselExampleCaptions" data-bs-slide="next"> <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>
Use alt attributes on images for accessibility.
Captions can be hidden on small screens using Bootstrap's display utility classes like d-none d-md-block.
Use aria-label and visually-hidden classes for better screen reader support on controls.
Carousel captions add text overlays to slides to explain content.
Use the carousel-caption class inside each carousel-item.
Control caption visibility and alignment with Bootstrap utility classes.
Practice
carousel-caption class in a Bootstrap carousel?Solution
Step 1: Understand the role of carousel-caption
Thecarousel-captionclass is used inside eachcarousel-itemto add text overlays on slides.Step 2: Differentiate from other carousel features
Other options control speed, navigation, or styling but do not add text overlays.Final Answer:
To add text overlays on each slide explaining the content -> Option AQuick Check:
carousel-caption adds text overlays = C [OK]
- Confusing caption with navigation controls
- Thinking it changes slide speed
- Assuming it styles background color
Solution
Step 1: Identify correct caption container
The Bootstrap documentation specifies using<div class='carousel-caption'>inside eachcarousel-item.Step 2: Check other options for correctness
<caption>is for tables,carousel-textis not a Bootstrap class, andspanis less common and outside the item is incorrect.Final Answer:
<div class='carousel-caption'>Caption Text</div> inside carousel-item -> Option BQuick Check:
Use div.carousel-caption inside carousel-item = B [OK]
- Using <caption> tag which is for tables
- Placing caption outside carousel-item
- Using wrong class names like carousel-text
<div class='carousel-caption text-start'>Caption</div>
What will be the alignment of the caption text when rendered?
Solution
Step 1: Understand Bootstrap text alignment classes
The classtext-startaligns text to the left side in Bootstrap.Step 2: Apply alignment to carousel-caption
Sincecarousel-captioncontainstext-start, the caption text will appear left aligned on the slide.Final Answer:
Text aligned to the left side of the slide -> Option DQuick Check:
text-start means left aligned = A [OK]
- Confusing text-start with center or right alignment
- Assuming captions are always centered
- Thinking text-start hides text
<div class='carousel-caption text-center'>Welcome</div>
But the caption text does not appear on the slide. What is the likely cause?
Solution
Step 1: Check caption placement rules
Bootstrap requirescarousel-captionto be inside acarousel-itemfor it to show properly.Step 2: Evaluate other options
text-centerdoes not hide text, captions must be insidecarousel-inner, anddivis the correct tag.Final Answer:
Missing carousel-item wrapper around the caption -> Option AQuick Check:
Caption must be inside carousel-item = A [OK]
- Placing caption outside carousel-item
- Thinking text-center hides text
- Using wrong tag for caption
Solution
Step 1: Match caption alignment classes to slides
The first slide usestext-startfor left alignment, second usestext-centerfor center, and third usestext-endfor right alignment.Step 2: Verify code structure and correctness
Each caption is inside itscarousel-itemwith correct alignment classes matching the requirement.Final Answer:
Code snippet with first left, second center, third right using text-start, text-center, text-end respectively -> Option CQuick Check:
Use text-start, text-center, text-end for left, center, right captions = D [OK]
- Mixing alignment classes order
- Using same alignment for all captions
- Placing captions outside carousel-item
Ready to prove it?
Pass the test and this pattern moves unknown
