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:

  • Toasts are opt-in for performance reasons, so you must initialize them yourself.
  • 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

html

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

Previously, our scripts dynamically added the .hide class to completely hide a toast (with display:none, rather than just with opacity:0). This is now not necessary anymore. However, for backwards compatibility, our script will continue to toggle the class (even though there is no practical need for it) until the next major version.

Live example

Click the button below to show a toast (positioned with our utilities in the lower right corner) that has been hidden by default.

Toasts - 图2 Bootstrap 11 mins ago

Hello, world! This is a toast message.

Toasts - 图3

  1. <button type="button" class="btn btn-primary" id="liveToastBtn">Show live toast</button>
  2. <div class="toast-container position-fixed bottom-0 end-0 p-3">
  3. <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  4. <div class="toast-header">
  5. <img src="..." class="rounded me-2" alt="...">
  6. <strong class="me-auto">Bootstrap</strong>
  7. <small>11 mins ago</small>
  8. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  9. </div>
  10. <div class="toast-body">
  11. Hello, world! This is a toast message.
  12. </div>
  13. </div>
  14. </div>

We use the following JavaScript to trigger our live toast demo:

site/assets/js/snippets.js

  1. const toastTrigger = document.getElementById('liveToastBtn')
  2. const toastLiveExample = document.getElementById('liveToast')
  3. if (toastTrigger) {
  4. const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
  5. toastTrigger.addEventListener('click', () => {
  6. toastBootstrap.show()
  7. })
  8. }

Translucent

Toasts are slightly translucent to blend in with what’s below them.

Toasts - 图4

html

  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-body-secondary">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.

Toasts - 图5

html

  1. <div class="toast-container position-static">
  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-body-secondary">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-body-secondary">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>
  24. </div>

Custom content

Customize your toasts by removing sub-components, tweaking them with utilities, or by 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 flexbox utilities to adjust the layout.

Toasts - 图6

html

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

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

Toasts - 图7

html

  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 color and background utilities. Here we’ve added .text-bg-primary to the .toast, and then added .btn-close-white to our close button. For a crisp edge, we remove the default border with .border-0.

Toasts - 图8

html

  1. <div class="toast align-items-center text-bg-primary border-0" role="alert" aria-live="assertive" aria-atomic="true">
  2. <div class="d-flex">
  3. <div class="toast-body">
  4. Hello, world! This is a toast message.
  5. </div>
  6. <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
  7. </div>
  8. </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 - 图9

html

  1. <form>
  2. <div class="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-body-secondary position-relative bd-example-toasts rounded-3">
  19. <div class="toast-container 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.

Toasts - 图10

html

  1. <div aria-live="polite" aria-atomic="true" class="position-relative">
  2. <!-- Position it: -->
  3. <!-- - `.toast-container` for spacing between toasts -->
  4. <!-- - `top-0` & `end-0` to position the toasts in the upper right corner -->
  5. <!-- - `.p-3` to prevent the toasts from sticking to the edge of the container -->
  6. <div class="toast-container top-0 end-0 p-3">
  7. <!-- Then put toasts within -->
  8. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  9. <div class="toast-header">
  10. <img src="..." class="rounded me-2" alt="...">
  11. <strong class="me-auto">Bootstrap</strong>
  12. <small class="text-body-secondary">just now</small>
  13. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  14. </div>
  15. <div class="toast-body">
  16. See? Just like this.
  17. </div>
  18. </div>
  19. <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  20. <div class="toast-header">
  21. <img src="..." class="rounded me-2" alt="...">
  22. <strong class="me-auto">Bootstrap</strong>
  23. <small class="text-body-secondary">2 seconds ago</small>
  24. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  25. </div>
  26. <div class="toast-body">
  27. Heads up, toasts will stack automatically
  28. </div>
  29. </div>
  30. </div>
  31. </div>

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

Toasts - 图11

html

  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>

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 just 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 so that users 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.

Toasts - 图12

html

  1. <div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-bs-autohide="false">
  2. <div class="toast-header">
  3. <img src="..." class="rounded me-2" alt="...">
  4. <strong class="me-auto">Bootstrap</strong>
  5. <small>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>

While technically it’s possible to add focusable/actionable controls (such as additional buttons or links) in your toast, you should avoid doing this for autohiding toasts. Even if you give the toast a long delay timeout, keyboard and assistive technology users may find it difficult to reach the toast in time to take action (since toasts don’t receive focus when they are displayed). If you absolutely must have further controls, we recommend using a toast with autohide: false.

