Corousel
A carousel is a slideshow component that cycles through a series of images or content panels, one at a time. Bootstrap 5 ships a ready-made carousel that handles the sliding animation, the previous and next controls, the little indicator buttons and optional auto-play, all driven by data attributes so you do not need to write JavaScript for the basic version. It is often used for hero banners, product galleries and testimonials.
The required markup
A carousel needs an outer element with the class carousel and a unique id. Inside it sits a carousel-inner wrapper, and inside that, one carousel-item per slide. Exactly one slide must carry the active class, otherwise nothing is visible. The slide class adds the sliding transition.
<div id="promoCarousel" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators">
<button type="button" data-bs-target="#promoCarousel" data-bs-slide-to="0"
class="active" aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#promoCarousel" data-bs-slide-to="1"
aria-label="Slide 2"></button>
</div>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="beach.jpg" class="d-block w-100" alt="Sunset over a beach">
<div class="carousel-caption d-none d-md-block">
<h5>Summer trips</h5>
</div>
</div>
<div class="carousel-item">
<img src="hills.jpg" class="d-block w-100" alt="Green hills under clouds">
</div>
</div>
<button class="carousel-control-prev" type="button"
data-bs-target="#promoCarousel" 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="#promoCarousel" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
What each data attribute does
data-bs-targeton a control points at the carousel it should move, using the carousel'sid.data-bs-slide="prev"or"next"moves one step backward or forward.data-bs-slide-to="2"on an indicator jumps straight to that zero-based slide.data-bs-ride="carousel"starts auto-play when the page loads.data-bs-interval="4000"on a singlecarousel-itemsets how long that slide stays, in milliseconds.
Add carousel-fade next to slide if you prefer a cross-fade instead of a horizontal slide, and add data-bs-theme="dark" to the carousel when your images are light and the white controls would disappear.
Auto-play has a few more switches. Setting data-bs-ride="true" instead of "carousel" waits until the user interacts before cycling. Adding data-bs-pause="false" keeps slides moving even while the pointer hovers over them, though pausing on hover is usually kinder. data-bs-wrap="false" stops the carousel at the last slide instead of looping back to the first, and data-bs-touch="false" turns off swiping on touch screens.
Controlling it from JavaScript
For custom buttons or to pause the show, create an instance with bootstrap.Carousel.getOrCreateInstance(element). The instance has methods such as next(), prev(), to(index), pause() and cycle(). You can also listen for the slide.bs.carousel and slid.bs.carousel events, which fire before and after a transition.
Common reasons a carousel will not slide
If the controls do nothing, check that you loaded bootstrap.bundle.min.js, not only the CSS. Next, confirm that every data-bs-target value exactly matches the carousel id, including the #. Mixing Bootstrap 4 attributes such as data-ride and data-slide into Bootstrap 5 markup is another frequent cause, because version 5 only reads the data-bs- prefix. Finally, images of different heights make the carousel jump; give them a fixed height with object-fit: cover.
Layout shift is worth checking too. If the first image loads slowly, the carousel briefly has no height and the content below jumps up and then down again. Set explicit width and height attributes on each image, or give the slides a fixed aspect-ratio, so the browser reserves the space before the image arrives.
Keeping it usable
Write meaningful alt text for each image, keep the visually-hidden labels on the controls, and avoid hiding essential information on slides that a visitor may never reach.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.