Creating a Custom Toast Notification using HTML, CSS, and JS Tutorial

In this tutorial, you can learn to create a custom and simple Toast Notification using HTML, CSS, and Pure JavaScript. The tutorial aims to provide students and beginners with a reference for learning to build a popup message box for websites or web applications. Here, I will be providing a sample web page script that demonstrates the creation of a simple Toast Notification.

What is Toast Notification?

The Toast Notification is one of the most common components or web features of a website or web application nowadays. It is a Popup or Overlay message or notification that often comes with some icons and a progress bar for the duration of showing the notification. This component is usually implemented by developers to provide better and more interactive web features.

How to Create a Toast Notification

The Toast Notification can be achieved easily using some HTML elements, CSS properties, and JavaScript event listeners and methods. We can simply create the Toast Box with some HTML Elements such as the div tag and design it with the CSS and put some animation to make it more interactive. Then, using JavaScript we can create a class or function that we can make the toast notification reusable and easy to initialize. Check out the source code scripts that I created and provided below to understand it more.

Sample Web Page Scripts

The following scripts result in a simple web page that contains 4 different buttons. Each button triggers to show a toast notification. The toast notification has 4 different types which are the success, danger, info, and warning toast notification. Each type has a specific icon.

Page Interface

Here's the HTML file script known as index.html. The file contains the page layout and button elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Toast Notification</title>
  6. <link rel="stylesheet" href="style.css">
  7. <link rel="stylesheet" href="customToast.css">
  8. <link rel="preconnect" href="https://fonts.googleapis.com">
  9. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  10. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  11. </head>
  12. <!-- <div class="toast toast-warning">
  13. <button class="toast-close-btn"><span class="material-symbols-outlined">close</span></button>
  14. <div class="toast-content-wrapper">
  15. <div class="toast-icon">
  16. <span class="material-symbols-outlined">task_alt</span>
  17. </div>
  18. <div class="toast-message">Sample success toast message.</div>
  19. <div class="toast-progress"></div>
  20. </div>
  21. </div> -->
  22. <div class="container">
  23. <h1 id="page-title">Toast Notification using HTML, CSS, and JS</h1>
  24. <hr id="title_hr">
  25. <div class="btn-wrapper">
  26. <button type="button" class="btn-toast btn-success-toast">Success Toast</button>
  27. <button type="button" class="btn-toast btn-danger-toast">Danger Toast</button>
  28. <button type="button" class="btn-toast btn-info-toast">Info Toast</button>
  29. <button type="button" class="btn-toast btn-warning-toast">Warning Toast</button>
  30. </div>
  31. </div>
  32. <script src="customToast.js"></script>
  33. <script src="script.js"></script>
  34. </body>
  35. </html>

Default Stylesheet

Next, here's the default CSS file script known as style.css. The file contains the page layout design stylesheet.

  1. @import url('https://fonts.googleapis.com/css2?family=Dongle:wght@300;400;700&family=Roboto+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;1,100;1,200;1,300;1,400;1,500;1,600&display=swap" rel="stylesheet');
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. font-family: 'Dongle', sans-serif;
  7. font-family: 'Roboto Mono', monospace;
  8. }
  9. ::selection{
  10. color: #fff;
  11. background: #4db2ec;
  12. }
  13. body{
  14. display: flex;
  15. align-items: center;
  16. justify-content: center;
  17. min-height: 100vh;
  18. background: #4facfe;
  19. background-image: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
  20. padding: 2em 0;
  21. }
  22. #page-title{
  23. color: #fff;
  24. text-align: center;
  25. font-weight: 500;
  26. text-shadow: 0px 0px 15px #0000003a;
  27. }
  28. #title_hr{
  29. width:60px;
  30. border: 2px solid #ffffff;
  31. margin: .35em auto;
  32. }
  33. @media (min-width: 780px){
  34. #page-title{
  35. width: 780px;
  36. }
  37. }
  38. .btn-wrapper{
  39. width: 680px;
  40. display: flex;
  41. flex-wrap:wrap;
  42. justify-content: space-evenly;
  43. grid-gap:10px;
  44. margin:2em auto;
  45. }
  46.  
  47. @media (max-width:700px){
  48. .btn-wrapper{
  49. width: 95%;
  50. }
  51. }
  52. button.btn-toast {
  53. padding: 0.5rem 1rem;
  54. border: none;
  55. background: #000;
  56. color: #fff;
  57. font-weight: 500;
  58. border-radius: 5px;
  59. box-shadow: 0px 0px 10px #0000004a;
  60. cursor: pointer;
  61. }
  62. button.btn-toast:hover{
  63. filter:brightness(.9);
  64. }
  65.  
  66. button.btn-success-toast{
  67. background-color:#26c468;
  68. }
  69. button.btn-warning-toast{
  70. background-color:#c99e25;
  71. }
  72. button.btn-info-toast{
  73. background-color:#5fbdfc;
  74. }
  75. button.btn-danger-toast{
  76. background-color:#ff3f3f;
  77. }

