TechBuzzPro

Bootstrap Toast Position & Stacking Guide

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Bootstrap toasts are lightweight notification components that appear in the corner of your screen. By default, they stack vertically and sit in the top-right, but you can place them anywhere and control how they pile up. This guide walks through positioning toasts with utility classes and stacking them using a container, plus common pitfalls and fixes.

What You'll Need

  • Bootstrap 5 (CSS and JS) included in your project. You can use a CDN or local files.
  • A basic HTML file with a <body> and a place to put the toast markup.
  • Optional: Bootstrap Icons or your own icon for the toast close button.

Step-by-Step

  1. Include Bootstrap assets. Add the Bootstrap CSS in the <head> and the Bootstrap JS bundle before the closing </body> tag. The bundle includes Popper, which toasts need for positioning.
  2. Create a toast container (for stacking). If you want multiple toasts to stack neatly, wrap them in a container with position: fixed and a placement utility class. Example: <div class="toast-container position-fixed top-0 end-0 p-3"></div>. This puts the container in the top-right corner with padding.
  3. Add toast markup inside the container. Each toast is a <div> with the classes toast and align-items-center (optional for vertical centering). Include a header and body. Example:
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
      <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">Hello, world!</div>
    </div>
  4. Place the container anywhere. Bootstrap provides placement utilities: top-0, bottom-0, start-0 (left), end-0 (right). Combine them: top-0 start-0 for top-left, bottom-0 end-0 for bottom-right, etc. Add p-3 for spacing from the edge. For example, bottom-center: position-fixed bottom-0 start-50 translate-middle-x p-3.
  5. Initialize toasts with JavaScript. To show a toast, use new bootstrap.Toast(toastElement).show(). You can also use data-bs-autohide="false" to keep it visible. For multiple toasts, loop through them or use event delegation.
  6. Stacking behavior. Toasts inside the same container stack vertically automatically. Each new toast appears below the previous one. To control the order, use the order class on the toast elements (e.g., order-1, order-2) if needed.
  7. Custom stacking with flexbox. If you want horizontal stacking or custom alignment, add d-flex flex-column (or flex-row) to the container. For example, toast-container position-fixed top-0 end-0 p-3 d-flex flex-column gap-2 adds a gap between toasts.

Common Mistakes

  • Forgetting the container. Placing toasts directly in the body without a fixed container results in them appearing in the document flow, not floating on top.
  • Missing the JS bundle. Toasts won't show or hide without Bootstrap's JavaScript. Ensure you include the bundle and that it loads after the HTML.
  • Using position: absolute instead of fixed. Absolute positioning scrolls with the page; fixed keeps the toast in the viewport.
  • Not adding p-3 or similar. Without padding, toasts touch the screen edges.
  • Overlapping toasts. If you have multiple containers, they can overlap. Use only one container for all toasts, or give each a distinct position.

Tips

  • Use data-bs-delay to control how long a toast stays visible. For example, data-bs-delay="5000" shows it for 5 seconds.
  • For accessibility, add role="alert" and aria-live="assertive" to important toasts.
  • Combine with z-index if other elements overlap. Bootstrap toasts have a default z-index of 1050, but you can increase it with a utility class like z-3.
  • Use the toast-container class for proper styling, even if you don't need stacking.
  • Test on mobile: bottom positions are often more thumb-friendly.

Troubleshooting

  • Toast doesn't appear. Check that you called .show() on the instance. Also verify the element has the toast class and that Bootstrap JS loaded without errors in the console.
  • Toast appears in the wrong place. Ensure the container has position-fixed and the correct placement classes. If using a custom position, double-check the utility classes (e.g., top-50 requires translate-middle to center).
  • Toasts stack horizontally instead of vertically. The default is vertical. If you added flex-row, remove it. Also check that the container doesn't have d-inline or similar.
  • Close button doesn't work. Make sure the button has data-bs-dismiss="toast" and that the JS bundle is included. Also ensure the toast is not disabled.
  • Toasts appear behind other elements. Increase the z-index of the container with a utility like z-3 (in Bootstrap 5, z-index utilities go up to 5).

Frequently Asked Questions

How do I show a toast on page load?

After creating the toast instance, call .show() inside a DOMContentLoaded event or at the end of the body script.

Can I have toasts in different corners at the same time?

Yes, create separate containers for each corner and place toasts in them. Each container positions independently.

How do I make a toast stay until manually closed?

Add data-bs-autohide="false" to the toast element, or set the autohide option to false when creating the instance.

Can I change the stacking order?

Yes, use the order utility class on the toast elements. Lower numbers appear first.

Do toasts work without jQuery?

Yes, Bootstrap 5 toasts are vanilla JavaScript. No jQuery needed.

How do I make a toast appear from the bottom on mobile?

Use a container with bottom-0 start-50 translate-middle-x to center it at the bottom. You can also use media queries to change the position on different screen sizes.