Toast Notification using HTML, CSS & JavaScript

Toast Notification using HTML, CSS & JavaScript

Hello developers, today in this, blog you will learn to create a Toast Notification using HTML, CSS & JavaScript.

The toast notification provides notifications to the users. The notification pop up from the right bottom of the webpage. It also provides simple feedback about an operation in a device. It occupies some space required for the message and, the current activity remains visible and interactive. Toasts notification automatically disappears after a few seconds(time-out). The user can decide what kinds of notifications appear and how long they remain on the screen.

In this blog (Toast Notification), there is a button named to show the notification. When you click on the button, the notification will get a popup at the right bottom of the screen which may give you some information you received a message from someone else. This toast notification will get disappear automatically after a few seconds. If more notifications come at the same time, the notifications get popup one by one that they are vertically arranged. The first notification goes a little up to the second notification.

The source code of this Toast Notification is given below, if you want the source code of this program, you can copy it. You can use this Toast Notification code with your creativity and can take this project to the next level.

Toast Notification [Source Code]

To make this website (Toast Notification), you need to create three files: an HTML file, a CSS file, and JavaScript. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html extension.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> <title>Toast Notification || Learningrobo</title> </head> <body> <div id="toasts"></div> <button class="btn" id="button">Show Notification</button> <div class="credit">Made with <span style="color:tomato">❤</span> by <a href="https://www.learningrobo.com/">Learning Robo</a></div> <script src="script.js"></script> </body> </html>
CSS provides style to an HTML page.To make the page attractive create a CSS file with the name style.css and remember that you have to create a file with a .css extension.


@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400&display=swap');

* {
  box-sizing: border-box;
}

body {
    background: #F2994A; 
    background: -webkit-linear-gradient(to right, #F2C94C, #F2994A);  
    background: linear-gradient(to right, #F2C94C, #F2994A); 
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

.btn {
  background-color: #ffffff;
  color: #000;
  font-family: inherit;
  font-weight: bold;
  padding: 1rem;
  border-radius: 5px;
  border: none;
  cursor: pointer;
}

.btn:focus {
  outline: none;
}

.btn:active {
  transform: scale(0.98);
}

#toasts {
  position: fixed;
  bottom: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.toast {
  background-color: #fff;
  border-radius: 5px;
  padding: 1rem 2rem;
  margin: 0.5rem;
}

.toast.info {
  color: rebeccapurple;
}

.toast.success {
  color: green;
}

.toast.error {
  color: red;
}

.credit a{
    text-decoration: none;
    color: #fff;
  }

  .credit {
      margin:10px auto;
      text-align: center;
  }
JavaScript makes the page work functionally. At last, create a JavaScript file with the name of script.js, and remember that you've got to make a file with a .js extension.


const button = document.getElementById('button')
const toasts = document.getElementById('toasts')

const messages = [
    'Message One',
    'Message Two',
     'Message Three',
    'Message Four',
]

const types = ['info', 'success', 'error']

button.addEventListener('click', () => createNotification())

function createNotification(message = null, type = null) {
    const notif = document.createElement('div')
    notif.classList.add('toast')
    notif.classList.add(type ? type : getRandomType())

    notif.innerText = message ? message : getRandomMessage()

    toasts.appendChild(notif)

    setTimeout(() => {
        notif.remove()
    }, 9000)
}

function getRandomMessage() {
    return messages[Math.floor(Math.random() * messages.length)]
}

function getRandomType() {
    return types[Math.floor(Math.random() * types.length)]
}
We hope you would like this Toast Notification using HTML, CSS & JavaScript.

Thank you for reading our blog. If you face any problem in creating this Toast Notification using HTML, CSS & JavaScript, then contact us or comment us. We’ll try to provide a solution to your problem as soon as possible.

Thank you
Learning robo team

Post a Comment

Thank you
Learning robo team

Post a Comment (0)

Previous Post Next Post
Learning Robo says...
code copied
Welcome