Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.
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.
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
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.
Toasts are slightly translucent, too, so they blend over whatever they might appear over. For browsers that support the
backdrop-filter CSS property, we’ll also attempt to blur the elements under a toast.
When you have multiple toasts, we default to vertiaclly stacking them in a readable manner.
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
For systems that generate more notifications, consider using a wrapping element so they can easily stack.
You can also get fancy with flexbox utilities to align toasts horizontally and/or vertically.
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 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
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 to ensure people have enough time to read the toast.
autohide: false, you must add a close button to allow users to dismiss the toast.
data-, as in
|Apply a CSS fade transition to the toast
|Auto hide the toast
|Delay hiding the toast (ms)
Asynchronous methods and transitions
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.
Attaches a toast handler to an element collection.
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.
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
Hides an element’s toast. Your toast will remain on the DOM but won’t show anymore.
|This event fires immediately when the
show instance method is called.
|This event is fired when the toast has been made visible to the user.
|This event is fired immediately when the
hide instance method has been called.
|This event is fired when the toast has finished being hidden from the user.