Push notifications to users 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.
Toasts are recommended to be built with a header and body. Headers use
display: flex, allowing easy alignment of content.
Toasts are as flexible as you need and have very little required markup. They are slightly translucent as to blend over whatever they might appear over. Browsers that support the
backdrop-filter CSS property also blur the elements under a toast.
Multiple toasts will stack vertically by default. Toasts are often positioned on the top right of a page, which can be done using wrappers and positioning as seen in the example below.
<div class="bg-dark position-relative" style="min-height:300px;"> <div class="position-absolute m-3" style="top:0;right:0;"> <div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false"> <div class="toast-header text-success"> <i class="fas fa-check mr-2"></i> <strong class="mr-auto">Well done</strong> <small class="text-muted">11 mins ago</small> <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="toast-body"> You successfully read this important alert message. </div> </div> <div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true" data-autohide="false"> <div class="toast-header text-danger"> <i class="fas fa-exclamation mr-2"></i> <strong class="mr-auto">Error</strong> <small class="text-muted">11 mins ago</small> <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="toast-body"> Change a few things up and try submitting again. </div> </div> </div> </div>
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
|animation||boolean||true||Apply a CSS fade transition to the toast.|
|autohide||boolean||true||Auto hide the toast.|
|delay||number||500||Delay hiding the toast (ms).|
||Reveals an element’s toast. Returns to the caller before the toast has actually been shown (i.e. before the
||Hides an element’s toast. Returns to the caller before the toast has actually been hidden (i.e. before the
||Hides an element’s toast. Your toast will remain on the DOM but won’t show anymore.|