Carousel, Toasts, Tooltips and Popovers
Some Bootstrap parts need its JavaScript file (bootstrap.bundle.min.js). Many work with just data-bs-* attributes, no code needed.
These components add motion and feedback: a carousel to show banners or product photos, a toast to confirm "Added to cart", tooltips to explain an icon. They're built and tested by the Bootstrap team, including keyboard support and screen-reader labels, which is hard to get right when building your own.
Carousel - a slideshow
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div id="offers" class="carousel slide" data-bs-ride="carousel" style="max-width: 520px">
<div class="carousel-indicators">
<button type="button" data-bs-target="#offers" data-bs-slide-to="0" class="active" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#offers" data-bs-slide-to="1" aria-label="Slide 2"></button>
</div>
<div class="carousel-inner rounded-3">
<div class="carousel-item active"><div class="p-5 text-center text-bg-primary">Big Diwali Sale</div></div>
<div class="carousel-item"><div class="p-5 text-center text-bg-success">Free delivery over ₹499</div></div>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#offers" data-bs-slide="prev">
<span class="carousel-control-prev-icon"></span><span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#offers" data-bs-slide="next">
<span class="carousel-control-next-icon"></span><span class="visually-hidden">Next</span>
</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>▶ Try it yourselfToasts, tooltips and popovers
A toast is a small message that pops up ("Added to cart"). Tooltips and popovers show help when you hover or click. These three must be switched on with a little JavaScript.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<button class="btn btn-success" id="addBtn">Add to cart</button>
<button class="btn btn-outline-secondary" data-bs-toggle="tooltip" data-bs-title="Free returns within 7 days">Returns?</button>
<button class="btn btn-outline-primary" data-bs-toggle="popover" data-bs-title="Delivery" data-bs-content="Usually 2-4 days in metro cities.">Delivery info</button>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="cartToast" class="toast" role="status" aria-live="polite">
<div class="toast-header"><strong class="me-auto">Terixto</strong><button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button></div>
<div class="toast-body">✅ Added to your cart</div>
</div>
</div>
<script>
window.addEventListener("load", () => {
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(el => new bootstrap.Tooltip(el));
document.querySelectorAll('[data-bs-toggle="popover"]').forEach(el => new bootstrap.Popover(el));
const toast = bootstrap.Toast.getOrCreateInstance(document.getElementById("cartToast"));
document.getElementById("addBtn").addEventListener("click", () => toast.show());
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>▶ Try it yourselfDon't put important information only in a tooltip. Phones have no hover, so many users never see it.
Key points
- Interactive components need bootstrap.bundle.min.js.
- Carousels work with data attributes; toasts, tooltips and popovers need a line of JavaScript to start.
- Don't hide important info in tooltips.
📝 Test yourself
1. Which file do carousels, toasts and tooltips need?
2. Which components must you switch on with JavaScript yourself?
3. Fill in the attribute that makes a button show a tooltip:
<button data-bs-toggle="" data-bs-title="Help">?</button>
