- Published on
How to Make A Modal With Tailwind CSS?

- What is Tailwind CSS?
- The description of Modal ui component
- Why use Tailwind CSS to build a Modal ui component?
- The preview of Modal ui component
- The source code of Modal ui component
- How to build a Modal with Tailwind CSS?
- Install tailwind css of verion 2.0.3
- All the unility class needed to build a Modal component
- 28 steps to build a Modal component with Tailwind CSS
- Conclusion
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">✗</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
Control the background color of an element to blue-600 using the
bg-blue-600
utilities.Control the background color of an element to blue-700 using the
hover:bg-blue-700
utilities on hover.Control the horizontal padding of an element to 0.75rem using the
px-3
utilities.Control the vertical padding of an element to 0.25rem using the
py-1
utilities.Control the text color of an element to white using the
text-white
utilities.Control the margin on all sides of an element to 1.25rem using the
m-5
utilities.Use
h-screen
to make an element span the entire height of the viewport.Use
w-full
to set an element to a 100% based width.Use
fixed
to position an element relative to the browser window.Use the
left-0
utilities to set the left position of a positioned element to 0rem.Use the
top-0
utilities to set the top position of a positioned element to 0rem.Use
flex
to create a block-level flex container.Control the background color of an element to black using the
bg-black
utilities.Control the background color of an element to opacity-50 using the
bg-opacity-50
utilities.Control the background color of an element to white using the
bg-white
utilities.Use
w-10/12
to set an element to a fixed width(10/12).Use
md:w-1/3
to set an element to a fixed width(1/3) at only medium screen sizes.Control the border color of an element to b using the
border-b
utilities.Control the horizontal padding of an element to 1rem using the
px-4
utilities.Control the vertical padding of an element to 0.5rem using the
py-2
utilities.Control the text color of an element to lg using the
text-lg
utilities.Control the text color of an element to black using the
text-black
utilities.Control the padding on all sides of an element to 0.75rem using the
p-3
utilities.Use
w-100
to set an element to a fixed width(25rem).Control the border color of an element to t using the
border-t
utilities.Control the background color of an element to red-600 using the
bg-red-600
utilities.Control the background color of an element to red-700 using the
hover:bg-red-700
utilities on hover.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.