Published on

3 Things You Must Know To Create A Tailwind CSS Modal With Tailwind CSS

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

Use this responsive modal component to show interactive information to your website users flowbite.com/docs/components/modal/

Why use Tailwind CSS to create a Tailwind CSS Modal ui component?

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

The preview of Tailwind CSS Modal ui component

Free download of the Tailwind CSS Modal's source code

The source code of Tailwind CSS Modal ui component

<!-- This is an example component -->
<div class="max-w-2xl mx-auto">

    <!-- Modal toggle -->
    <button class="block text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" type="button" data-modal-toggle="default-modal">
    Toggle modal
    </button>

    <!-- Main modal -->
    <div id="default-modal" data-modal-show="true" aria-hidden="true" class="hidden overflow-x-hidden overflow-y-auto fixed h-modal md:h-full top-4 left-0 right-0 md:inset-0 z-50 justify-center items-center">
        <div class="relative w-full max-w-2xl px-4 h-full md:h-auto">
            <!-- Modal content -->
            <div class="bg-white rounded-lg shadow relative dark:bg-gray-700">
                <!-- Modal header -->
                <div class="flex items-start justify-between p-5 border-b rounded-t dark:border-gray-600">
                    <h3 class="text-gray-900 text-xl lg:text-2xl font-semibold dark:text-white">
                        Terms of Service
                    </h3>
                    <button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-toggle="default-modal">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>  
                    </button>
                </div>
                <!-- Modal body -->
                <div class="p-6 space-y-6">
                    <p class="text-gray-500 text-base leading-relaxed dark:text-gray-400">
                        With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply.
                    </p>
                    <p class="text-gray-500 text-base leading-relaxed dark:text-gray-400">
                        The European Union’s General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them.
                    </p>
                </div>
                <!-- Modal footer -->
                <div class="flex space-x-2 items-center p-6 border-t border-gray-200 rounded-b dark:border-gray-600">
                    <button data-modal-toggle="default-modal" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">I accept</button>
                    <button data-modal-toggle="default-modal" type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:ring-gray-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600">Decline</button>
                </div>
            </div>
        </div>
    </div>

    <p class="mt-5">This modal element is part of a larger, open-source library of Tailwind CSS components. Learn more by going to the official <a class="text-blue-600 hover:underline" href="https://flowbite.com/docs/getting-started/introduction/" target="_blank">Flowbite Documentation</a>.</p>
</div>

<script src="https://unpkg.com/[email protected]/dist/flowbite.js"></script>

How to create a Tailwind CSS Modal 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 create a Tailwind CSS Modal component

  • max-w-2xl
  • mx-auto
  • block
  • text-white
  • bg-blue-700
  • hover:bg-blue-800
  • text-sm
  • px-5
  • py-2.5
  • text-center
  • dark:bg-blue-600
  • hidden
  • overflow-y-auto
  • fixed
  • h-modal
  • md:h-full
  • top-4
  • left-0
  • right-0
  • z-50
  • relative
  • w-full
  • px-4
  • h-full
  • md:h-auto
  • bg-white
  • dark:bg-gray-700
  • flex
  • p-5
  • border-b
  • dark:border-gray-600
  • text-gray-900
  • text-xl
  • lg:text-2xl
  • dark:text-white
  • text-gray-400
  • bg-transparent
  • hover:bg-gray-200
  • hover:text-gray-900
  • p-1.5
  • ml-auto
  • inline-flex
  • w-5
  • h-5
  • p-6
  • text-gray-500
  • text-base
  • dark:text-gray-400
  • border-t
  • border-gray-200
  • hover:bg-gray-100
  • focus:z-10
  • dark:text-gray-300
  • dark:border-gray-500
  • mt-5
  • text-blue-600

56 steps to create a Tailwind CSS Modal component with Tailwind CSS

  1. Set the maximum width/height of an element using the max-w-2xl utilities.

  2. Control the horizontal margin of an element to auto using the mx-auto utilities.

  3. Use inline utilities to put the element on its own line and fill its parent.

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

  5. Control the background color of an element to blue-700 using the bg-blue-700 utilities.

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

  7. Control the text color of an element to sm using the text-sm utilities.

  8. Control the horizontal padding of an element to 1.25rem using the px-5 utilities.

  9. Control the vertical padding of an element to 2.5 using the py-2.5 utilities.

  10. Control the text color of an element to center using the text-center utilities.

  11. Control the background color of an element to blue-600 using the dark:bg-blue-600 utilities in dark theme.

  12. Use hidden to set an element to display: none and remove it from the page layout.

  13. Use overflow-y-auto to allow vertical scrolling if needed.

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

  15. Use h-modal to set an element to a fixed height(modal).

  16. Use h-full to set an element’s height to 100% of its parent at only medium screen sizes, as long as the parent has a defined height.

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

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

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

  20. 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.

  21. Use relative to position an element according to the normal flow of the document.

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

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

  24. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

  25. Use md:h-auto to set an element to a fixed height(auto) at only medium screen sizes.

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

  27. Control the background color of an element to gray-700 using the dark:bg-gray-700 utilities in dark theme.

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

  29. Control the padding on all sides of an element to 1.25rem using the p-5 utilities.

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

  31. Control the border color of an element to gray-600 using the dark:border-gray-600 utilities in dark theme.

  32. Control the text color of an element to gray-900 using the text-gray-900 utilities.

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

  34. Control the text color of an element to 2xl at only large screen sizes using the lg:text-2xl utilities.

  35. Control the text color of an element to white in dark theme using the dark:text-white utilities.

  36. Control the text color of an element to gray-400 using the text-gray-400 utilities.

  37. Control the background color of an element to transparent using the bg-transparent utilities.

  38. Control the background color of an element to gray-200 using the hover:bg-gray-200 utilities on hover.

  39. Control the text color of an element to gray-900 on hover using the hover:text-gray-900 utilities.

  40. Control the padding on all sides of an element to 1.5 using the p-1.5 utilities.

  41. Control the margin on left side of an element to auto using the ml-auto utilities.

  42. Use inline-flex to create an inline flex container that flows with text.

  43. Use w-5 to set an element to a fixed width(1.25rem).

  44. Use h-5 to set an element to a fixed height(1.25rem).

  45. Control the padding on all sides of an element to 1.5rem using the p-6 utilities.

  46. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  47. Control the text color of an element to base using the text-base utilities.

  48. Control the text color of an element to gray-400 in dark theme using the dark:text-gray-400 utilities.

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

  50. Control the border color of an element to gray-200 using the border-gray-200 utilities.

  51. Control the background color of an element to gray-100 using the hover:bg-gray-100 utilities on hover.

  52. Control the stack order (or three-dimensional positioning) of an element to 10 on focus in Tailwind, regardless of order it has been displayed, using the focus:z-10 utilities.

  53. Control the text color of an element to gray-300 in dark theme using the dark:text-gray-300 utilities.

  54. Control the border color of an element to gray-500 using the dark:border-gray-500 utilities in dark theme.

  55. Control the margin on top side of an element to 1.25rem using the mt-5 utilities.

  56. Control the text color of an element to blue-600 using the text-blue-600 utilities.

Conclusion

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