Bootstrap Toast Position & Stacking Guide
In this article
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
- 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. - Create a toast container (for stacking). If you want multiple toasts to stack neatly, wrap them in a container with
position: fixedand 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. - Add toast markup inside the container. Each toast is a
<div>with the classestoastandalign-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> - Place the container anywhere. Bootstrap provides placement utilities:
top-0,bottom-0,start-0(left),end-0(right). Combine them:top-0 start-0for top-left,bottom-0 end-0for bottom-right, etc. Addp-3for spacing from the edge. For example, bottom-center:position-fixed bottom-0 start-50 translate-middle-x p-3. - Initialize toasts with JavaScript. To show a toast, use
new bootstrap.Toast(toastElement).show(). You can also usedata-bs-autohide="false"to keep it visible. For multiple toasts, loop through them or use event delegation. - Stacking behavior. Toasts inside the same container stack vertically automatically. Each new toast appears below the previous one. To control the order, use the
orderclass on the toast elements (e.g.,order-1,order-2) if needed. - Custom stacking with flexbox. If you want horizontal stacking or custom alignment, add
d-flex flex-column(orflex-row) to the container. For example,toast-container position-fixed top-0 end-0 p-3 d-flex flex-column gap-2adds 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: absoluteinstead of fixed. Absolute positioning scrolls with the page; fixed keeps the toast in the viewport. - Not adding
p-3or 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-delayto control how long a toast stays visible. For example,data-bs-delay="5000"shows it for 5 seconds. - For accessibility, add
role="alert"andaria-live="assertive"to important toasts. - Combine with
z-indexif other elements overlap. Bootstrap toasts have a default z-index of 1050, but you can increase it with a utility class likez-3. - Use the
toast-containerclass 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 thetoastclass and that Bootstrap JS loaded without errors in the console. - Toast appears in the wrong place. Ensure the container has
position-fixedand the correct placement classes. If using a custom position, double-check the utility classes (e.g.,top-50requirestranslate-middleto 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 haved-inlineor 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 notdisabled. - 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.