Toast

Toasts display brief, temporary notifications for users on the outcome of an action.

Bootstrap 5 Toasts component

Responsive toast built with the latest Bootstrap 5. Toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems.

Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.

Basic toast

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.

<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast-header">
    <img src="..." class="rounded me-2" alt="...">
    <strong class="me-auto">Bootstrap</strong>
    <small>11 mins ago</small>
    <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>

Live example

Clicking the button below. The toast will be displayed in the bottom-left of the page.

<button type="button" class="btn btn-primary" id="liveToastBtn">Show toast</button>
<div class="toast-container position-fixed bottom-0 start-0 p-6">
  <div class="toast" id="liveToast" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span><i class="fa-solid fa-circle-check fa-lg icon-success"></i></span>
        <div class="d-flex flex-column flex-grow-1 gap-2">
          <div class="d-flex align-items-center">
            <span class="fw-semibold">#1 Your changes were saved</span>
            <button type="button" class="btn-close btn-close-sm ms-auto" data-bs-dismiss="toast"
              aria-label="Close"></button>
          </div>
          <span>I will auto dismiss after 8 seconds.</span>
        </div>
      </div>
    </div>
  </div>
</div>

Toasts are opt-in for performance reasons, so you must initialize toast yourself via js new bootstrap.Toast().

JS

  const toastTrigger = document.getElementById("liveToastBtn");
  const toastLiveExample = document.getElementById("liveToast");
  toastTrigger.addEventListener("click", () => {
    const toast = new bootstrap.Toast(toastLiveExample);
    toast.show();
  });

Color schemes

For fancy style? you can create different toast color schemes with our color and background utilities, likes .text-bg-{color} to the .toast, and then added .btn-close-white to our close button.

<div class="toast text-bg-primary" role="alert">
  <div class="toast-body">
    <div class="d-flex gap-4">
      <span><i class="fa-solid fa-circle-check fa-lg"></i></span>
      <div class="d-flex flex-grow-1 align-items-center">
        <span class="fw-semibold">Welcome to the room</span>
        <button type="button" 
        class="btn-close btn-close-white btn-close-sm ms-auto" 
        data-bs-dismiss="toast" 
        aria-label="Close"></button>
      </div>
    </div>
  </div>
</div>

Success example

Adding .text-bg-success to the .toast, and then added .btn-close-white to our close button.

<div class="toast text-bg-success" role="alert">
  <div class="toast-body">
    <div class="d-flex gap-4">
      <span><i class="fa-solid fa-circle-check fa-lg"></i></span>
      <div class="d-flex flex-grow-1 align-items-center">
        <span class="fw-semibold">Welcome to the room</span>
        ...
      </div>
    </div>
  </div>
</div>

Error example

Adding .text-bg-danger to the .toast.

<div class="toast text-bg-danger" role="alert">
  <div class="toast-body">
    <div class="d-flex gap-4">
      <span><i class="fa-solid fa-circle-exclamation fa-lg"></i></span>
      <div class="d-flex flex-grow-1 align-items-center">
        <span class="fw-semibold">We are having issues</span>
        ...
      </div>
    </div>
  </div>
</div>

Warning example

Adding .text-bg-warning to the .toast.

<div class="toast text-bg-warning" role="alert">
  <div class="toast-body">
    <div class="d-flex gap-4">
      <span><i class="fa-solid fa-triangle-exclamation fa-lg"></i></span>
      <div class="d-flex flex-grow-1 align-items-center">
        <span class="fw-semibold">Trying again...</span>
        ...
      </div>
    </div>
  </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. Check out our position utilities for more information.

Top-Left

use the position-fixed, top-0 and start-0 utility classes on a .toast-container element to position Toasts at the top left of the viewport.

<div class="toast-container top-0 start-0 p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Top-Center

use the position-fixed, top-0, start-50 and translate-middle-x utility classes on a .toast-container element to position Toasts at the top center of the viewport.

<div class="toast-container top-0 start-50 translate-middle-x p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Top-Right

use the position-fixed, top-0, end-0 utility classes on a .toast-container element to position Toasts at the top right of the viewport.

<div class="toast-container top-0 end-0 p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Bottom-Left

use the position-fixed, bottom-0, and start-0 utility classes on a .toast-container element to position Toasts at the bottom left of the viewport.

<div class="toast-container bottom-0 start-0 p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Bottom-Center

use the position-fixed, bottom-0, start-50 and translate-middle-x utility classes on a .toast-container element to position Toasts at the bottom left of the viewport.

<div class="toast-container bottom-0 start-50 translate-middle-x p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Bottom-Right

use the position-fixed, bottom-0, and end-0 utility classes on a .toast-container element to position Toasts at the bottom left of the viewport.

<div class="toast-container bottom-0 end-0 p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Middle-Left

use the position-fixed, top-50, start-0 and translate-middle-y utility classes on a .toast-container element to position Toasts at the middle left of the viewport.

<div class="toast-container top-50 start-0 translate-middle-y p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Middle-Right

use the position-fixed, top-50, end-0 and translate-middle-y utility classes on a .toast-container element to position Toasts at the middle right of the viewport.

<div class="toast-container top-50 end-0 translate-middle-y p-3">
  <div class="toast show fade" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-body">
      <div class="d-flex gap-4">
        <span class="text-primary"><i class="fa-solid fa-circle-info fa-lg"></i></span>
        <div class="d-flex flex-grow-1 align-items-center">
          <span class="fw-semibold">Hello, world! This is a toast message.</span>
          <button type="button" class="btn-close btn-close-sm btn-close-black ms-auto" data-bs-dismiss="toast"
            aria-label="Close"></button>
        </div>
      </div>
    </div>
  </div>
</div>

Stack toasts

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

<div class="toast-container position-static">
  <div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-header">
      <i class="fa-solid fa-circle-info fa-lg text-primary me-2"></i>
      <strong class="me-auto">Bootstrap</strong>
      <small class="text-body-secondary">just now</small>
      <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
    </div>
    <div class="toast-body">
      See? Just like this.
    </div>
  </div>
  <div class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-header">
      <i class="fa-solid fa-circle-info fa-lg text-primary me-2"></i>
      <strong class="me-auto">Bootstrap</strong>
      <small class="text-body-secondary">2 seconds ago</small>
      <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
    </div>
    <div class="toast-body">
      Heads up, toasts will stack automatically
    </div>
  </div>
</div>

Custom toast

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.

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

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

<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast-body">
    Hello, world! This is a toast message.
    <div class="mt-2 pt-2 border-top">
      <button type="button" class="btn btn-primary btn-sm">Take action</button>
      <button type="button" class="btn btn-default btn-sm" data-bs-dismiss="toast">Close</button>
    </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 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.

<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-bs-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.

<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-bs-autohide="false">
  <div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</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.

Usage

Initialize

Initialize toasts via JavaScript:

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

Dismiss button

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

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

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

<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}'.

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

Methods

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