CSS

Variables

Added in v5.2.0

As part of Bootstrap’s evolving CSS variables approach, toasts now use local CSS variables on .toast for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

scss/_toasts.scss

  1. --#{$prefix}toast-zindex: #{$zindex-toast};
  2. --#{$prefix}toast-padding-x: #{$toast-padding-x};
  3. --#{$prefix}toast-padding-y: #{$toast-padding-y};
  4. --#{$prefix}toast-spacing: #{$toast-spacing};
  5. --#{$prefix}toast-max-width: #{$toast-max-width};
  6. @include rfs($toast-font-size, --#{$prefix}toast-font-size);
  7. --#{$prefix}toast-color: #{$toast-color};
  8. --#{$prefix}toast-bg: #{$toast-background-color};
  9. --#{$prefix}toast-border-width: #{$toast-border-width};
  10. --#{$prefix}toast-border-color: #{$toast-border-color};
  11. --#{$prefix}toast-border-radius: #{$toast-border-radius};
  12. --#{$prefix}toast-box-shadow: #{$toast-box-shadow};
  13. --#{$prefix}toast-header-color: #{$toast-header-color};
  14. --#{$prefix}toast-header-bg: #{$toast-header-background-color};
  15. --#{$prefix}toast-header-border-color: #{$toast-header-border-color};

Sass variables

scss/_variables.scss

  1. $toast-max-width: 350px;
  2. $toast-padding-x: .75rem;
  3. $toast-padding-y: .5rem;
  4. $toast-font-size: .875rem;
  5. $toast-color: null;
  6. $toast-background-color: rgba(var(--#{$prefix}body-bg-rgb), .85);
  7. $toast-border-width: var(--#{$prefix}border-width);
  8. $toast-border-color: var(--#{$prefix}border-color-translucent);
  9. $toast-border-radius: var(--#{$prefix}border-radius);
  10. $toast-box-shadow: var(--#{$prefix}box-shadow);
  11. $toast-spacing: $container-padding-x;
  12. $toast-header-color: var(--#{$prefix}secondary-color);
  13. $toast-header-background-color: rgba(var(--#{$prefix}body-bg-rgb), .85);
  14. $toast-header-border-color: $toast-border-color;

Usage

Initialize toasts via JavaScript:

  1. const toastElList = document.querySelectorAll('.toast')
  2. const toastList = [...toastElList].map(toastEl => new bootstrap.Toast(toastEl, option))

Triggers

Dismissal can be achieved with the data-bs-dismiss attribute on a button within the toast as demonstrated below:

  1. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>

or on a button outside the toast using the additional data-bs-target as demonstrated below:

  1. <button type="button" class="btn-close" data-bs-dismiss="toast" data-bs-target="#my-toast" aria-label="Close"></button>

Options

As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-, as in data-bs-animation="{value}". Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier" instead of data-bs-customClass="beautifier".

As of Bootstrap 5.2.0, all components support an experimental reserved data attribute data-bs-config that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}' and data-bs-title="456" attributes, the final title value will be 456 and the separate data attributes will override values given on data-bs-config. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'.

The final configuration object is the merged result of data-bs-config, data-bs-, and js object where the latest given key-value overrides the others.

NameTypeDefaultDescription
animationbooleantrueApply a CSS fade transition to the toast.
autohidebooleantrueAutomatically hide the toast after the delay.
delaynumber5000Delay in milliseconds before hiding the toast.

Methods

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. Learn more in our JavaScript docs.

MethodDescription
disposeHides an element’s toast. Your toast will remain on the DOM but won’t show anymore.
getInstanceStatic method which allows you to get the toast instance associated with a DOM element.
For example: const myToastEl = document.getElementById(‘myToastEl’) const myToast = bootstrap.Toast.getInstance(myToastEl) Returns a Bootstrap toast instance.
getOrCreateInstanceStatic method which allows you to get the toast instance associated with a DOM element, or create a new one, in case it wasn’t initialized.
const myToastEl = document.getElementById(‘myToastEl’) const myToast = bootstrap.Toast.getOrCreateInstance(myToastEl) Returns a Bootstrap toast instance.
hideHides 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.
isShownReturns a boolean according to toast’s visibility state.
showReveals 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.

Events

EventDescription
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.
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.
  1. const myToastEl = document.getElementById('myToast')
  2. myToastEl.addEventListener('hidden.bs.toast', () => {
  3. // do something...
  4. })