Bootstrap Toasts
Learn how to display temporary notifications using Bootstrap 5.
Introduction
A Toast is a small notification message that appears on a webpage to inform users about an action or event. Toasts can display success messages, error notifications, reminders and other updates without navigating to another page.
Learning Objectives
- Understand Bootstrap Toasts.
- Create success and error notifications.
- Show and hide Toasts using JavaScript.
- Configure automatic hiding.
- Build a real-life notification example.
How Toasts Work
Step 1
The user saves a form or clicks a button.
Step 2
JavaScript displays a Toast message.
Step 3
The Toast hides after the configured delay.
1. Basic Toast
The toast class creates a Toast component.
The toast-header contains the title and
close button, while toast-body contains
the notification message.
Program Code
<button id="showToastBtn"
class="btn btn-primary"
type="button">
Show Toast
</button>
<div id="basicToast"
class="toast"
role="status"
aria-live="polite"
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">
Your action was completed.
</div>
</div>
<script>
const toastElement = document.getElementById("basicToast");
const toast = bootstrap.Toast.getOrCreateInstance(
toastElement,
{ delay: 3000 }
);
document.getElementById("showToastBtn")
.addEventListener("click", function () {
toast.show();
});
</script>
Program Output
Click Show Toast to display the notification.
2. Success Toast
A success Toast informs users that an action has completed. For example, a website can display a success message after a form has been submitted successfully.
Program Code
<button type="button"
class="btn btn-success"
onclick="showSuccessToast()">
Save Details
</button>
<div id="successToast"
class="toast"
role="status"
aria-live="polite">
<div class="toast-header">
<strong class="me-auto text-success">Success</strong>
<button type="button"
class="btn-close"
data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
<div class="toast-body">
Details saved successfully.
</div>
</div>
<script>
function showSuccessToast() {
bootstrap.Toast.getOrCreateInstance(
document.getElementById("successToast"),
{ delay: 3000 }
).show();
}
</script>
Program Output
Demo only: this button simulates a successful save.
3. Error Toast
An error Toast alerts users when an operation fails. In a real application, show this message after the server returns an error or the application detects a failure.
Program Code
<button type="button"
class="btn btn-danger"
onclick="showErrorToast()">
Simulate Error
</button>
<div id="errorToast" class="toast"
role="alert" aria-live="assertive">
<div class="toast-header">
<strong class="me-auto text-danger">Error</strong>
<button type="button"
class="btn-close"
data-bs-dismiss="toast"
aria-label="Close"></button>
</div>
<div class="toast-body">
Something went wrong.
</div>
</div>
<script>
function showErrorToast() {
bootstrap.Toast.getOrCreateInstance(
document.getElementById("errorToast"),
{ delay: 4000 }
).show();
}
</script>
Program Output
4. Toast with Auto Hide
The delay option specifies how long the
Toast stays visible, in milliseconds. The
autohide option determines whether the
Toast hides automatically.
Program Code
const toastElement = document.getElementById("timedToast");
const toast = bootstrap.Toast.getOrCreateInstance(
toastElement,
{
delay: 5000,
autohide: true
}
);
toast.show();
Program Output
5. Real-Life Example: CIIT Enquiry Form
Imagine a student fills out the enquiry form on the CIIT Training Institute website. After the form is successfully submitted, the website displays a Toast confirming that the enquiry was received.
If submission fails, the website can display an error notification instead. The following demo simulates both outcomes without sending any real data.
Program Output
This is a frontend demonstration. It does not save student details to a database.
Important Classes and Options
| Class / Option | Purpose |
|---|---|
toast |
Creates the Toast component. |
toast-header |
Displays the title and header controls. |
toast-body |
Displays the notification message. |
btn-close |
Creates a close button. |
delay |
Sets the auto-hide delay in milliseconds. |
autohide |
Enables or disables automatic hiding. |
bootstrap.Toast |
Provides JavaScript methods to control Toasts. |
Common Mistakes
- Forgetting to load the Bootstrap JavaScript bundle.
- Using an incorrect ID to select the Toast element.
- Expecting a Toast to appear without calling its show method.
-
Setting
autohide: falseand expecting the Toast to disappear automatically. - Showing a success notification before the server confirms success.
Summary
- Toasts display temporary notifications.
- Success and error Toasts communicate different outcomes.
- The Bootstrap Toast API provides the
show()method. - The delay option controls automatic hiding.
- Real applications should show results based on actual server responses.