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 .

The animation effect of this component is dependent on the prefers-reduced-motion media query. See the reduced motion section of our accessibility documentation.

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.

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 me-2" alt="...">
  4. <strong class="me-auto">Bootstrap</strong>
  5. <small class="text-muted">11 mins ago</small>
  6. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  7. </div>
  8. <div class="toast-body">
  9. Hello, world! This is a toast message.
  10. </div>
  11. </div>

Stacking

You can stack toasts by wrapping them in a toast container, which will vertically add some spacing.

  1. <div class="toast-container">
  2. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  3. <div class="toast-header">
  4. <img src="..." class="rounded me-2" alt="...">
  5. <strong class="me-auto">Bootstrap</strong>
  6. <small class="text-muted">just now</small>
  7. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  8. </div>
  9. <div class="toast-body">
  10. See? Just like this.
  11. </div>
  12. </div>
  13. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  14. <div class="toast-header">
  15. <img src="..." class="rounded me-2" alt="...">
  16. <strong class="me-auto">Bootstrap</strong>
  17. <small class="text-muted">2 seconds ago</small>
  18. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  19. </div>
  20. <div class="toast-body">
  21. Heads up, toasts will stack automatically
  22. </div>
  23. </div>

Customize your toasts by removing sub-components, tweaking with , or adding your own markup. Here we’ve created a simpler toast by removing the default .toast-header, adding a custom hide icon from Bootstrap Icons, and using some to adjust the layout.

  1. <div class="toast d-flex align-items-center" role="alert" aria-live="assertive" aria-atomic="true">
  2. <div class="toast-body">
  3. Hello, world! This is a toast message.
  4. </div>
  5. <button type="button" class="btn-close ms-auto me-2" data-bs-dismiss="toast" aria-label="Close"></button>
  6. </div>

Alternatively, you can also add additional controls and components to toasts.

Toasts - 图5

  1. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  2. <div class="toast-body">
  3. Hello, world! This is a toast message.
  4. <div class="mt-2 pt-2 border-top">
  5. <button type="button" class="btn btn-primary btn-sm">Take action</button>
  6. <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="toast">Close</button>
  7. </div>
  8. </div>
  9. </div>

Color schemes

Building on the above example, you can create different toast color schemes with our . Here we’ve added .bg-primary and .text-white to the .toast, and then added to our close button. For a crisp edge, we remove the default border with .border-0.

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 - 图7

  1. <form>
  2. <div class="form-group mb-3">
  3. <label for="selectToastPlacement">Toast placement</label>
  4. <select class="form-select mt-2" id="selectToastPlacement">
  5. <option value="" selected>Select a position...</option>
  6. <option value="top-0 start-0">Top left</option>
  7. <option value="top-0 start-50 translate-middle-x">Top center</option>
  8. <option value="top-0 end-0">Top right</option>
  9. <option value="top-50 start-0 translate-middle-y">Middle left</option>
  10. <option value="top-50 start-50 translate-middle">Middle center</option>
  11. <option value="top-50 end-0 translate-middle-y">Middle right</option>
  12. <option value="bottom-0 start-0">Bottom left</option>
  13. <option value="bottom-0 start-50 translate-middle-x">Bottom center</option>
  14. <option value="bottom-0 end-0">Bottom right</option>
  15. </select>
  16. </div>
  17. </form>
  18. <div aria-live="polite" aria-atomic="true" class="bg-dark position-relative bd-example-toasts">
  19. <div class="toast-container position-absolute p-3" id="toastPlacement">
  20. <div class="toast">
  21. <div class="toast-header">
  22. <img src="..." class="rounded me-2" alt="...">
  23. <strong class="me-auto">Bootstrap</strong>
  24. <small>11 mins ago</small>
  25. </div>
  26. <div class="toast-body">
  27. Hello, world! This is a toast message.
  28. </div>
  29. </div>
  30. </div>
  31. </div>

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

  1. <div aria-live="polite" aria-atomic="true" class="position-relative">
  2. <!-- Position it: -->
  3. <!-- - `.toast-container` for spacing between toasts -->
  4. <!-- - `.position-absolute`, `top-0` & `end-0` to position the toasts in the upper right corner -->
  5. <div class="toast-container position-absolute top-0 end-0 p-3">
  6. <!-- Then put toasts within -->
  7. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  8. <div class="toast-header">
  9. <img src="..." class="rounded me-2" alt="...">
  10. <strong class="me-auto">Bootstrap</strong>
  11. <small class="text-muted">just now</small>
  12. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></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 me-2" alt="...">
  21. <strong class="me-auto">Bootstrap</strong>
  22. <small class="text-muted">2 seconds ago</small>
  23. </div>
  24. <div class="toast-body">
  25. Heads up, toasts will stack automatically
  26. </div>
  27. </div>
  28. </div>
  29. </div>

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

Toasts - 图9

  1. <!-- Flexbox container for aligning the toasts -->
  2. <div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center w-100">
  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 me-2" alt="...">
  7. <strong class="me-auto">Bootstrap</strong>
  8. <small>11 mins ago</small>
  9. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  10. </div>
  11. <div class="toast-body">
  12. Hello, world! This is a toast message.
  13. </div>
  14. </div>
  15. </div>

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-bs-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.

Usage

Initialize toasts via JavaScript:

  1. var toastElList = [].slice.call(document.querySelectorAll('.toast'))
  2. var toastList = toastElList.map(function (toastEl) {
  3. return new bootstrap.Toast(toastEl, option)
  4. })

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

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.

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. toast.show()

hide

    dispose

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

    1. 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.
    This event is fired when the toast has finished being hidden from the user.