Toasts

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.

The animation effect of this component is dependent on the 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.

Toasts - 图1

  1. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  2. <div class="toast-header">
  3. <img src="..." class="rounded mr-2" alt="...">
  4. <strong class="mr-auto">Bootstrap</strong>
  5. <small>11 mins ago</small>
  6. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  7. <span aria-hidden="true">&times;</span>
  8. </button>
  9. </div>
  10. <div class="toast-body">
  11. Hello, world! This is a toast message.
  12. </div>
  13. </div>

Translucent

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.

Toasts - 图2

  1. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  2. <div class="toast-header">
  3. <img src="..." class="rounded mr-2" alt="...">
  4. <strong class="mr-auto">Bootstrap</strong>
  5. <small class="text-muted">11 mins ago</small>
  6. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  7. <span aria-hidden="true">&times;</span>
  8. </button>
  9. </div>
  10. <div class="toast-body">
  11. Hello, world! This is a toast message.
  12. </div>
  13. </div>

Stacking

When you have multiple toasts, we default to vertically stacking them in a readable manner.

Toasts - 图3

  1. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  2. <div class="toast-header">
  3. <img src="..." class="rounded mr-2" alt="...">
  4. <strong class="mr-auto">Bootstrap</strong>
  5. <small class="text-muted">just now</small>
  6. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  7. <span aria-hidden="true">&times;</span>
  8. </button>
  9. </div>
  10. <div class="toast-body">
  11. See? Just like this.
  12. </div>
  13. </div>
  14. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  15. <div class="toast-header">
  16. <img src="..." class="rounded mr-2" alt="...">
  17. <strong class="mr-auto">Bootstrap</strong>
  18. <small class="text-muted">2 seconds ago</small>
  19. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  20. <span aria-hidden="true">&times;</span>
  21. </button>
  22. </div>
  23. <div class="toast-body">
  24. Heads up, toasts will stack automatically
  25. </div>
  26. </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.

Toasts - 图4

  1. <div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
  2. <div class="toast" style="position: absolute; top: 0; right: 0;">
  3. <div class="toast-header">
  4. <img src="..." class="rounded mr-2" alt="...">
  5. <strong class="mr-auto">Bootstrap</strong>
  6. <small>11 mins ago</small>
  7. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  8. <span aria-hidden="true">&times;</span>
  9. </button>
  10. </div>
  11. <div class="toast-body">
  12. Hello, world! This is a toast message.
  13. </div>
  14. </div>
  15. </div>

For systems that generate more notifications, consider using a wrapping element so they can easily stack.

Toasts - 图5

  1. <div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
  2. <!-- Position it -->
  3. <div style="position: absolute; top: 0; right: 0;">
  4. <!-- Then put toasts within -->
  5. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  6. <div class="toast-header">
  7. <img src="..." class="rounded mr-2" alt="...">
  8. <strong class="mr-auto">Bootstrap</strong>
  9. <small class="text-muted">just now</small>
  10. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  11. <span aria-hidden="true">&times;</span>
  12. </button>
  13. </div>
  14. <div class="toast-body">
  15. See? Just like this.
  16. </div>
  17. </div>
  18. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  19. <div class="toast-header">
  20. <img src="..." class="rounded mr-2" alt="...">
  21. <strong class="mr-auto">Bootstrap</strong>
  22. <small class="text-muted">2 seconds ago</small>
  23. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  24. <span aria-hidden="true">&times;</span>
  25. </button>
  26. </div>
  27. <div class="toast-body">
  28. Heads up, toasts will stack automatically
  29. </div>
  30. </div>
  31. </div>
  32. </div>

You can also get fancy with flexbox utilities to align toasts horizontally and/or vertically.

Toasts - 图6

  1. <!-- Flexbox container for aligning the toasts -->
  2. <div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center" style="min-height: 200px;">
  3. <!-- Then put toasts within -->
  4. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  5. <div class="toast-header">
  6. <img src="..." class="rounded mr-2" alt="...">
  7. <strong class="mr-auto">Bootstrap</strong>
  8. <small>11 mins ago</small>
  9. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  10. <span aria-hidden="true">&times;</span>
  11. </button>
  12. </div>
  13. <div class="toast-body">
  14. Hello, world! This is a toast message.
  15. </div>
  16. </div>
  17. </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.

  1. <div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-delay="10000">
  2. <div role="alert" aria-live="assertive" aria-atomic="true">...</div>
  3. </div>

When using autohide: false, you must add a close button to allow users to dismiss the toast.

Toasts - 图7

  1. <div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
  2. <div class="toast-header">
  3. <img src="..." class="rounded mr-2" alt="...">
  4. <strong class="mr-auto">Bootstrap</strong>
  5. <small>11 mins ago</small>
  6. <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
  7. <span aria-hidden="true">&times;</span>
  8. </button>
  9. </div>
  10. <div class="toast-body">
  11. Hello, world! This is a toast message.
  12. </div>
  13. </div>

JavaScript behavior

Usage

Initialize toasts via JavaScript:

  1. $('.toast').toast(option)

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-animation="".

NameTypeDefaultDescription
animationbooleantrueApply a CSS fade transition to the toast
autohidebooleantrueAuto hide the toast
delaynumber 500 Delay hiding the toast (ms)

Methods

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.

See our JavaScript documentation for more information.

$().toast(options)

Attaches a toast handler to an element collection.

.toast('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.

  1. $('#element').toast('show')

.toast('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.

  1. $('#element').toast('hide')

.toast('dispose')

Hides an element’s toast. Your toast will remain on the DOM but won’t show anymore.

  1. $('#element').toast('dispose')

Events

Event TypeDescription
show.bs.toastThis event fires immediately when the show instance method is called.
shown.bs.toastThis event is fired when the toast has been made visible to the user.
hide.bs.toastThis event is fired immediately when the hide instance method has been called.
hidden.bs.toastThis event is fired when the toast has finished being hidden from the user.
  1. $('#myToast').on('hidden.bs.toast', function () {
  2. // do something...
  3. })