Published on

A Complete Guide To Create A Toast Notification With Tailwind CSS

Tags
Toast Notification

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Toast Notification ui component

Toast notification with mode success, info, warning, and danger

Why use Tailwind CSS to build a Toast Notification ui component?

  • It can make the building process of Toast Notification ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Toast Notification component file.

The preview of Toast Notification ui component

Free download of the Toast Notification's source code

The source code of Toast Notification ui component

<!-- Toast Container -->
  <!-- put taost notification in here , to cope when the toast more than one -->
  <!-- class="absolute right-0 top-0 m-5" For put toast on top right -->
  <div class="flex flex-col jusctify-center">

    <!-- Toast Notification Success-->
    <div class="flex items-center bg-green-500 border-l-4 border-green-700 py-2 px-3 shadow-md mb-2">
       <!-- icons -->
      <div class="text-green-500 rounded-full bg-white mr-3">
        <svg width="1.8em" height="1.8em" viewBox="0 0 16 16" class="bi bi-check" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path fill-rule="evenodd" d="M10.97 4.97a.75.75 0 0 1 1.071 1.05l-3.992 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.236.236 0 0 1 .02-.022z"/>
        </svg>
      </div>
      <!-- message -->
      <div class="text-white max-w-xs ">
        ini pesan ketika sukses
      </div>
    </div>

    <!-- Toast Notification Info -->
    <div class="flex items-center bg-blue-400 border-l-4 border-blue-700 py-2 px-3 shadow-md mb-2">
      <!-- icons -->
     <div class="text-blue-500 rounded-full bg-white mr-3">
      <svg width="1.8em" height="1.8em" viewBox="0 0 16 16" class="bi bi-info" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
        <path d="M8.93 6.588l-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.808 1.319.545 0 1.178-.252 1.465-.598l.088-.416c-.2.176-.492.246-.686.246-.275 0-.375-.193-.304-.533L8.93 6.588z"/>
        <circle cx="8" cy="4.5" r="1"/>
      </svg>
     </div>
     <!-- message -->
     <div class="text-white max-w-xs ">
       ini pesan ketika ada informasi
     </div>
   </div>

    <!-- Toast Notification Warning -->
    <div class="flex items-center bg-orange-400 border-l-4 border-orange-700 py-2 px-3 shadow-md mb-2">
       <!-- icons -->
      <div class="text-orange-500 rounded-full bg-white mr-3">
        <svg width="1.8em" height="1.8em" viewBox="0 0 16 16" class="bi bi-exclamation" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path d="M7.002 11a1 1 0 1 1 2 0 1 1 0 0 1-2 0zM7.1 4.995a.905.905 0 1 1 1.8 0l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 4.995z"/>
        </svg>
      </div>
      <!-- message -->
      <div class="text-white max-w-xs ">
        ini pesan ketika ada warning
      </div>
    </div>

    <!-- Toast Notification Danger -->
    <div class="flex items-center bg-red-500 border-l-4 border-red-700 py-2 px-3 shadow-md mb-2" >
      <!-- icons -->
     <div class="text-red-500 rounded-full bg-white mr-3">
      <svg width="1.8em" height="1.8em" viewBox="0 0 16 16" class="bi bi-x" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
        <path fill-rule="evenodd" d="M11.854 4.146a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708-.708l7-7a.5.5 0 0 1 .708 0z"/>
        <path fill-rule="evenodd" d="M4.146 4.146a.5.5 0 0 0 0 .708l7 7a.5.5 0 0 0 .708-.708l-7-7a.5.5 0 0 0-.708 0z"/>
      </svg>
     </div>
     <!-- message -->
     <div class="text-white max-w-xs ">
       ini pesan ketika ada error
     </div>
   </div>

How to build a Toast Notification with Tailwind CSS?

Install tailwind css of verion 1.9.6

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.9.6

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to build a Toast Notification component

  • absolute
  • right-0
  • top-0
  • m-5
  • flex
  • flex-col
  • bg-green-500
  • border-l-4
  • border-green-700
  • py-2
  • px-3
  • mb-2
  • text-green-500
  • bg-white
  • mr-3
  • text-white
  • max-w-xs
  • bg-blue-400
  • border-blue-700
  • text-blue-500
  • bg-orange-400
  • border-orange-700
  • text-orange-500
  • bg-red-500
  • border-red-700
  • text-red-500

26 steps to build a Toast Notification component with Tailwind CSS

  1. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  2. Use the right-0 utilities to set the right position of a positioned element to 0rem.

  3. Use the top-0 utilities to set the top position of a positioned element to 0rem.

  4. Control the margin on all sides of an element to 1.25rem using the m-5 utilities.

  5. Use flex to create a block-level flex container.

  6. Use flex to create a block-level flex container.

  7. Control the background color of an element to green-500 using the bg-green-500 utilities.

  8. Control the border color of an element to l-4 using the border-l-4 utilities.

  9. Control the border color of an element to green-700 using the border-green-700 utilities.

  10. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  11. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  12. Control the margin on bottom side of an element to 0.5rem using the mb-2 utilities.

  13. Control the text color of an element to green-500 using the text-green-500 utilities.

  14. Control the background color of an element to white using the bg-white utilities.

  15. Control the margin on right side of an element to 0.75rem using the mr-3 utilities.

  16. Control the text color of an element to white using the text-white utilities.

  17. Set the maximum width/height of an element using the max-w-xs utilities.

  18. Control the background color of an element to blue-400 using the bg-blue-400 utilities.

  19. Control the border color of an element to blue-700 using the border-blue-700 utilities.

  20. Control the text color of an element to blue-500 using the text-blue-500 utilities.

  21. Control the background color of an element to orange-400 using the bg-orange-400 utilities.

  22. Control the border color of an element to orange-700 using the border-orange-700 utilities.

  23. Control the text color of an element to orange-500 using the text-orange-500 utilities.

  24. Control the background color of an element to red-500 using the bg-red-500 utilities.

  25. Control the border color of an element to red-700 using the border-red-700 utilities.

  26. Control the text color of an element to red-500 using the text-red-500 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to build a Toast Notification components, learn and follow along to implement your own components.