🎠 Bootstrap 5 – Carousel: Create Responsive Image Sliders & Slideshows
🧲 Introduction – What Is Carousel in Bootstrap 5?
The carousel in Bootstrap 5 is a flexible, responsive component that lets you create sliding banners, image galleries, testimonials, and featured content sliders. With built-in controls, indicators, and autoplay support, it’s widely used in landing pages and hero sections.
🎯 In this guide, you’ll learn:
- How to create carousels using HTML and
data-bs-*
attributes - Use controls, indicators, and intervals
- Customize transitions, auto-slide timing, and responsiveness
- Best practices for mobile and accessibility
✅ Example 1 – Basic Image Carousel
<div id="carouselExample" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img src="img1.jpg" class="d-block w-100" alt="First slide">
</div>
<div class="carousel-item">
<img src="img2.jpg" class="d-block w-100" alt="Second slide">
</div>
<div class="carousel-item">
<img src="img3.jpg" class="d-block w-100" alt="Third slide">
</div>
</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>
🔍 Code Explanation:
Class/Attribute | Purpose |
---|---|
.carousel | Main carousel wrapper |
.carousel-inner | Contains all carousel slides |
.carousel-item | Individual slide item |
.active | Activates the visible slide |
.carousel-control-prev/next | Navigation buttons for previous/next |
data-bs-ride="carousel" | Enables automatic cycling |
data-bs-slide="prev/next" | Controls slide direction |
✅ Example 2 – Carousel with Indicators
<div id="carouselWithIndicators" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselWithIndicators" data-bs-slide-to="0" class="active"></button>
<button type="button" data-bs-target="#carouselWithIndicators" data-bs-slide-to="1"></button>
<button type="button" data-bs-target="#carouselWithIndicators" data-bs-slide-to="2"></button>
</div>
<div class="carousel-inner">
<!-- Same .carousel-item structure as above -->
</div>
</div>
🔍 Use Case:
- Dots below the carousel to jump to specific slides
- Improves usability and slide navigation
✅ Example 3 – Carousel with Captions
<div class="carousel-item active">
<img src="slide1.jpg" class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Welcome</h5>
<p>Responsive carousel with caption overlay</p>
</div>
</div>
🔍 Tips:
.carousel-caption
: Places text over the imaged-none d-md-block
: Hides captions on small screens
✅ Customizing Carousel Behavior
Attribute | Effect |
---|---|
data-bs-interval="5000" | Set auto-slide interval in milliseconds (default 5000) |
data-bs-pause="hover" | Pause carousel on mouse hover (default behavior) |
data-bs-wrap="false" | Prevent wrap-around when reaching last slide |
✅ JavaScript API for Manual Control
var myCarousel = document.querySelector('#carouselExample');
var carousel = new bootstrap.Carousel(myCarousel, {
interval: 3000,
pause: 'hover'
});
🛠️ Best Practices for Carousel Usage
Best Practice | Reason |
---|---|
Optimize images for performance | Reduces load time on mobile |
Use readable overlay text | Maintain contrast and visibility |
Don’t overuse auto-slide | Avoid UX fatigue; let users control navigation |
Add alt text to all images | Improves accessibility and SEO |
Avoid hiding critical content | Don’t rely solely on carousel for important information |
📌 Summary – Build Responsive Carousels with Bootstrap
The Bootstrap 5 Carousel offers a simple, responsive way to display sliding content—whether you’re showing featured images, customer testimonials, or banners.
🔍 Key Takeaways:
- Use
.carousel
,.carousel-inner
,.carousel-item
for structure - Add indicators and controls for full UX
- Use
data-bs-ride
,interval
, andpause
to customize behavior - Supports captions, fade transitions, and dynamic JS control
⚙️ Ideal for landing pages, portfolios, product sliders, and hero sections.
❓ FAQs – Bootstrap 5 Carousel
❓ How do I disable auto sliding in the carousel?
✅ Remove the data-bs-ride="carousel"
attribute or set interval: false
in JS.
❓ Can I use fade transitions instead of sliding?
✅ Yes. Add the .carousel-fade
class to the .carousel
element.
❓ Does Bootstrap carousel support touch/swipe?
✅ Yes. Bootstrap 5 has built-in swipe support on mobile devices.
❓ How can I change the slide duration?
✅ Set data-bs-interval="3000"
on individual .carousel-item
or globally in JS.
Share Now :