Kuliga Poster
← Back to all posts

Bootstrap 5 Toasts: Stacking, Autohide, and Undo

September 10, 2026

Bootstrap 5 Toasts: Stacking, Autohide, and Undo

Bootstrap 5 Toasts: Stacking, Autohide, and Undo

The docs example for a Bootstrap 5 toast is one <div class="toast"> sitting in the markup, shown and hidden by hand. That's enough to learn the API, but it's not how toasts actually get used — real apps fire them from JavaScript in response to events, several can be on screen at once, and some need to wait for the user instead of disappearing on a timer.

Here's how to get from the docs example to that.

Option 1: One toast, shown and hidden

The baseline. A toast lives in the markup, wrapped in a .toast-container that pins it to a corner of the viewport, and bootstrap.Toast controls it.

HTML
<div class="toast-container position-fixed bottom-0 end-0 p-3">
  <div id="basicToast" class="toast" role="status" aria-live="polite" aria-atomic="true">
    <div class="toast-header">
      <strong class="me-auto">Saved</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">Your changes have been saved.</div>
  </div>
</div>
Live demo — click to show, then dismiss it

By default a toast autohides after 5 seconds and dismisses on close-button click. Fine for a single, known-ahead-of-time toast — but nothing here explains what happens when an event fires three times in a row.

Option 2: Creating toasts on the fly

Real notifications come from code, not markup — a save completed, a message arrived, a background job finished. Instead of pre-declaring a toast, build the element when the event happens and append it to the container.

JAVASCRIPT
const container = document.getElementById('toastContainer');

function pushToast(message) {
  const el = document.createElement('div');
  el.className = 'toast';
  el.role = 'status';
  el.ariaLive = 'polite';
  el.ariaAtomic = 'true';
  el.innerHTML = `
    <div class="toast-header">
      <strong class="me-auto">Notification</strong>
      <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
    </div>
    <div class="toast-body">${message}</div>
  `;
  container.appendChild(el);

  const toast = new bootstrap.Toast(el, { delay: 2500 });

  // Bootstrap only hides the toast, it doesn't remove the element —
  // without this the container quietly fills up with dead nodes.
  el.addEventListener('hidden.bs.toast', () => el.remove());

  toast.show();
}

.toast-container is a flex column, so every toast appended to it stacks above the last automatically — no manual positioning needed. Click the button below a few times quickly to see them pile up and clear themselves out.

Live demo — click a few times fast

Option 3: Toasts that wait for you

Not every toast should vanish on a timer. An "item deleted, undo?" message that autohides after 5 seconds isn't actually offering an undo — it's just announcing the delete and hoping you're fast. Set autohide: false and give the user a real way to dismiss it.

JAVASCRIPT
const el = /* ...same toast markup, plus an Undo button in .toast-body... */;
container.appendChild(el);

// autohide: false — this toast waits for the user, not a timer.
const toast = new bootstrap.Toast(el, { autohide: false });
el.addEventListener('hidden.bs.toast', () => el.remove());

el.querySelector('#undoBtn').addEventListener('click', () => {
  toast.hide();
});

toast.show();
Live demo — click Delete, then Undo (this toast waits for you)

Which one should you use?

SituationUse
A single, known toast on the pageStatic markup + .show()
Notifications fired from events, any numberCreate elements on the fly
The toast needs a decision, not just a lookautohide: false + own close

Two gotchas

Bootstrap hides, it doesn't clean up. toast.hide() sets display: none (via a class) and fires hidden.bs.toast — the element stays in the DOM. For toasts you create dynamically, always remove the element in that event handler, or the container silently accumulates nodes for the life of the page.

role and aria-live matter more than they look. A toast is visual — nothing tells a screen reader it appeared unless the live region attributes are there. Use role="status" aria-live="polite" for routine notifications, and role="alert" aria-live="assertive" for anything urgent enough to interrupt, like the delete/undo toast above.

Once toasts are just DOM nodes you create and destroy on events, "stacking" isn't a feature you build — it falls out of .toast-container being a flex column and nothing more.

← Back to all posts