- Published on
Beginners Guide: Create A Modal With JavaScript With Tailwind CSS

- What is Tailwind CSS?
- The description of Modal with JavaScript ui component
- Why use Tailwind CSS to make a Modal with JavaScript ui component?
- The preview of Modal with JavaScript ui component
- The source code of Modal with JavaScript ui component
- How to make a Modal with JavaScript with Tailwind CSS?
- Install tailwind css of verion 1.2.0
- All the unility class needed to make a Modal with JavaScript component
- 18 steps to make a Modal with JavaScript 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 with JavaScript ui component
A modal with javascript for tailwind ccss
Why use Tailwind CSS to make a Modal with JavaScript ui component?
- It can make the building process of Modal with JavaScript ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in Modal with JavaScript component file.
The preview of Modal with JavaScript ui component
Free download of the Modal with JavaScript's source code
The source code of Modal with JavaScript ui component
<style>
.modal-bg {
background-color: rgba(0,0,0,0.5);
z-index: -9999;
}
</style>
<button id="openBtn">Open Modal</button>
<div id="modal" class="modal-bg transition-opacity duration-500 opacity-0 pt-16 fixed w-full h-full left-0 z-10 overflow-auto">
<div class="modal-content relative m-auto bg-gray-100 w-4/5 shadow-lg">
<div class="p-4 bg-gray-700 text-white">
<span class="closeBtn float-right text-lg font-bold hover:text-gray-500 no-underline cursor-pointer">×</span>
<h2>Modal Header</h2>
</div>
<div class="p-4">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
<div class="p-4 text-white bg-gray-500">
<h3>Modal Footer</h3>
</div>
</div>
</div>
<script>
var modal = document.querySelector("#modal");
var openBtn = document.querySelector("#openBtn");
var closeBtn = document.querySelector(".closeBtn");
openBtn.onclick = function() {
modal.classList.add('opacity-100');
modal.classList.add('z-50');
}
closeBtn.onclick = function() {
modal.classList.remove('opacity-100');
modal.classList.remove('z-50');
}
window.onclick = function(event) {
if (event.target == modal) {
modal.classList.remove('opacity-100');
modal.classList.remove('z-50');
}
}
</script>
How to make a Modal with JavaScript with Tailwind CSS?
Install tailwind css of verion 1.2.0
Use the link
html tag to import the stylesheet of Tailwind CSS of the version 1.2.0
<link href=https://unpkg.com/tailw[email protected]/dist/tailwind.min.css rel="stylesheet">
All the unility class needed to make a Modal with JavaScript component
pt-16
fixed
w-full
h-full
left-0
z-10
overflow-auto
relative
m-auto
bg-gray-100
w-4/5
p-4
bg-gray-700
text-white
float-right
text-lg
hover:text-gray-500
bg-gray-500
18 steps to make a Modal with JavaScript component with Tailwind CSS
Control the padding on top side of an element to 4rem using the
pt-16
utilities.Use
fixed
to position an element relative to the browser window.Use
w-full
to set an element to a 100% based width.Use
h-full
to set an element’s height to 100% of its parent, as long as the parent has a defined height.Use the
left-0
utilities to set the left position of a positioned element to 0rem.Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the
z-10
utilities.Use
overflow-auto
to add scrollbars to an element in the event that its content overflows the bounds of that element. Unlike.overflow-scroll
, which always shows scrollbars, this utility will only show them if scrolling is necessary.Use
relative
to position an element according to the normal flow of the document.Control the margin on all sides of an element to auto using the
m-auto
utilities.Control the background color of an element to gray-100 using the
bg-gray-100
utilities.Use
w-4/5
to set an element to a fixed width(4/5).Control the padding on all sides of an element to 1rem using the
p-4
utilities.Control the background color of an element to gray-700 using the
bg-gray-700
utilities.Control the text color of an element to white using the
text-white
utilities.Use
float-right
to float an element to the right of its container.Control the text color of an element to lg using the
text-lg
utilities.Control the text color of an element to gray-500 on hover using the
hover:text-gray-500
utilities.Control the background color of an element to gray-500 using the
bg-gray-500
utilities.
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to make a Modal with JavaScript components, learn and follow along to implement your own components.