Bootstrap 5 Tutorial
v5.3.0Bootstrap 5 Tutorial
Toasts in Bootstrap 5
Toasts: Lightweight notifications designed to mimic push notifications.
Basic Toast
Simple Toast Example
Bootstrap11 mins ago
Hello, world! This is a toast message.
<!-- Basic toast structure -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
<!-- JavaScript initialization -->
<script>
const toastElList = document.querySelectorAll('.toast')
const toastList = [...toastElList].map(toastEl => new bootstrap.Toast(toastEl))
</script>Live Demo Area
Interactive Toast Demo
Toasts will appear here
Toast Types
Toast Color Variations
Primary ToastJust now
This is a primary toast message.
Success Toast2 mins ago
Operation completed successfully!
Warning Toast5 mins ago
Please check your input data.
Error Toast10 mins ago
An error occurred while processing.
<!-- Toast with different colors -->
<div class="toast">
<div class="toast-header bg-primary text-white">
<strong class="me-auto">Primary</strong>
<button type="button" class="btn-close btn-close-white"></button>
</div>
<div class="toast-body">Message</div>
</div>
<div class="toast">
<div class="toast-header bg-success text-white">
<strong class="me-auto">Success</strong>
<button type="button" class="btn-close btn-close-white"></button>
</div>
<div class="toast-body">Message</div>
</div>
<div class="toast">
<div class="toast-header bg-warning text-dark">
<strong class="me-auto">Warning</strong>
<button type="button" class="btn-close"></button>
</div>
<div class="toast-body">Message</div>
</div>
<div class="toast">
<div class="toast-header bg-danger text-white">
<strong class="me-auto">Error</strong>
<button type="button" class="btn-close btn-close-white"></button>
</div>
<div class="toast-body">Message</div>
</div>Toast with Custom Content
Advanced Toast Examples
Notification
System alert
You have 3 new notifications
Processing...Just now
75%
Order Update
Wireless Headphones
Your order has been shipped!
Tracking #: ABC123456Confirmation Required
Are you sure you want to delete this item?
<!-- Toast with icon -->
<div class="toast">
<div class="toast-header">
<div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center me-2" style="width: 30px; height: 30px;">
<i class="bi bi-bell"></i>
</div>
<strong class="me-auto">Notification</strong>
<button type="button" class="btn-close"></button>
</div>
<div class="toast-body">
<p class="mb-2">You have new notifications</p>
<button class="btn btn-sm btn-primary">View All</button>
</div>
</div>
<!-- Toast with progress bar -->
<div class="toast">
<div class="toast-header">
<div class="spinner-border spinner-border-sm text-primary me-2" role="status"></div>
<strong class="me-auto">Processing...</strong>
<button type="button" class="btn-close"></button>
</div>
<div class="toast-body">
<div class="progress">
<div class="progress-bar" style="width: 75%"></div>
</div>
</div>
</div>
<!-- Toast with product info -->
<div class="toast">
<div class="toast-header bg-info text-white">
<strong class="me-auto">
<i class="bi bi-cart-check me-1"></i>
Order Update
</strong>
<button type="button" class="btn-close btn-close-white"></button>
</div>
<div class="toast-body">
<div class="d-flex">
<img src="product.jpg" alt="Product" style="width: 50px; height: 50px;"/>
<div class="ms-3">
<h6 class="mb-1">Product Name</h6>
<p class="mb-0">Your order has been shipped!</p>
</div>
</div>
</div>
</div>Toast Placement Options
| Position | CSS Classes | Description |
|---|---|---|
| Top Start | top-0 start-0 | Top left corner |
| Top Center | top-0 start-50 translate-middle-x | Top center |
| Top End | top-0 end-0 | Top right corner (default) |
| Middle Start | top-50 start-0 translate-middle-y | Middle left |
| Middle Center | top-50 start-50 translate-middle | Center of screen |
| Middle End | top-50 end-0 translate-middle-y | Middle right |
| Bottom Start | bottom-0 start-0 | Bottom left corner |
| Bottom Center | bottom-0 start-50 translate-middle-x | Bottom center |
| Bottom End | bottom-0 end-0 | Bottom right corner |
Tip: Use the
position-fixed class for toast containers to keep them in view during scrolling.JavaScript API
JavaScript Methods and Events
| Method | Description | Example |
|---|---|---|
.show() | Shows toast | myToast.show() |
.hide() | Hides toast | myToast.hide() |
.dispose() | Destroys toast | myToast.dispose() |
.getInstance() | Gets toast instance | bootstrap.Toast.getInstance(toastEl) |
Configuration Options
animation: true- Apply CSS fade transitionautohide: true- Auto hide the toastdelay: 5000- Delay in ms before hiding
Events
show.bs.toast- Fires immediately when show is calledshown.bs.toast- Fires when toast is visiblehide.bs.toast- Fires immediately when hide is calledhidden.bs.toast- Fires when toast is hidden
// JavaScript for toast control
// Initialize toast
const toastEl = document.getElementById('myToast');
const toast = new bootstrap.Toast(toastEl, {
animation: true,
autohide: true,
delay: 5000
});
// Show toast
toast.show();
// Hide toast
toast.hide();
// Event handling
toastEl.addEventListener('shown.bs.toast', function () {
console.log('Toast was shown');
});
toastEl.addEventListener('hidden.bs.toast', function () {
console.log('Toast was hidden');
});
// Create toast dynamically
function createToast(title, message, type = 'info') {
const toastId = 'toast-' + Date.now();
const toastHtml = `
<div id="${toastId}" class="toast" role="alert">
<div class="toast-header bg-${type} text-white">
<strong class="me-auto">${title}</strong>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast"></button>
</div>
<div class="toast-body">
${message}
</div>
</div>
`;
// Add to container
const container = document.getElementById('toastContainer');
container.insertAdjacentHTML('beforeend', toastHtml);
// Initialize and show
const newToastEl = document.getElementById(toastId);
const newToast = new bootstrap.Toast(newToastEl, {
autohide: true,
delay: 5000
});
newToast.show();
// Remove from DOM after hide
newToastEl.addEventListener('hidden.bs.toast', function () {
this.remove();
});
}
// Example usage
createToast('Success', 'Operation completed successfully!', 'success');Accessibility Features
Accessibility Best Practices
- Use
role="alert"orrole="status" - Include
aria-liveattribute (assertive or polite) - Add
aria-atomic="true"for screen readers - Provide close button with proper label
- Ensure proper color contrast
- Manage focus appropriately
- Test with screen readers
Real-World Examples
Practical Use Cases
Form Submission Feedback
Shows success toast on submission
Network Status
E-commerce Notifications
System Alerts
Custom Styling
Gradient Toast
Premium FeatureJust now
You've unlocked a premium feature!
Glassmorphism Toast
Modern Design2 mins ago
This toast uses glassmorphism effect.
/* Custom CSS for toasts */
.custom-toast {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
.custom-toast .toast-header {
background: rgba(255, 255, 255, 0.1);
color: white;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}
.custom-toast .btn-close {
filter: invert(1);
}
.glass-toast {
background: rgba(255, 255, 255, 0.2);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 10px;
}
/* Animation for toast entrance */
@keyframes slideInRight {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.slide-toast {
animation: slideInRight 0.3s ease-out;
}