Still Failing at Toast Notification in JavaScript?

Let’s Be Confident and Start Coding :

<button id="opentoast" class="toast-btn">show me Toast</button>

<div class="toast-container" id="toast">
  <span class="toast-message">This is a toast notification!</span>
</div>

CSS Explanation (Main Rules Only) :

.toast-container {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(100px); /* hidden */
  background: #333;
  color: #fff;
  padding: 15px 20px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 220px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  font-size: 15px;
  transition: right 0.4s ease;
  z-index: 9999;
}

.toast-container.show {
  transform: translateX(-50%) translateY(0); /* visible */
}

JavaScript Code

const toast = document.getElementById("toast");
const opentoast = document.getElementById("opentoast");

opentoast.addEventListener("click", () => {
  toast.classList.add("show");

  setTimeout(() => {
    toast.classList.remove("show");
  }, 3000);
});

Why setTimeout is important?

Want to View the Full Code?

Conclusion : –

Stay Ahead in Your Tech Career 🚀

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top