Class 33 of 48 • Interactive JavaScript

Bootstrap 5 Toasts: Push Notifications, Auto-Hide & Positioning

Toasts are lightweight, non-disruptive push notifications designed to mimic smartphone push alerts. They float in the corner of the viewport (typically top-right or bottom-right), automatically dismiss after a configurable timeout, and stack neatly when multiple notifications arrive.

Bootstrap 5 Push Notification Toasts with Auto-Hide
Figure 33: Toast notification layout: header icon, timestamp, dismiss button, and auto-dismiss stacking container.

1. Fixed Toast Container & Toast Markup

Place toasts inside a fixed .toast-container positioned at the top-right corner:

<!-- Toast Container (Positioned Top Right) -->
<div class="toast-container position-fixed top-0 end-0 p-3" style="z-index: 1090;">
  <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-bs-delay="5000">
    <div class="toast-header bg-primary text-white">
      <strong class="me-auto"><i class="fa fa-bell"></i> System Notice</strong>
      <small>Just now</small>
      <button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
    </div>
    <div class="toast-body bg-white">
      Class 33 practice exercises have been unlocked!
    </div>
  </div>
</div>

2. Triggering Toasts Programmatically via JavaScript

<button type="button" class="btn btn-primary" id="toastTriggerBtn">Show Live Toast</button>

<script>
  const triggerBtn = document.getElementById('toastTriggerBtn');
  const toastEl = document.getElementById('liveToast');

  if (triggerBtn) {
    triggerBtn.addEventListener('click', () => {
      const toast = bootstrap.Toast.getOrCreateInstance(toastEl);
      toast.show();
    });
  }
</script>

Explore Related Bootstrap Tutorials & Next Steps

Strengthen your responsive web design mastery by studying these connected classes:

Static Alerts vs Dynamic Toasts

Choose between inline persistent alerts and floating auto-dismissing toasts.

Modals & Toasts Integration

Trigger a success toast after a modal form is successfully submitted.

Toast JavaScript API

Programmatically create, show, and auto-dismiss toast notifications.

Toast ARIA Live Notifications

Configure role='status' and aria-live='polite' for screen reader alerts.

Frequently Asked Questions (FAQ) - Student & Interview Review

Add data-bs-autohide='false' to the .toast element.
By default, Bootstrap toasts auto-hide after 5000 milliseconds (5 seconds). Customize this with data-bs-delay='8000'.
Yes! Placing multiple .toast elements inside a single .toast-container automatically stacks them vertically.

Next Step in Your Bootstrap Learning Path:

Advance your practical frontend engineering skills with the next topic in our synchronized curriculum.

Continue to Class 34: Spacing (m-* & p-*) → Previous: ← Class 32: Tooltips & Popovers | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!