UI Snippet
🔔 Toast Notifications
A modern toast notification system with 4 variants, auto-dismiss, slide-in animation, and stacking. Pure HTML, CSS, and JavaScript.
🔍 Live Demo
Click any button to fire a toast notification.
📘 Features
✅ 4 variants — success, error, warning, info
✅ Auto-dismiss after 4 seconds
✅ Progress bar showing remaining time
✅ Slide-in animation from the right
✅ Stacking — multiple toasts appear in a column
✅ Manual close with × button
✅ Auto-dismiss after 4 seconds
✅ Progress bar showing remaining time
✅ Slide-in animation from the right
✅ Stacking — multiple toasts appear in a column
✅ Manual close with × button
💻 Complete Code
<!-- HTML: Toast container (add anywhere in body) -->
<div class="toast-container" id="toastContainer"></div>
<!-- CSS -->
.toast-container {
position: fixed;
top: 80px;
right: 20px;
z-index: 9999;
display: flex;
flex-direction: column;
gap: 0.75rem;
max-width: 380px;
pointer-events: none;
}
.toast-container > * { pointer-events: auto; }
.toast {
display: flex;
align-items: flex-start;
gap: 0.75rem;
background: #1e293b;
border: 1px solid #334155;
border-left: 4px solid #3b82f6;
border-radius: 0.75rem;
padding: 1rem;
color: #cbd5e1;
box-shadow: 0 10px 25px -10px rgba(0,0,0,0.5);
transform: translateX(120%);
opacity: 0;
transition: transform 0.35s ease, opacity 0.35s ease;
min-width: 300px;
position: relative;
overflow: hidden;
}
.toast.show { transform: translateX(0); opacity: 1; }
.toast.success { border-left-color: #22c55e; }
.toast.error { border-left-color: #ef4444; }
.toast.warning { border-left-color: #f59e0b; }
.toast.info { border-left-color: #3b82f6; }
.toast-icon { font-size: 1.25rem; flex-shrink: 0; margin-top: 1px; }
.toast.success .toast-icon { color: #22c55e; }
.toast.error .toast-icon { color: #ef4444; }
.toast.warning .toast-icon { color: #f59e0b; }
.toast.info .toast-icon { color: #3b82f6; }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; color: white; font-size: 0.9rem; margin-bottom: 0.15rem; }
.toast-message { font-size: 0.8rem; color: #94a3b8; }
.toast-close {
background: none; border: none; color: #64748b;
cursor: pointer; font-size: 1rem; padding: 0; line-height: 1;
flex-shrink: 0;
}
.toast-close:hover { color: white; }
.toast-progress {
position: absolute; left: 0; bottom: 0;
height: 2px; background: currentColor;
animation: toast-progress 4s linear forwards;
}
.toast.success .toast-progress { background: #22c55e; }
.toast.error .toast-progress { background: #ef4444; }
.toast.warning .toast-progress { background: #f59e0b; }
.toast.info .toast-progress { background: #3b82f6; }
@keyframes toast-progress {
from { width: 100%; }
to { width: 0%; }
}
<!-- JavaScript -->
const ICONS = {
success: '<i class="fas fa-check-circle"></i>',
error: '<i class="fas fa-times-circle"></i>',
warning: '<i class="fas fa-exclamation-triangle"></i>',
info: '<i class="fas fa-info-circle"></i>'
};
function showToast(type, title, message, duration = 4000) {
const container = document.getElementById('toastContainer');
// Create toast element
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.innerHTML = `
<div class="toast-icon">${ICONS[type]}</div>
<div class="toast-body">
<div class="toast-title">${title}</div>
<div class="toast-message">${message}</div>
</div>
<button class="toast-close" aria-label="Close">×</button>
<div class="toast-progress" style="animation-duration: ${duration}ms;"></div>
`;
container.appendChild(toast);
// Trigger slide-in on next frame
requestAnimationFrame(() => {
requestAnimationFrame(() => toast.classList.add('show'));
});
// Close handler
const closeBtn = toast.querySelector('.toast-close');
closeBtn.addEventListener('click', () => removeToast(toast));
// Auto-dismiss
const timer = setTimeout(() => removeToast(toast), duration);
// Pause on hover (nice UX)
toast.addEventListener('mouseenter', () => {
clearTimeout(timer);
toast.querySelector('.toast-progress').style.animationPlayState = 'paused';
});
toast.addEventListener('mouseleave', () => {
// Just let it finish quickly on leave
const newTimer = setTimeout(() => removeToast(toast), 800);
});
}
function removeToast(toast) {
toast.classList.remove('show');
toast.addEventListener('transitionend', () => toast.remove(), { once: true });
}
// Example: fire all four in sequence
function stackToasts() {
showToast('success', 'Saved', 'Your changes have been saved.');
setTimeout(() => showToast('info', 'Syncing', 'Sync in progress...'), 200);
setTimeout(() => showToast('warning', 'Heads up', 'You have 3 unread messages.'), 400);
}
🧠 How It Works
- Fixed container: A single
#toastContainersits at the top-right of the viewport. All toasts go inside it. - Slide-in: Toasts start at
translateX(120%)and animate to0when the.showclass is added. - Progress bar: A CSS animation on the bottom border shows remaining time.
- Auto-dismiss: A
setTimeoutremoves the toast after 4 seconds (configurable). - Stacking: Flexbox column layout naturally stacks multiple toasts.
- Pause on hover: Timer stops when the user hovers — so they can read the message.
- Four variants: Each uses a different border-left color and icon.
- Accessible: Close button has
aria-label, uses semantic icons.
💡 Why This Snippet?
- Universally needed — every web app shows notifications (success, error, info).
- Zero dependencies — pure HTML/CSS/JS, no libraries.
- Copy-paste ready — drop the container div + CSS + JS into any project.
- Customizable — change colors, duration, or position with CSS variables.
- Better UX — pause-on-hover and progress bar are details most snippets miss.
- Small footprint — under 2 KB of JS.