Bootstrap 5 Toasts component
Responsive toast built with the latest Bootstrap 5. Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems.
Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.
Basic toast
To encourage extensible and predictable toasts, we recommend a header and body. Toast headers use display: flex
, allowing easy alignment of content thanks to our margin and flexbox utilities.
Toasts are as flexible as you need and have very little required markup. At a minimum, we require a single element to contain your “toasted” content and strongly encourage a dismiss button.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
Live example
Clicking the button below. The toast will be displayed in the bottom-left of the page.
<button type="button" class="btn btn-primary" id="liveToastBtn">Show toast</button>
<div class="toast-container position-fixed bottom-0 start-0 p-6">
<div class="toast" id="liveToast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-success"><i class="fa-solid fa-circle-check fa-lg"></i></span>
<div class="d-flex flex-column flex-grow-1 gap-2">
<div class="d-flex align-items-center">
<span class="fw-semibold">#1 Your changes were saved</span>
<button type="button" class="btn-close btn-close-sm ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
<span>I will auto dismiss after 8 seconds.</span>
</div>
</div>
</div>
</div>
</div>
Toasts are opt-in for performance reasons, so you must initialize toast yourself via js new bootstrap.Toast()
.
JS
const toastTrigger = document.getElementById("liveToastBtn");
const toastLiveExample = document.getElementById("liveToast");
toastTrigger.addEventListener("click", () => {
const toast = new bootstrap.Toast(toastLiveExample);
toast.show();
});
Color schemes
For fancy style? you can create different toast color schemes with our color and background utilities, likes .text-bg-{color}
to the .toast
, and then added .btn-close-white
to our close button.
Success example
Adding .text-bg-success
to the .toast
, and then added .btn-close-white
to our close button. For a crisp edge, we remove the default border with .border-0.
<div class="toast fade show text-bg-success" role="alert">
<div class="toast-body">
<div class="d-flex gap-4">
<span><i class="fa-solid fa-circle-check fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Welcome to the room</span>
<button type="button"
class="btn-close btn-close-white btn-close-sm ms-auto"
data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
Danger example
Adding .text-bg-danger
to the .toast
, and then added .btn-close-white
to our close button.
<div class="toast fade show text-bg-danger" role="alert">
<div class="toast-body">
<div class="d-flex gap-4">
<span><i class="fa-solid fa-circle-exclamation fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">We are having issues</span>
<button type="button"
class="btn-close btn-close-white btn-close-sm ms-auto"
data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
Warning example
Adding .text-bg-warning
to the .toast
, and then added .btn-close-black
to our close button.
<div class="toast fade show text-bg-warning" role="alert">
<div class="toast-body">
<div class="d-flex gap-4">
<span><i class="fa-solid fa-triangle-exclamation fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Trying again...</span>
<button type="button"
class="btn-close btn-close-sm btn-close-black ms-auto"
data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
Placement
Place toasts with custom CSS as you need them. The top right is often used for notifications, as is the top middle. If you’re only ever going to show one toast at a time, put the positioning styles right on the .toast
. Check out our position utilities for more information.
Top-Left
use the position-fixed
, top-0
and start-0
utility classes on a .toast-container
element to position Toasts at the top left of the viewport.
<div class="toast-container top-0 start-0 p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Top-Center
use the position-fixed
, top-0
, start-50
and translate-middle-x
utility classes on a .toast-container
element to position Toasts at the top center of the viewport.
<div class="toast-container top-0 start-50 translate-middle-x p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Top-Right
use the position-fixed
, top-0
, end-0
utility classes on a .toast-container
element to position Toasts at the top right of the viewport.
<div class="toast-container top-0 end-0 p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Bottom-Left
use the position-fixed
, bottom-0
, and start-0
utility classes on a .toast-container
element to position Toasts at the bottom left of the viewport.
<div class="toast-container bottom-0 start-0 p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Bottom-Center
use the position-fixed
, bottom-0
, start-50
and translate-middle-x
utility classes on a .toast-container
element to position Toasts at the bottom left of the viewport.
<div class="toast-container bottom-0 start-50 translate-middle-x p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Bottom-Right
use the position-fixed
, bottom-0
, and end-0
utility classes on a .toast-container
element to position Toasts at the bottom left of the viewport.
<div class="toast-container bottom-0 end-0 p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Middle-Left
use the position-fixed
, top-50
, start-0
and translate-middle-y
utility classes on a .toast-container
element to position Toasts at the middle left of the viewport.
<div class="toast-container top-50 start-0 translate-middle-y p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Middle-Right
use the position-fixed
, top-50
, end-0
and translate-middle-y
utility classes on a .toast-container
element to position Toasts at the middle right of the viewport.
<div class="toast-container top-50 end-0 translate-middle-y p-3">
<div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
<div class="d-flex gap-4">
<span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
<div class="d-flex flex-grow-1 align-items-center">
<span class="fw-semibold">Hello, world! This is a toast message.</span>
<button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
</div>
</div>
</div>
</div>
Stack toasts
You can stack toasts by wrapping them in a toast container with toast-container
, which will vertically add some spacing.
<div class="toast-container position-static">
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<i class="fa-solid fa-circle-info fa-lg text-primary me-2"></i>
<strong class="me-auto">Bootstrap</strong>
<small class="text-body-secondary">just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<i class="fa-solid fa-circle-info fa-lg text-primary me-2"></i>
<strong class="me-auto">Bootstrap</strong>
<small class="text-body-secondary">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</div>
</div>
</div>
Custom toast
Customize your toasts by removing sub-components, tweaking them with utilities, or by adding your own markup. Here we’ve created a simpler toast by removing the default .toast-header
, adding a custom hide icon from Bootstrap Icons, and using some flexbox utilities to adjust the layout.
<div class="toast align-items-center" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body">
Hello, world! This is a toast message.
</div>
<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
Alternatively, you can also add additional controls and components to toasts.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
Hello, world! This is a toast message.
<div class="mt-2 pt-2 border-top">
<button type="button" class="btn btn-primary btn-sm">Take action</button>
<button type="button" class="btn btn-default btn-sm" data-bs-dismiss="toast">Close</button>
</div>
</div>
</div>
Accessibility
Toasts are intended to be small interruptions to your visitors or users, so to help those with screen readers and similar assistive technologies, you should wrap your toasts in an aria-live
region. Changes to live regions (such as injecting/updating a toast component) are automatically announced by screen readers without needing to move the user’s focus or otherwise interrupt the user. Additionally, include aria-atomic="true"
to ensure that the entire toast is always announced as a single (atomic) unit, rather than just announcing what was changed (which could lead to problems if you only update part of the toast’s content, or if displaying the same toast content at a later point in time). If the information needed is important for the process, e.g. for a list of errors in a form, then use the alert component instead of toast.
Note that the live region needs to be present in the markup before the toast is generated or updated. If you dynamically generate both at the same time and inject them into the page, they will generally not be announced by assistive technologies.
You also need to adapt the role
and aria-live
level depending on the content. If it’s an important message like an error, use role="alert" aria-live="assertive"
, otherwise use role="status" aria-live="polite"
attributes.
As the content you’re displaying changes, be sure to update the delay
timeout so that users have enough time to read the toast.
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-bs-delay="10000">
<div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</div>
When using autohide: false
, you must add a close button to allow users to dismiss the toast.
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-bs-autohide="false">
<div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</div>
While technically it’s possible to add focusable/actionable controls (such as additional buttons or links) in your toast, you should avoid doing this for autohiding toasts. Even if you give the toast a long delay timeout
, keyboard and assistive technology users may find it difficult to reach the toast in time to take action (since toasts don’t receive focus when they are displayed). If you absolutely must have further controls, we recommend using a toast with autohide: false
.