Bootstrap 5 Tutorial
v5.3.0Bootstrap 5 Tutorial
Carousel in Bootstrap 5
Carousel: A slideshow component for cycling through elements—images or slides of text—like a carousel.
Basic Carousel
Here's a basic carousel with slides only. Note the presence of the indicators and controls.
<!-- Basic Carousel -->
<div id="carouselExample" class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active">
<Image width={400} height={300} src="slide1.jpg" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<Image width={400} height={300} src="slide2.jpg" class="d-block w-100" alt="...">
</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>Carousel with Indicators
<!-- Carousel with Indicators -->
<div id="carouselExampleIndicators" class="carousel slide">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="0" class="active" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="1" aria-label="Slide 2"></button>
</div>
<div class="carousel-inner">
<!-- Slides -->
</div>
<!-- Controls -->
</div>Carousel with Captions
<!-- Carousel with Captions -->
<div class="carousel-item active">
<Image width={400} height={300} src="..." class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Slide label</h5>
<p>Slide description text.</p>
</div>
</div>Crossfade Animation
<!-- Crossfade Carousel --> <div id="carouselExampleFade" class="carousel slide carousel-fade"> <!-- Carousel items --> </div>
Carousel with Dark Variant
<!-- Dark Variant Carousel --> <div id="carouselExampleDark" class="carousel carousel-dark slide"> <!-- Carousel content --> </div>
Carousel with Different Intervals
<!-- Different Intervals --> <div class="carousel-item active" data-bs-interval="1000"> <!-- Content with 1 second interval --> </div> <div class="carousel-item" data-bs-interval="2000"> <!-- Content with 2 second interval --> </div> <div class="carousel-item"> <!-- Default interval --> </div>
Carousel without Controls
Carousel with Thumbnails
Carousel with Cards
JavaScript Methods
Carousel JavaScript API
Methods
cycle()- Cycles through the carousel itemspause()- Stops the carousel from cyclingprev()- Cycles to the previous itemnext()- Cycles to the next itemto(index)- Cycles to a specific itemdispose()- Destroys the carousel
Events
slide.bs.carousel- Fires immediately when slide startsslid.bs.carousel- Fired when slide transition completes
// JavaScript Examples
var myCarousel = document.querySelector('#myCarousel')
var carousel = new bootstrap.Carousel(myCarousel)
// Go to specific slide
carousel.to(2)
// Pause carousel
carousel.pause()
// Resume cycling
carousel.cycle()
// Event listeners
myCarousel.addEventListener('slide.bs.carousel', function () {
console.log('Slide started')
})
myCarousel.addEventListener('slid.bs.carousel', function () {
console.log('Slide completed')
})Accessibility
Accessibility Best Practices
- Always include
alttext for images - Use
aria-labelfor controls and indicators - Include
visually-hiddentext for screen readers - Ensure keyboard navigation works
- Consider pausing auto-rotation for accessibility
- Provide alternative navigation methods
Best Practices
Do's
- ✅ Use for image galleries and featured content
- ✅ Include clear navigation controls
- ✅ Add captions for context
- ✅ Make it touch-friendly for mobile
- ✅ Optimize images for fast loading
- ✅ Test on different screen sizes
Don'ts
- ❌ Don't use for critical content
- ❌ Avoid too many slides (max 5-7)
- ❌ Don't make auto-rotation too fast
- ❌ Avoid low-contrast captions
- ❌ Don't forget about accessibility
- ❌ Avoid using for text-heavy content
Customization Examples
Custom CSS Styling
/* Custom Carousel Styles */
.custom-carousel .carousel-control-prev-icon,
.custom-carousel .carousel-control-next-icon {
background-color: rgba(0, 0, 0, 0.5);
border-radius: 50%;
width: 50px;
height: 50px;
background-size: 60%;
}
.custom-carousel .carousel-indicators button {
width: 12px;
height: 12px;
border-radius: 50%;
margin: 0 5px;
}
.custom-carousel .carousel-caption {
background: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 10px;
}
/* Responsive adjustments */
@media (max-width: 768px) {
.custom-carousel .carousel-caption h5 {
font-size: 1rem;
}
.custom-carousel .carousel-caption p {
font-size: 0.875rem;
}
}