Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They’re built with flexbox, so they’re easy to align and position.
Overview
Things to know when using the toast plugin:
- Toasts are opt-in for performance reasons, so you must initialize them yourself.
- Toasts will automatically hide if you do not specify
autohide: false
.
prefers-reduced-motion
media query. See the reduced motion section of our accessibility documentation.Examples
Basic
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.
Title
This is an informal toast. It shows you did something, but it's not very important.<div class="toast toast-primary" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header pe-2 align-items-start">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">info</i>
<div>
<h5 class="mb-0">Title</h5>
This is an informal toast. It shows you did something, but it's not very important.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close" data-bs-theme="dark">
</button>
</div>
</div>
.hide
class to completely hide a toast (with display:none
, rather than just with opacity:0
). This is now not necessary anymore. However, for backwards compatibility, our script will continue to toggle the class (even though there is no practical need for it) until the next major version.Live example
Click the button below to show a toast (positioned with our utilities in the lower right corner) that has been hidden by default.
Title
This is an informal toast. It shows you did something, but it's not very important.<button type="button" class="btn btn-primary" id="liveToastBtn">Show live toast</button>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="liveToast" class="toast toast-primary" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header pe-2 align-items-start">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">info</i>
<div>
<h4 class="mb-0">Title</h4>
This is a neutral toast. It shows that you did you did something okay.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
</div>
We use the following JavaScript to trigger our live toast demo:
const toastTrigger = document.getElementById('liveToastBtn')
const toastLiveExample = document.getElementById('liveToast')
if (toastTrigger) {
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
toastTrigger.addEventListener('click', () => {
toastBootstrap.show()
})
}
Stacking
You can stack toasts by wrapping them in a toast container, which will vertically add some spacing.
Title
This is an informal toast. It shows you did something, but it's not very important.Title
This is a error toast. It shows that you did you did something not very well.<div class="toast-container position-static">
<div class="toast toast-primary align-items-center" role="alert">
<div class="toast-header pe-2 align-items-start">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">info</i>
<div>
<h4 class="mb-0">Title</h4>
This is an informal toast. It shows you did something, but it's not very important.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
<div class="toast toast-danger show align-items-center mt-2" role="alert">
<div class="toast-header pe-2 align-items-start" data-bs-theme="dark">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">alert</i>
<div>
<h4 class="mb-0">Title</h4>
This is a error toast. It shows that you did you did something not very well.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
</div>
Color schemes
Building on the above example, you can create different toast color schemes with our color and background utilities. Here we’ve added .text-bg-primary
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
.
Title
This is an informal toast. It shows you did something, but it's not very important.Title
This is a neutral toast. It shows that you did you did something okay.Title
This is a success toast. It shows that you did you did something really well.Title
This is a error toast. It shows that you did you did something not very well.<div class="toast toast-primary align-items-center" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header pe-2 align-items-start">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">info</i>
<div>
<h4 class="mb-0">Title</h4>
This is an informal toast. It shows you did something, but it's not very important.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
<div class="toast toast-secondary show align-items-center mt-2" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header pe-2 align-items-start">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">info</i>
<div>
<h4 class="mb-0">Title</h4>
This is a neutral toast. It shows that you did you did something okay.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
<div class="toast toast-success show align-items-center mt-2" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header pe-2 align-items-start" data-bs-theme="dark">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">check_circle</i>
<div>
<h4 class="mb-0">Title</h4>
This is a success toast. It shows that you did you did something really well.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
<div class="toast toast-danger show align-items-center mt-2" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header pe-2 align-items-start" data-bs-theme="dark">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">alert</i>
<div>
<h4 class="mb-0">Title</h4>
This is a error toast. It shows that you did you did something not very well.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</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
.
Title
This is a error toast. It shows that you did you did something not very well.<form>
<div class="mb-3">
<label for="selectToastPlacement">Toast placement</label>
<select class="form-select mt-2" id="selectToastPlacement">
<option value="" selected disabled>Select a position...</option>
<option value="top-0 start-0">Top left</option>
<option value="top-0 start-50 translate-middle-x">Top center</option>
<option value="top-0 end-0">Top right</option>
<option value="top-50 start-0 translate-middle-y">Middle left</option>
<option value="top-50 start-50 translate-middle">Middle center</option>
<option value="top-50 end-0 translate-middle-y">Middle right</option>
<option value="bottom-0 start-0">Bottom left</option>
<option value="bottom-0 start-50 translate-middle-x">Bottom center</option>
<option value="bottom-0 end-0">Bottom right</option>
</select>
</div>
</form>
<div aria-live="polite" aria-atomic="true" class="bg-body-secondary position-relative bd-example-toasts rounded-3">
<div class="toast-container p-3" id="toastPlacement">
<div class="toast toast-danger">
<div class="toast-header pe-2 align-items-start" data-bs-theme="dark">
<i class="modus-icons me-2 align-items-top" aria-hidden="true">alert</i>
<div>
<h4 class="mb-0">Title</h4>
This is a error toast. It shows that you did you did something not very well.
</div>
<button type="button" class="btn-close me-0 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
</div>
</div>
For systems that generate more notifications, consider using a wrapping element so they can easily stack.
<div aria-live="polite" aria-atomic="true" class="position-relative">
<!-- Position it: -->
<!-- - `.toast-container` for spacing between toasts -->
<!-- - `top-0` & `end-0` to position the toasts in the upper right corner -->
<!-- - `.p-3` to prevent the toasts from sticking to the edge of the container -->
<div class="toast-container top-0 end-0 p-3">
<!-- Then put toasts within -->
<div class="toast toast-primary show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Modus Bootstrap</strong>
<small class="text-muted"></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 toast-primary show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Modus Bootstrap</strong>
<small class="text-muted"></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>
</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 toast-success" 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 role="alert" aria-live="assertive" aria-atomic="true" class="toast toast-danger" data-bs-autohide="false">
<div class="toast-header" data-bs-theme="dark">
<strong class="me-auto">Modus Bootstrap</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</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
.
CSS
Variables
As part of Bootstrap’s evolving CSS variables approach, toasts now use local CSS variables on .toast
for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
--#{$prefix}toast-zindex: #{$zindex-toast};
--#{$prefix}toast-padding-x: #{$toast-padding-x};
--#{$prefix}toast-padding-y: #{$toast-padding-y};
--#{$prefix}toast-spacing: #{$toast-spacing};
--#{$prefix}toast-max-width: #{$toast-max-width};
@include rfs($toast-font-size, --#{$prefix}toast-font-size);
--#{$prefix}toast-color: #{$toast-color};
--#{$prefix}toast-bg: #{$toast-background-color};
--#{$prefix}toast-border-width: #{$toast-border-width};
--#{$prefix}toast-border-color: #{$toast-border-color};
--#{$prefix}toast-border-radius: #{$toast-border-radius};
--#{$prefix}toast-box-shadow: #{$toast-box-shadow};
--#{$prefix}toast-header-color: #{$toast-header-color};
--#{$prefix}toast-header-bg: #{$toast-header-background-color};
--#{$prefix}toast-header-border-color: #{$toast-header-border-color};
Sass variables
$toast-max-width: 370px;
$toast-padding-x: 1rem; // Modus Customized
$toast-padding-y: .5rem;
$toast-font-size: .875rem;
$toast-color: #fff;
$toast-background-color: rgba(var(--#{$prefix}body-bg-rgb), .85);
$toast-border-width: var(--#{$prefix}border-width);
$toast-border-color: transparent;
$toast-border-radius: var(--#{$prefix}border-radius);
$toast-box-shadow: var(--#{$prefix}box-shadow);
$toast-spacing: $container-padding-x;
$toast-header-color: var(--#{$prefix}white); // Modus Customized
$toast-header-background-color: null;
$toast-header-border-color: null;
Usage
Initialize toasts via JavaScript:
const toastElList = document.querySelectorAll('.toast')
const toastList = [...toastElList].map(toastEl => new bootstrap.Toast(toastEl, option))
Triggers
Dismissal can be achieved with the data-bs-dismiss
attribute on a button within the toast as demonstrated below:
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
or on a button outside the toast using the additional data-bs-target
as demonstrated below:
<button type="button" class="btn-close" data-bs-dismiss="toast" data-bs-target="#my-toast" aria-label="Close"></button>
Options
As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-
, as in data-bs-animation="{value}"
. Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier"
instead of data-bs-customClass="beautifier"
.
As of Bootstrap 5.2.0, all components support an experimental reserved data attribute data-bs-config
that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}'
and data-bs-title="456"
attributes, the final title
value will be 456
and the separate data attributes will override values given on data-bs-config
. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'
.
The final configuration object is the merged result of data-bs-config
, data-bs-
, and js object
where the latest given key-value overrides the others.
Name | Type | Default | Description |
---|---|---|---|
animation |
boolean | true |
Apply a CSS fade transition to the toast. |
autohide |
boolean | true |
Automatically hide the toast after the delay. |
delay |
number | 5000 |
Delay in milliseconds before hiding the toast. |
Methods
Method | Description |
---|---|
dispose |
Hides an element’s toast. Your toast will remain on the DOM but won’t show anymore. |
getInstance |
Static method which allows you to get the toast instance associated with a DOM element. For example: const myToastEl = document.getElementById('myToastEl') const myToast = bootstrap.Toast.getInstance(myToastEl) Returns a Bootstrap toast instance. |
getOrCreateInstance |
Static method which allows you to get the toast instance associated with a DOM element, or create a new one, in case it wasn’t initialized. const myToastEl = document.getElementById('myToastEl') const myToast = bootstrap.Toast.getOrCreateInstance(myToastEl) Returns a Bootstrap toast instance. |
hide |
Hides an element’s toast. Returns to the caller before the toast has actually been hidden (i.e. before the hidden.bs.toast event occurs). You have to manually call this method if you made autohide to false . |
isShown |
Returns a boolean according to toast’s visibility state. |
show |
Reveals an element’s toast. Returns to the caller before the toast has actually been shown (i.e. before the shown.bs.toast event occurs). You have to manually call this method, instead your toast won’t show. |
Events
Event | Description |
---|---|
hide.bs.toast |
This event is fired immediately when the hide instance method has been called. |
hidden.bs.toast |
This event is fired when the toast has finished being hidden from the user. |
show.bs.toast |
This event fires immediately when the show instance method is called. |
shown.bs.toast |
This event is fired when the toast has been made visible to the user. |
const myToastEl = document.getElementById('myToast')
myToastEl.addEventListener('hidden.bs.toast', () => {
// do something...
})