webdevpuneet.com

Bootstrap Toast Notification Stack — Free Snippet

Bootstrap Toast Notification Stack · Modals · Plain HTML, CSS & JS · Live preview

CategoryModals

Copy & paste

Source Code

<div class="container py-5 text-center">
  <div class="d-flex gap-2 justify-content-center flex-wrap">
    <button class="btn btn-success btn-sm" data-type="success">Trigger success</button>
    <button class="btn btn-danger btn-sm" data-type="danger">Trigger error</button>
    <button class="btn btn-secondary btn-sm" data-type="info">Trigger info</button>
  </div>
  <p class="text-muted small mt-3">Toasts stack, each auto-dismisses after 4s, or click × to close early.</p>
</div>

<div class="toast-container position-fixed bottom-0 end-0 p-3" id="bstoastContainer"></div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Real Bootstrap 5.3 Toast component, using its own built-in auto-dismiss delay, not setTimeout
Toasts are created fresh on each trigger and genuinely stack — any number can be visible at once
Removes each toast's DOM element only after Bootstrap's own hidden.bs.toast event fires
Three ready-made message types (success, danger, info) driven by one small config object
Manual dismiss (×) and auto-dismiss both work correctly without any custom timer logic
No memory leak from accumulating hidden-but-undeleted toast elements over a long session

About this UI Snippet

Bootstrap Toast Notification Stack — HTML, CSS & JavaScript

Screenshot of the Bootstrap Toast Notification Stack snippet rendered live

Bootstrap's real Toast component only shows one static example in most demos — this snippet shows the pattern a real app actually needs: creating a new toast on demand and letting several stack in the corner at once. Each button click builds a fresh <div class="toast"> element from a small message config, appends it into a .toast-container, and hands it to a new bootstrap.Toast instance with a 4-second delay — Bootstrap's own auto-dismiss timer, not a custom setTimeout.

One detail matters for anything beyond a single toast: Bootstrap hides a toast on dismiss but never removes its element from the DOM. Left unhandled, that would silently leave an invisible element behind after every notification. This snippet listens for Bootstrap's own hidden.bs.toast event — fired once the hide animation genuinely finishes — and removes the element then, so triggering dozens of toasts over a session never leaks stale nodes.

Step by step

How to Use

  1. 1
    Load the snippetClick the snippet in the sidebar Library tab. The preview loads three trigger buttons.
  2. 2
    Click "Trigger success"A real Bootstrap toast slides in at the bottom-right corner and auto-dismisses after 4 seconds.
  3. 3
    Click multiple triggers quicklySeveral toasts stack vertically in the corner, each running its own independent 4-second timer.
  4. 4
    Dismiss one earlyClick a toast's × button — it closes immediately rather than waiting for its timer.
  5. 5
    Customize the messageEdit the MESSAGES object in the JS panel to change icons, colors, and text per type.

Real-world uses

Common Use Cases

Save confirmations and background action feedback
The standard place for "saved," "sent," or "uploaded" confirmations that shouldn't interrupt what the user is doing.
Learning to create Bootstrap components dynamically
Most Bootstrap component demos show static markup — this is the pattern for genuinely creating and mounting instances at runtime.
Error and status messaging in dashboards
Pair with a real API call's success/error handlers to surface backend responses without a full-page alert.
Any app with background or async operations
File uploads, autosave, sync status — anywhere a brief, non-blocking notification is the right amount of interruption.

Got questions?

Frequently Asked Questions

Real Bootstrap 5.3 — every toast is a genuine bootstrap.Toast instance using the actual component's show/hide/delay behavior, loaded from the real CDN.

Yes — Bootstrap only hides a dismissed toast, it never removes the element. This snippet listens for the hidden.bs.toast event (fired once the hide animation completes) and removes the element then, so nothing accumulates in the DOM.

Yes — each click creates and mounts an entirely new toast element into the shared .toast-container, so triggering several in a row shows them all stacked, each running its own independent dismiss timer.

Change the delay: 4000 value (in milliseconds) passed to new bootstrap.Toast(el, { delay: ... }).

Yes — add a new key to the MESSAGES object with its own icon, Bootstrap background class (bg), and text, then trigger it from a button with the matching data-type attribute.