Toast Notification Stylesheet

Next, here's the CSS file script known as customToast.css. The file contains the stylesheet codes for the toast notification box and its child elements.

  1. /**
  2. * Custom Simple Toast Stylesheet
  3. * Developed by: oretnom23
  4. * Please load this file with the CustomToast.js file and Google Icons
  5. */
  6. .toast {
  7. position: fixed;
  8. top: 25px;
  9. right: 25px;
  10. width: 300px;
  11. background: #fff;
  12. padding: 0.5em 0.35em;
  13. border-left: 4px solid #b7b7b7;
  14. border-radius: 4px;
  15. box-shadow: -1px 1px 10px #00000057;
  16. z-index: 1023;
  17. animation: leftToRight .5s ease-in-out forwards;
  18. transform: translateX(110%);
  19. }
  20. .toast.closing{
  21. animation: RightToLeft .5s ease-in-out forwards;
  22. }
  23. .toast-progress {
  24. position: absolute;
  25. display: block;
  26. bottom: 0;
  27. left: 0;
  28. height: 4px;
  29. width: 100%;
  30. background: #b7b7b7;
  31. animation: Toastprogress 3s ease-in-out forwards;
  32. }
  33. @keyframes leftToRight {
  34. 0%{
  35. transform: translateX(110%);
  36. }
  37. 75%{
  38. transform: translateX(-10%);
  39. }
  40. 100%{
  41. transform: translateX(0%);
  42. }
  43. }
  44. @keyframes RightToLeft {
  45. 0%{
  46. transform: translateX(0%);
  47. }
  48. 25%{
  49. transform: translateX(-10%);
  50. }
  51. 100%{
  52. transform: translateX(110%);
  53. }
  54. }
  55. @keyframes Toastprogress {
  56. 0%{
  57. width: 100%;
  58. }
  59. 100%{
  60. width: 0%;
  61. }
  62. }
  63. button.toast-close-btn {
  64. outline: none;
  65. background: none;
  66. border: none;
  67. float: right;
  68. cursor: pointer;
  69. }
  70. button.toast-close-btn>span,
  71. button.toast-close-btn>i{
  72. font-size:1.2rem;
  73. color:#747474;
  74. font-weight: 500;
  75. }
  76. button.toast-close-btn:hover>span,
  77. button.toast-close-btn:hover>i{
  78. color:#585858;
  79. }
  80. .toast-content-wrapper {
  81. display: flex;
  82. /* margin-top: 1rem; */
  83. justify-content: space-evenly;
  84. align-items: start;
  85. }
  86. .toast-icon {
  87. padding: 0.35rem 0.5rem;
  88. }
  89. .toast-message {
  90. font-size: .9rem;
  91. color: #424242;
  92. padding: 0.15rem 0.5rem;
  93. }
  94.  
  95. /* success toast */
  96. .toast.toast-success{
  97. border-color: #03e05f;
  98. }
  99. .toast.toast-success .toast-progress{
  100. background-color: #088d3f;
  101. }
  102. .toast.toast-success .toast-icon>span,
  103. .toast.toast-success .toast-icon>i{
  104. color: #26c468;
  105. }
  106.  
  107. /* danger toast */
  108. .toast.toast-danger{
  109. border-color: #ff3f3f;
  110. }
  111. .toast.toast-danger .toast-progress{
  112. background-color: #d63030;
  113. }
  114. .toast.toast-danger .toast-icon>span,
  115. .toast.toast-danger .toast-icon>i{
  116. color: #ff3f3f;
  117. }
  118.  
  119. /* info toast */
  120. .toast.toast-info{
  121. border-color: #5fbdfc;
  122. }
  123. .toast.toast-info .toast-progress{
  124. background-color: #4b9fd8;
  125. }
  126. .toast.toast-info .toast-icon>span,
  127. .toast.toast-info .toast-icon>i{
  128. color: #5fbdfc;
  129. }
  130.  
  131. /* warning toast */
  132. .toast.toast-warning{
  133. border-color: #c99e25;
  134. }
  135. .toast.toast-warning .toast-progress{
  136. background-color: #bb9223;
  137. }
  138. .toast.toast-warning .toast-icon>span,
  139. .toast.toast-warning .toast-icon>i{
  140. color: #c99e25;
  141. }

Toast Notification Script

