JavaScript Notes
Learn how to build a custom toast notification system in JavaScript from scratch. This beginner-friendly tutorial covers creating, positioning, animating, auto-dismissing, and stacking multiple toast alerts with complete source code.
Introduction
Toast notifications are small, non-blocking popup messages that appear briefly on screen to inform users about an action's result — like "File saved successfully" or "Error: Network failed." Unlike traditional alert() boxes, toast notifications don't interrupt the user's workflow.
In this project, you will build a complete toast notification system from scratch using vanilla JavaScript, HTML, and CSS. No libraries needed!
What you will learn:
- Creating DOM elements dynamically with JavaScript
- CSS animations for smooth enter/exit transitions
- Using
setTimeoutfor auto-dismiss functionality - Stacking multiple notifications without overlap
Note: In this tutorial we will build a custom toast notification system using JavaScript that can display success, error, warning, and info messages.
Toast Notification Types
| Type | Color | Use Case | Icon |
|---|---|---|---|
| Success | Green (#4caf50) | Action completed | ✓ |
| Error | Red (#f44336) | Something failed | ✗ |
| Warning | Orange (#ff9800) | Caution needed | ⚠ |
| Info | Blue (#2196f3) | General info | ℹ |
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Toast Notification System</title>
</head>
<body>
<div class="controls">
<h1>Toast Notification System</h1>
<button onclick="showToast('Operation successful!', 'success')">Success</button>
<button onclick="showToast('Something went wrong!', 'error')">Error</button>
<button onclick="showToast('Check your input.', 'warning')">Warning</button>
<button onclick="showToast('New update available.', 'info')">Info</button>
</div>
<div id="toast-container"></div>
<script src="script.js"></script>
</body>
</html>The #toast-container is empty — JavaScript fills it dynamically. Each button calls showToast() with a message and type.
CSS — Styling and Animations
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', sans-serif;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #1a1a2e;
color: #fff;
}
.controls { text-align: center; }
.controls button {
padding: 12px 24px;
margin: 5px;
border: none;
border-radius: 6px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
}
#toast-container {
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
display: flex;
flex-direction: column;
gap: 10px;
}
.toast {
display: flex;
align-items: center;
gap: 12px;
padding: 16px 20px;
min-width: 300px;
border-radius: 8px;
color: #fff;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
animation: slideIn 0.4s ease forwards;
}
.toast .icon { font-size: 1.4rem; }
.toast .message { flex: 1; }
.toast .close-btn {
background: none;
border: none;
color: #fff;
font-size: 1.2rem;
cursor: pointer;
opacity: 0.7;
}
.toast.success { background: #4caf50; }
.toast.error { background: #f44336; }
.toast.warning { background: #ff9800; }
.toast.info { background: #2196f3; }
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
@keyframes slideOut {
from { transform: translateX(0); opacity: 1; }
to { transform: translateX(100%); opacity: 0; }
}
.toast.exit { animation: slideOut 0.4s ease forwards; }Animation logic: slideIn moves toast from off-screen right to its position. Adding .exit class triggers slideOut before removal.
JavaScript — Core Logic
Creating a Toast
Removing a Toast with Animation
showToast('Saved!', 'success') →
1. Green toast slides in from right
2. Shows "✓ Saved!" with × button
3. After 3 seconds, slides out and is removed from DOMAuto-Dismiss with setTimeout
Note: setTimeout sets a timer — after 3 seconds the toast will automatically disappear.Stacking Multiple Toasts
Clicking multiple buttons rapidly creates independent toasts that stack:
showToast('First notification', 'success');
showToast('Second notification', 'error');
showToast('Third notification', 'info');┌──────────────────────────┐ ← top-right corner │ ✓ First notification × │ ├──────────────────────────┤ │ ✗ Second notification × │ ├──────────────────────────┤ │ ℹ Third notification × │ └──────────────────────────┘ Each disappears independently after its own 3s timer. When one is removed, others slide up via flexbox reflow.
Complete Working Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Toast Notification System</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', sans-serif;
min-height: 100vh;
display: flex; align-items: center; justify-content: center;
background: #1a1a2e; color: #fff;
}
.controls { text-align: center; }
.controls h1 { margin-bottom: 15px; }
.controls button {Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Toast Notification Project — Custom Alerts Tutorial 2026.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, toast
Related JavaScript Master Course Topics