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.
Overview
Things to know when using the toast plugin:
- If you’re building our JavaScript from source, it requires util.js.
- Toasts are opt-in for performance reasons, so you must initialize them yourself.
- Please note that you are responsible for positioning toasts.
- Toasts will automatically hide if you do not specify
autohide: false
.
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.
John Doe
11 mins ago
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="img/avatar-1.jpg" class="avatar rounded-circle mr-md-2" alt="Image description">
<h6 class="font-weight-semi-bold mb-0">John Doe</h6>
<small class="text-muted ml-auto">11 mins ago</small>
<button type="button" class="ml-4 close" data-dismiss="toast" aria-label="Close">
<i class="gd-close small" aria-hidden="true"></i>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
Stacking
When you have multiple toasts, we default to vertically stacking them in a readable manner.
John Doe
just nowJohn Doe
2 seconds ago
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="img/avatar-1.jpg" class="avatar rounded-circle mr-md-2" alt="Image description">
<h6 class="font-weight-semi-bold mb-0">John Doe</h6>
<small class="text-muted ml-auto">just now</small>
<button type="button" class="ml-4 close" data-dismiss="toast" aria-label="Close">
<i class="gd-close small" aria-hidden="true"></i>
</button>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="img/avatar-1.jpg" class="avatar rounded-circle mr-md-2" alt="Image description">
<h6 class="font-weight-semi-bold mb-0">John Doe</h6>
<small class="text-muted ml-auto">2 seconds ago</small>
<button type="button" class="ml-4 close" data-dismiss="toast" aria-label="Close">
<i class="gd-close small" aria-hidden="true"></i>
</button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</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
.
John Doe
13 mins ago
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
<div class="toast" style="position: absolute; top: 0; right: 0;">
<div class="toast-header">
<img src="img/avatar-1.jpg" class="avatar rounded-circle mr-md-2" alt="Image description">
<h6 class="font-weight-semi-bold mb-0">John Doe</h6>
<small class="text-muted ml-auto">13 mins ago</small>
<button type="button" class="ml-4 close" data-dismiss="toast" aria-label="Close">
<i class="gd-close small" aria-hidden="true"></i>
</button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</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 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 to ensure people have enough time to read the toast.
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-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.
John Doe
11 mins ago
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
<div class="toast-header">
<img src="img/avatar-1.jpg" class="avatar rounded-circle mr-md-2" alt="Image description">
<h6 class="font-weight-semi-bold mb-0">John Doe</h6>
<small class="text-muted ml-auto">11 mins ago</small>
<button type="button" class="ml-4 close" data-dismiss="toast" aria-label="Close">
<i class="gd-close small" aria-hidden="true"></i>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>