Bootstrap 5 Toast
A toast is a small, temporary notification that slides in — usually in a corner of the screen — and disappears on its own: "Message sent", "Item added to cart", "Connection lost". The name comes from toast popping out of a toaster. Toasts need Bootstrap's JavaScript.
Toasts are opt-in
Toasts are hidden by default and must be shown with JavaScript: bootstrap.Toast.getOrCreateInstance(el).show().
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<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 type="button" class="btn btn-primary" id="showToast">Send message</button>
<div class="toast mt-3" id="sent" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<span class="me-2">✉️</span>
<strong class="me-auto">Messenger</strong>
<small>just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">Your message was sent.</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
const toastEl = document.getElementById('sent');
document.getElementById('showToast').addEventListener('click', () => {
bootstrap.Toast.getOrCreateInstance(toastEl).show();
});
toastEl.addEventListener('hidden.bs.toast', () => console.log('toast hidden'));
</script>
</body>
</html>By default a toast hides itself after 5 seconds (delay: 5000). While the mouse is over the toast (or focus is inside it) the timer pauses, and it restarts when the pointer or focus leaves.
Structure and options
| Class / attribute | Purpose |
|---|---|
.toast |
The toast box (hidden until shown) |
.toast-header / .toast-body |
Optional header / main text |
.toast-container |
Stacks several toasts with spacing |
.btn-close + data-bs-dismiss="toast" |
Close button |
data-bs-autohide="false" |
Don't hide automatically |
data-bs-delay="ms" |
Time before hiding (default 5000) |
data-bs-animation="false" |
No fade |
role="alert" aria-live="assertive" |
Important message, read immediately |
role="status" aria-live="polite" |
Less urgent, read when the user is idle |
Positioning toasts
Put toasts in a .toast-container and position it with utilities. position-fixed bottom-0 end-0 p-3 gives the classic bottom-right corner.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<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" style="min-height: 260px;">
<button class="btn btn-success" id="add">🛒 Add to cart</button>
<div class="toast-container position-fixed bottom-0 end-0 p-3" id="stack"></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
let count = 0;
document.getElementById('add').addEventListener('click', () => {
count++;
const el = document.createElement('div');
el.className = 'toast align-items-center text-bg-success border-0';
el.setAttribute('role', 'status');
el.setAttribute('aria-live', 'polite');
el.innerHTML = `<div class="d-flex">
<div class="toast-body">Item #${count} added to your cart.</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>`;
document.getElementById('stack').append(el);
el.addEventListener('hidden.bs.toast', () => el.remove());
new bootstrap.Toast(el, { delay: 3000 }).show();
console.log('toast', count, 'shown');
});
</script>
</body>
</html>Click several times: the toasts stack upwards and each disappears after 3 seconds. The example removes hidden toasts from the page so they don't pile up.
Colours and persistent toasts
Use .text-bg-{color} for coloured toasts (with .btn-close-white on dark ones). With data-bs-autohide="false", the toast stays until the user closes it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<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 class="toast-container">
<div class="toast text-bg-danger border-0" role="alert" aria-live="assertive" aria-atomic="true" data-bs-autohide="false">
<div class="d-flex">
<div class="toast-body">⚠️ Connection lost. Retrying…</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
<div class="toast" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false">
<div class="toast-body">
New version available.
<div class="mt-2 pt-2 border-top">
<button type="button" class="btn btn-primary btn-sm">Update</button>
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="toast">Later</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.querySelectorAll('.toast').forEach((el) => bootstrap.Toast.getOrCreateInstance(el).show());
</script>
</body>
</html>Tip: Toasts should never be the only place important information appears — they vanish, and users may miss them. Don't auto-hide toasts that contain actions like "Undo" too quickly.
Test yourself
How do you display a toast?
- Add .show in the HTML only
- Call bootstrap.Toast.getOrCreateInstance(el).show()
- Use data-bs-toggle="toast" on the toast
- Toasts show automatically on load
How long does a toast stay visible by default?
- 1 second
- 3 seconds
- 5 seconds
- Until closed
Which element stacks several toasts with spacing between them?
- .toast-stack
- .toast-container
- .toast-group
- .toast-list