WindFrame is here: Design, prototype and code
sale icon End of Summer Sale ūüéĀ Get Contrast PRO UI library + admin template with 5 admin dahsboards, 23 pages and 10000+ components at 60% off ūüéČ

How to create Tailwind CSS Modal


By Emmanuel Chinonso

Technical Writer

When building web applications, one of the most important thing a developer must have in mind is user experience. One of the ways a developer can do this is to pay attention to how user interact with his/her applications.

Table of content

  • Introduction
  • What is a modal dialog
  • Prerequisites
  • Add Tailwind CSS to your Project
  • Create the button
  • Adding an overlay effect
  • Create the modal dialog
  • Adding functionality
  • Conclusion


Tailwind CSS is a self descriibed utility-first CSS framework. It is used for building beautiful user interfaces. It is non-opionated therefore easy to style.

If you do not have Tailwind CSS already installed in your HTML project, check out our article here to figure out how to install the Tailwind CSS and use it in your HTML project.

In this article, we are going to build a Tailwind CSS Modal dialog.

What is a modal dialog

A modal is simply a pop up window that appears on top of a main content of a web application. Modal dialog always seeks for user interaction. there can also be used to give critical warning to users to prevent errors. Throughout the Tailwind CSS tutorial we are going to use Tailwind CSS, HTML and JavaScript to build our modal dialog. The Tailwind CSS modal dialog will look like the image below


  • Latest version of Tailwind CSS
  • Knowledge of HTML
  • Knowledge of CSS
  • Knowledge of JavaScript

Add Tailwind CSS to your Project

Before we can go on, it is assumed you have Tailwind CSS installed on your system. If you haven’t, you can check out how to do this on our previous tailwind CSS tutorial. Creating a button The first step after installation of the tailwind CSS is to link the html to the tailwind CSS like the code below.


1<!DOCTYPE html>
2<html lang="en">
5    <meta charset="UTF-8">
6    <meta http-equiv="X-UA-Compatible" content="IE=edge">
7    <meta name="viewport" content="width=device-width, initial-scale=1.0">
8    <title>modal dialog</title>
9    <link rel="stylesheet" href="style.css">

Creating a button

Once your Tailwind CSS stylesheet is correctly linked. We can go ahead to create a simple button first. To do this, we wrapped our button under a ‚Äúdiv‚ÄĚ container and added instruction on our page to click on the button. You can see the code below


1<div class="w-80 mx-auto mt-5 p-7">
2    <p class="text-2xl font-medium text-gray-800">
3       click here to open the modal
4    </p>
5    <button
6     class="bg-purple-500 text-white rounded-md px-8 py-2 text-base font-medium hover:bg-blue-600 
7 focus:outline-none focus:ring-2 focus:ring-green-300"
8    id="open-btn">
9     Open Modal
10     </button>

In the code above, we created the button and styled it with Tailwind CSS styles. Our modal will look like the image below at this point. Our modal will look like the image below at this point.

Tailwind CSS Modal

Overlap effect

We can go ahead to create an overlap effect. Overlap effect makes sure the user attends to the dialog box first before continuing with the site.


1 <div class="fixed hidden insert-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full" id="modal">

In the code above, we used some Tailwind CSS style to render an overlap effect on our page.

Creating the modal dialog

We initially stated at the beginning of our tutorial how important the modal dialog is and what it does for the user. In our case we created a modal dialog that tells the user whether he /she has successfully created an account.


1<div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
2            <div class="mt-3 text-center">
3                <div class="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-purple-100">
4                    <svg class="h-6 w-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"
5                        xmlnx="http://www.w.org/2000/svg">
7                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
8                    </svg>
9                </div>
10                <h3 class="text-lg leading-6 font-medium text-gray-900">Successfull</h3>
11                <div class="mt-2 px-7 py-3">
12                    <p class="text-sm text-gray-500">Account has been Successfull registered.</p>
13                </div>
14                <div class="items-center px-4 py-3">
15                    <button id="ok-btn"
16                        class="px-4 py-2 bg-purple-500 text-white 
17 text-base font-medium rounded-md w-full 
18 shadow-sm hover:bg-purple-600 focus:outline-none focus:ring-2 focus:ring-purple-300">
19                        OK
20                    </button>
21                </div>
22            </div>
23        </div>

In the code above, we first started by styling the head of our modal dialog. We did this by adding classes that will enable us align our elements inside the parent ‚Äúdiv‚ÄĚ. we went ahead to create child ‚Äúdiv‚ÄĚ which contained an ‚Äúsvg‚ÄĚ element. Some classes we added to our modal dialog include

  • text-center which center all the text present
  • font-medium which changes the font weight to 500
  • px-4 and py-3 which adds padding of 16px to the y-axis and 8px to the y-axis
  • focus:ring-2 which creates an outline ring on the button
  • focus: ring-purple-300 which creates a purple outline ring
  • hover: bg-purple-600 simply adds a hover effect on the button by changing the background color.

We have successfully styled our Tailwind CSS modal dialog. We can now go ahead and add some functionality to our Tailwind CSS modal

Functionality of the Tailwind CSS Modal dialog

We initially added some IDs to the buttons when we were creating the style for the modal dialog and the instruction button. We are going to go ahead and add functionality to this buttons by first grabbling all our IDs. You can write the code inside the script tag at the bottom of our page.


2        let modal = document.getElementById("modal");
3        let btn = document.getElementById("open-btn");
4        let button = document.getElementById("ok-btn");

In the code above, we used the ‚Äúdocument.getElementById‚ÄĚ to grab the IDs we have given to the buttons on our page. We can now make the buttons do what we want. First we want our ‚Äúopen modal‚ÄĚ button to open the modal dialog when clicked and our ‚ÄúOK ‚Äú button on the Tailwind CSS modal dialog to close when clicked. To do this we have to create a function for the both button to do this. The functions are written below


1btn.onclick = function () {
2            modal.style.display = "block";
3        }
5        button.onclick = function () {
6            modal.style.display = "none";
7        }

Moreover, we need the button to close too when the user clicks anywhere on the window. So we are going to create a function that will do this for us


1window.onclick = function (event) {
2            if (event.target == modal) {
3                modal.style.display = "none";
4            }
5        }
6    </script>

With the code above, our Tailwind CSS modal dialog is ready. It should look like the image below

Tailwind CSS Modal


In our tailwind CSS tutorial today, we went through how to use tailwind CSS to create modal dialog. In the tutorial we walked through the process of creating the modal dialog from its styling to creating their functionality. These are one of many ways you can practice your skills with Tailwind CSS. I hope you enjoyed the tutorial.

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro


Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts