Published on

How to Make A Modal With Tailwind CSS?

Modal

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 Modal ui component

Modal for tailwindcss

Why use Tailwind CSS to build a Modal ui component?

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

The preview of Modal ui component

Free download of the Modal's source code

The source code of Modal ui component

<button class="bg-blue-600 hover:bg-blue-700 px-3 py-1 rounded text-white m-5 show-modal">show modal</button>

  <div class="modal h-screen w-full fixed left-0 top-0 flex justify-center items-center bg-black bg-opacity-50">
    <!-- modal -->
    <div class="bg-white rounded shadow-lg w-10/12 md:w-1/3">
      <!-- modal header -->
      <div class="border-b px-4 py-2 flex justify-between items-center">
        <h3 class="font-semibold text-lg">Modal Title</h3>
        <button class="text-black close-modal">&cross;</button>
      </div>
      <!-- modal body -->
      <div class="p-3">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Rerum, delectus cumque fugiat nemo ducimus quae deserunt cupiditate sapiente incidunt aut accusantium dolore assumenda vitae similique, exercitationem voluptatum praesentium laboriosam nam.
      </div>
      <div class="flex justify-end items-center w-100 border-t p-3">
        <button class="bg-red-600 hover:bg-red-700 px-3 py-1 rounded text-white mr-1 close-modal">Cancel</button>
        <button class="bg-blue-600 hover:bg-blue-700 px-3 py-1 rounded text-white">Oke</button>
      </div>
    </div>
  </div>
  
  <script>
    const modal = document.querySelector('.modal');

    const showModal = document.querySelector('.show-modal');
    const closeModal = document.querySelectorAll('.close-modal');

    showModal.addEventListener('click', function (){
      modal.classList.remove('hidden')
    });

    closeModal.forEach(close => {
      close.addEventListener('click', function (){
        modal.classList.add('hidden')
      });
    });
  </script>

How to build a Modal with Tailwind CSS?

Install tailwind css of verion 2.0.3

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

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

All the unility class needed to build a Modal component

  • bg-blue-600
  • hover:bg-blue-700
  • px-3
  • py-1
  • text-white
  • m-5
  • h-screen
  • w-full
  • fixed
  • left-0
  • top-0
  • flex
  • bg-black
  • bg-opacity-50
  • bg-white
  • w-10/12
  • md:w-1/3
  • border-b
  • px-4
  • py-2
  • text-lg
  • text-black
  • p-3
  • w-100
  • border-t
  • bg-red-600
  • hover:bg-red-700
  • mr-1

28 steps to build a Modal component with Tailwind CSS

  1. Control the background color of an element to blue-600 using the bg-blue-600 utilities.

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

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

  4. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

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

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

  7. Use h-screen to make an element span the entire height of the viewport.

  8. Use w-full to set an element to a 100% based width.

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

  10. Use the left-0 utilities to set the left position of a positioned element to 0rem.

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

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

  13. Control the background color of an element to black using the bg-black utilities.

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

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

  16. Use w-10/12 to set an element to a fixed width(10/12).

  17. Use md:w-1/3 to set an element to a fixed width(1/3) at only medium screen sizes.

  18. Control the border color of an element to b using the border-b utilities.

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

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

  21. Control the text color of an element to lg using the text-lg utilities.

  22. Control the text color of an element to black using the text-black utilities.

  23. Control the padding on all sides of an element to 0.75rem using the p-3 utilities.

  24. Use w-100 to set an element to a fixed width(25rem).

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

  26. Control the background color of an element to red-600 using the bg-red-600 utilities.

  27. Control the background color of an element to red-700 using the hover:bg-red-700 utilities on hover.

  28. Control the margin on right side of an element to 0.25rem using the mr-1 utilities.

Conclusion

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