Published on

How To Make A Simple toast With Tailwind CSS In 6 Easy Steps?

Simple toast

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 Simple toast ui component

A simple toast with its functionality. a user can click the toast to close it or it will close automatically after 5 seconds.

Why use Tailwind CSS to make a Simple toast ui component?

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

The preview of Simple toast ui component

Free download of the Simple toast's source code

The source code of Simple toast ui component

<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/boxicons.min.css" />
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

<!-- page -->
<main x-data="app" class="min-w-screen flex min-h-screen items-center justify-center">
  <!-- toast -->
  <button type="button" @click="closeToast()" x-show="open" x-transition.duration.300ms class="fixed top-4 right-4 z-50 rounded-md bg-green-500 px-4 py-2 text-white transition hover:bg-green-600">
    <div class="flex items-center space-x-2">
      <span class="text-3xl"><i class="bx bx-check"></i></span>
      <p class="font-bold">Item Created Successfully!</p>
    </div>
  </button>

  <!-- trigger button -->
  <button type="button" @click="openToast()" class="rounded-md bg-blue-500 px-4 py-2 font-bold text-white transition hover:bg-blue-600">Launch toast</button>
</main>

<script>
  let timer;

  document.addEventListener("alpine:init", () => {
      Alpine.data("app", () => ({
          open: false,

          openToast() {
              if (this.open) return;
              this.open = true;

              // reset time to 0 second
              clearTimeout(timer);

              // auto close toast after 5 seconds
              timer = setTimeout(() => {
                  this.open = false;
              }, 5000);
          },

          closeToast() {
              this.open = false;
          },
      }));
  });
</script>

How to make a Simple toast with Tailwind CSS?

Install tailwind css of verion 3.0.18

Use the script html tag to import the script of Tailwind CSS of the version 3.0.18

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to make a Simple toast component

  • min-w-screen
  • flex
  • min-h-screen
  • fixed
  • top-4
  • right-4
  • z-50
  • bg-green-500
  • px-4
  • py-2
  • text-white
  • hover:bg-green-600
  • text-3xl
  • bg-blue-500
  • hover:bg-blue-600

15 steps to make a Simple toast component with Tailwind CSS

  1. Set the minimum width/height of an element using the min-w-screen utilities.

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

  3. Set the minimum width/height of an element using the min-h-screen utilities.

  4. Use fixed to position an element relative to the browser window.

  5. Use the top-4 utilities to set the top position of a positioned element to 1rem.

  6. Use the right-4 utilities to set the right position of a positioned element to 1rem.

  7. Control the stack order (or three-dimensional positioning) of an element to 50 in Tailwind, regardless of order it has been displayed, using the z-50 utilities.

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

  9. Control the horizontal padding of an element to 1rem using the px-4 utilities.

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

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

  12. Control the background color of an element to green-600 using the hover:bg-green-600 utilities on hover.

  13. Control the text color of an element to 3xl using the text-3xl utilities.

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

  15. Control the background color of an element to blue-600 using the hover:bg-blue-600 utilities on hover.

Conclusion

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