Next, here's the JavaScript file script known as customToast.js. The file contains the toast notification class and object.

  1. /**
  2. * Custom Simple Toast Class
  3. * Developed by: oretnom23
  4. * Please load this file with the CustomToast.css file and Google Icons
  5. */
  6. class CustomToast{
  7. // Toast Box variable
  8. toastBox;
  9. // Toast Box Duration variable
  10. duration;
  11. // Valid Toast Icons
  12. toastIcon = {
  13. success: `<span class="material-symbols-outlined">task_alt</span>`,
  14. danger: `<span class="material-symbols-outlined">error</span>`,
  15. warning: `<span class="material-symbols-outlined">warning</span>`,
  16. info: `<span class="material-symbols-outlined">info</span>`
  17. };
  18. show(message="Sample Message", toastType="info", duration = 5000){
  19. // Check if toast type is valid, otherwise make info toast as the default
  20. if(!Object.keys(this.toastIcon).includes(toastType))
  21. toastType = `info`;
  22. // Creatign the Toast Box Element
  23. this.toastBox = document.createElement('div')
  24. // Adding .toast class to Toast Box Element
  25. this.toastBox.classList.add('toast', `toast-${toastType}`)
  26. // Toast box content
  27. this.toastBox.innerHTML = `<button class="toast-close-btn"><span class="material-symbols-outlined">close</span></button>
  28. <div class="toast-content-wrapper">
  29. <div class="toast-icon">
  30. ${this.toastIcon[toastType]}
  31. </div>
  32. <div class="toast-message">${message}</div>
  33. <div class="toast-progress"></div>
  34. </div>`;
  35. // Set Toast Duration
  36. this.duration = duration
  37. // Update Toast Duration
  38. this.toastBox.querySelector('.toast-progress').style.animationDuration = `${this.duration / 1000}s`
  39.  
  40. // Remove Current Toast Notification if Exists
  41. if(document.body.querySelector('.toast') != null)
  42. document.body.querySelector('.toast').remove();
  43. // Append New Toast Notification to the document
  44. document.body.appendChild(this.toastBox)
  45. // Trigger closing duration
  46. this.triggerClose()
  47. // When Close Icon/Button is clicked event listener
  48. this.toastBox.querySelector('.toast-close-btn').addEventListener('click', e=>{
  49. e.preventDefault()
  50. // Trigger immediate closing
  51. this.triggerClose(0)
  52. })
  53. }
  54. triggerClose(closeDuration = null){
  55. // Set toast duration as the closing delay if the closing duration value is null
  56. if(closeDuration == null){
  57. closeDuration=this.duration
  58. }
  59. setTimeout(()=>{
  60. // adding closing class for closing animation
  61. this.toastBox.classList.add('closing')
  62. // trigger removing the toast notification
  63. this.closeToast()
  64. },closeDuration)
  65. }
  66. closeToast(){
  67. // Set removing toast delay
  68. setTimeout(()=>{
  69. this.toastBox.remove();
  70. }, 500)
  71. }
  72. }

Buttons Action Scripts

Finally, here's the JavaScript file script known as script.js. The file contains the codes that make the buttons functional which are triggering the toast notifications to show.

  1. const successToast = document.querySelector('.btn-toast.btn-success-toast')
  2. const infoToast = document.querySelector('.btn-toast.btn-info-toast')
  3. const dangerToast = document.querySelector('.btn-toast.btn-danger-toast')
  4. const warningToast = document.querySelector('.btn-toast.btn-warning-toast')
  5.  
  6. successToast.addEventListener('click', e=>{
  7. e.preventDefault()
  8. new CustomToast().show(`This is only a sample success toast notification.`, 'success', 10000)
  9. })
  10.  
  11. infoToast.addEventListener('click', e=>{
  12. e.preventDefault()
  13. new CustomToast().show(`This is only a sample info toast notification.`, 'info', 10000)
  14. })
  15.  
  16. dangerToast.addEventListener('click', e=>{
  17. e.preventDefault()
  18. new CustomToast().show(`This is only a sample danger toast notification.`, 'danger', 10000)
  19. })
  20.  
  21. warningToast.addEventListener('click', e=>{
  22. e.preventDefault()
  23. new CustomToast().show(`This is only a sample warning toast notification.`, 'warning', 10000)
  24. })

Snapshots

Here are some snapshots of the overall result of the web page script I have provided above:

Page UI

Toast Notification using HTML, CSS, and JavaScript

Success Toast

Toast Notification using HTML, CSS, and JavaScript

Info Toast

Toast Notification using HTML, CSS, and JavaScript

Warning Toast

Toast Notification using HTML, CSS, and JavaScript

Danger Toast

Toast Notification using HTML, CSS, and JavaScript

There you go! I have also provided the complete source code zip file of the scripts that I provided on this site and it is free to download. The download button can be found below this tutorial's content. Feel free to download and modify the source code the way you wanted.

DEMO VIDEO

You can also use the Toast Notification JS Class that I created for your own projects. Just copy and load the customToast.css and customToast.js. Note that this Toast notification is using Google Icons which means you have to load also the Google Icons asset.

That's it! I hope this Creating a Custom Toast Notification using HTML, CSS, and JS Tutorial will help you with what you are looking for and you'll find it useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment