Layout · 06 of 23

Toast

Notification slide-in

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<button class="trigger-btn" id="toastBtn">Show Toast</button>

<div class="toast-container" id="toastContainer"></div>

CSS

.trigger-btn {
  padding: 0.6rem 1.2rem;
  background: #7c5cff;
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}

/* Toasts stack up from the bottom-right corner */
.toast-container {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.toast {
  background: #12121a;
  border: 1px solid #7c5cff;
  padding: 1rem 1.5rem;
  border-radius: 8px;
  color: #fff;
  min-width: 200px;
  animation: slideIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

JavaScript

var toastBtn = document.getElementById('toastBtn');
var toastContainer = document.getElementById('toastContainer');
var LIFETIME = 3000; // ms before the toast fades out

toastBtn.addEventListener('click', function () {
  var toast = document.createElement('div');
  toast.className = 'toast';
  toast.textContent = 'Notification Received';
  toastContainer.appendChild(toast);

  // Entry is handled by the CSS animation; exit is done inline
  setTimeout(function () {
    toast.style.transition = 'opacity 0.5s, transform 0.5s';
    toast.style.opacity = '0';
    toast.style.transform = 'translateY(20px)';
    setTimeout(function () { toast.remove(); }, 500);
  }, LIFETIME);
});