Toast notifications are lightweight, non-intrusive messages that provide feedback to the user. They appear on the screen for a short duration and then automatically disappear, making them ideal for brief notifications that do not interrupt the user’s workflow.
Toasts can convey information such as success messages, errors, or general updates, enhancing user experience by providing immediate feedback without disrupting the workflow.
You can customize Toast notifications by adjusting their duration, style, and content. For example:
Click the buttons below to see Toast notifications in action:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Toast Examples</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<div class="d-flex flex-column gap-2 mb-3">
<!-- Toast Trigger Buttons -->
<button type="button" class="btn btn-success" id="successToastBtn">Show Success Toast</button>
<button type="button" class="btn btn-danger" id="errorToastBtn">Show Error Toast</button>
<button type="button" class="btn btn-info" id="infoToastBtn">Show Info Toast</button>
<button type="button" class="btn btn-warning" id="warningToastBtn">Show Warning Toast</button>
<button type="button" class="btn btn-dark" id="customToastBtn">Show Custom Toast</button>
</div>
<!-- Toast Notification Container -->
<div class="toast-container position-fixed bottom-0 end-0 p-3" aria-live="polite" aria-atomic="true">
<div class="toast" id="successToast" role="alert" aria-live="assertive" aria-atomic="true" style="display: none;">
<div class="toast-header">
<strong class="me-auto">Success</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Your action was successful!
</div>
</div>
<div class="toast" id="errorToast" role="alert" aria-live="assertive" aria-atomic="true" style="display: none;">
<div class="toast-header">
<strong class="me-auto">Error</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
There was an error processing your request.
</div>
</div>
<div class="toast" id="infoToast" role="alert" aria-live="assertive" aria-atomic="true" style="display: none;">
<div class="toast-header">
<strong class="me-auto">Info</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is some information for you.
</div>
</div>
<div class="toast" id="warningToast" role="alert" aria-live="assertive" aria-atomic="true" style="display: none;">
<div class="toast-header">
<strong class="me-auto">Warning</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is a warning message. Please check the input fields!
</div>
</div>
<div class="toast" id="customToast" role="alert" aria-live="assertive" aria-atomic="true" style="display: none;">
<div class="toast-header">
<strong class="me-auto">Custom Message</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
This is a customizable toast message with extra information.
</div>
</div>
</div>
</div>
<script>
// Function to show toast notifications
function showToast(toastId) {
const toastElement = document.getElementById(toastId);
const toast = new bootstrap.Toast(toastElement);
toast.show();
toastElement.style.display = 'block'; // Ensure toast is displayed
}
// Event listeners for toast buttons
document.getElementById('successToastBtn').addEventListener('click', () => showToast('successToast'));
document.getElementById('errorToastBtn').addEventListener('click', () => showToast('errorToast'));
document.getElementById('infoToastBtn').addEventListener('click', () => showToast('infoToast'));
document.getElementById('warningToastBtn').addEventListener('click', () => showToast('warningToast'));
document.getElementById('customToastBtn').addEventListener('click', () => showToast('customToast'));
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>