{"componentChunkName":"component---src-templates-blog-js","path":"/blog/how-to-create-tailwind-css-modal/","result":{"data":{"site":{"siteMetadata":{"title":"Resources to help developers | Contrast | Devwares"}},"mdx":{"id":"2978fc41-8153-5500-b364-16114ec1cb43","excerpt":"Tailwind Modals Modal windows or pop-ups are a popular UI element used in modern web design to provide extra information or interaction. They are an effective…","body":"var _excluded = [\"components\"];\nfunction _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }\nfunction _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }\nfunction _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }\n/* @jsxRuntime classic */\n/* @jsx mdx */\n\nvar _frontmatter = {\n  \"title\": \"How to create Tailwind CSS Modal:A Step-by-Step Guide\",\n  \"lastUpdated\": \"17 May 2024\",\n  \"date\": \"2021-01-10T02:16:04+01:00\",\n  \"metaTitle\": \"How to create Tailwind CSS Modal\",\n  \"metaDescription\": \"Tailwind CSS Modals are effective way of grabbing user attention without taking them away from the current page. In this article, we will guide you on how to create a modal using Tailwind CSS, a popular utility-first CSS framework.\",\n  \"type\": \"blog\",\n  \"author\": \"Emmanuel Chinonso\",\n  \"role\": \"Technical Writer/Web developer\",\n  \"authorImage\": \"/blog/author/author_emmanuel.jpg\",\n  \"image\": \"./_/images/create-tailwind-css-modal.png\",\n  \"tags\": [\"tailwind css\", \"bootstrap\", \"bootstrap 5 alpha\"],\n  \"previousTitle\": \"How to create Tailwind CSS dropdown\",\n  \"previousPath\": \"how-to-create-tailwind-css-dropdown\",\n  \"nextTitle\": \"How to Create Tailwind CSS table\",\n  \"nextPath\": \"how-to-create-tailwind-css-tables\",\n  \"blogtype\": \"tailwindcss\",\n  \"canonicalUrl\": \"https://windframe.dev/blog/how-to-create-tailwind-css-modal\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n    props = _objectWithoutProperties(_ref, _excluded);\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Tailwind Modals\"), mdx(\"p\", null, \"Modal windows or pop-ups are a popular UI element used in modern web design to provide extra information or interaction. They are an effective way of grabbing user attention without taking them away from the current page. In this article, we will guide you on how to create a modal using Tailwind CSS, a popular utility-first CSS framework.\"), mdx(\"h2\", null, \"Table of content\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Introduction\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"What is a modal dialog\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Prerequisites\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Add Tailwind CSS to your Project\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Linking HTML page to Tailwind CSS\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Create the button\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Adding an overlay effect\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Create the modal dialog\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Adding functionality\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Conclusion\")), mdx(\"h2\", null, \"Introduction\"), mdx(\"p\", null, \"Tailwind CSS is a self-described utility-first CSS framework. It is used for building beautiful user interfaces. It is non-opinionated and therefore easy to style.\"), mdx(\"p\", null, \"If you do not have Tailwind CSS already installed in your HTML project, check out our article \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/blog/how-to-use-tailwind-css-in-HTML\"\n  }, \"here\"), \" to figure out how to install the Tailwind CSS and use it in your projects.\\nApart from learning how to install Tailwind in your project, we are going to build a Tailwind Modal to help you kick off your development.\"), mdx(\"h2\", null, \"Tailwind modal: What is a modal dialog\"), mdx(\"p\", null, \"A modal is simply a popup window that appears on top of the main content of a web application. Modal dialog always seeks user interaction. there can also be used to give critical warnings to users to prevent errors.\"), mdx(\"p\", null, \"Throughout the Tailwind CSS tutorial, we are going to use Tailwind CSS, HTML, and JavaScript to build our Tailwind modal dialog.\\nThe Tailwind CSS modal popup window will look like the image below\"), mdx(\"p\", null, mdx(\"img\", {\n    parentName: \"p\",\n    \"src\": \"/blog/images/create-tailwind-css-modal3.png\",\n    \"alt\": \"Tailwind CSS Modal\"\n  })), mdx(\"h2\", null, \"Prerequisite\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Latest version of Tailwind CSS\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Knowledge of HTML\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Knowledge of CSS\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Knowledge of JavaScript\")), mdx(\"h2\", null, \"Add Tailwind CSS to your Project\"), mdx(\"p\", null, \"Before we can go on, it is assumed you have Tailwind CSS installed on your system. If you haven\\u2019t, you can check out how to do this \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/blog/how-to-use-tailwind-css-in-HTML/\"\n  }, \"How to use TailwindCSS in HTML\"), \".\"), mdx(\"h2\", null, \"Linking HTML page to Tailwind CSS\"), mdx(\"p\", null, \"The first step to building our Tailwind modal after installation of the Tailwind CSS is to link the Html to the Tailwind CSS like the code below.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-html\"\n  }, \"<!DOCTYPE html>\\n<html lang=\\\"en\\\">\\n  <head>\\n    <meta charset=\\\"UTF-8\\\" />\\n    <meta http-equiv=\\\"X-UA-Compatible\\\" content=\\\"IE=edge\\\" />\\n    <meta name=\\\"viewport\\\" content=\\\"width=device-width, initial-scale=1.0\\\" />\\n    <title>modal dialog</title>\\n    <link rel=\\\"stylesheet\\\" href=\\\"style.css\\\" />\\n  </head>\\n</html>\\n\")), mdx(\"h2\", null, \"Creating a button for Tailwind Modal\"), mdx(\"p\", null, \"Once your Tailwind CSS stylesheet is correctly linked. The next thing we are going to do is to create a button for our Tailwind Modal.\\nTo do this, we wrapped our button under a \\u201Cdiv\\u201D container and added instructions on our page to click on the button. You can see the code below\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-html\"\n  }, \"<div class=\\\"w-80 mx-auto mt-5 p-7\\\">\\n  <p class=\\\"text-2xl font-medium text-gray-800\\\">\\n    click here to open the modal\\n  </p>\\n  <button\\n    class=\\\"bg-purple-500 text-white rounded-md px-8 py-2 text-base font-medium hover:bg-blue-600 \\n    focus:outline-none focus:ring-2 focus:ring-green-300\\\"\\n    id=\\\"open-btn\\\"\\n  >\\n    Open Modal\\n  </button>\\n</div>\\n\")), mdx(\"p\", null, \"In the code above, we created the button and styled it with Tailwind CSS styles. At this point, Our Tailwind modal popup will look like the image below.\"), mdx(\"p\", null, mdx(\"img\", {\n    parentName: \"p\",\n    \"src\": \"/blog/images/create-tailwind-css-modal2.png\",\n    \"alt\": \"Tailwind CSS Modal\"\n  })), mdx(\"h2\", null, \"Overlap effect for Tailwind modal\"), mdx(\"p\", null, \"We can go ahead to create an overlapping effect. The overlap effect makes sure the user attends to the Tailwind modal dialog box first before continuing with the site.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-html\"\n  }, \"<div\\n  class=\\\"fixed hidden insert-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full\\\"\\n  id=\\\"modal\\\"\\n></div>\\n\")), mdx(\"p\", null, \"In the code above, we used some Tailwind CSS style to render an overlapping effect on our page.\"), mdx(\"h2\", null, \"Creating the Tailwind CSS modal dialog\"), mdx(\"p\", null, \"We initially stated at the beginning of our tutorial how important the Tailwind modal dialog is and what it does for the user.\\nIn our case, we created a Tailwind CSS modal dialog that tells the user whether he/she has successfully created an account.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-html\"\n  }, \"\\n<div class=\\\"relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white\\\">\\n            <div class=\\\"mt-3 text-center\\\">\\n                <div class=\\\"mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-purple-100\\\">\\n                    <svg class=\\\"h-6 w-6 text-purple-600\\\" fill=\\\"none\\\" stroke=\\\"currentColor\\\"\\nviewBox=\\\"0 0 24 24\\\"\\nxmlnx=\\\"http://www.w.org/2000/svg\\\"><path stroke-linecap=\\\"round\\\" stroke-linejoin=\\\"round\\\" stroke-width=\\\"2\\\"\\nd=\\\"M5 13l4 4L19 7\\\">\\n</path></svg>\\n                </div>\\n                <h3 class=\\\"text-lg leading-6 font-medium text-gray-900\\\">Successfull</h3>\\n                <div class=\\\"mt-2 px-7 py-3\\\">\\n                    <p class=\\\"text-sm text-gray-500\\\">Account has been Successful registered.</p>\\n                </div>\\n                <div class=\\\"items-center px-4 py-3\\\">\\n                    <button id=\\\"ok-btn\\\"\\n                        class=\\\"px-4 py-2 bg-purple-500 text-white\\n                            text-base font-medium rounded-md w-full\\n                            shadow-sm hover:bg-purple-600 focus:outline-none focus:ring-2 focus:ring-purple-300\\\">\\n                        OK\\n                    </button>\\n                </div>\\n            </div>\\n        </div>\\n</div>\\n\\n\")), mdx(\"p\", null, \"In the code above, we first started by styling the head of our Tailwind modal dialog. We did this by adding classes that will enable us to align our elements inside the parent \\u201Cdiv\\u201D. we went ahead to create a child \\u201Cdiv\\u201D which contained an \\u201CSVG\\u201D element.\\nSome classes we added to our modal dialog include\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"text-center\"), \" which centers all the text present\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"font-medium\"), \" which changes the \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://www.devwares.com/tailwindcss/classes/tailwind-font-weight/\"\n  }, \"font weight\"), \" to 500\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"px-4\"), \" and \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"py-3\"), \" which adds padding of 16px to the y-axis and 8px to the y-axis\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"focus:ring-2\"), \" which creates an outline ring on the button\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"focus: ring-purple-300\"), \" which creates a purple outline ring\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"hover: bg-purple-600\"), \" simply adds a hover effect on the button by changing the background color.\")), mdx(\"p\", null, \"We have successfully styled our Tailwind CSS modal dialog. We can now go ahead and add some functionality to our Tailwind CSS modal\"), mdx(\"h2\", null, \"Tailwind Modal: Functionality of the Tailwind CSS Modal dialog\"), mdx(\"p\", null, \"We initially added some IDs to the buttons when we were creating the style for the modal dialog and the instruction button.\\nWe are going to go ahead and add functionality to these buttons by first grabbing all our IDs.\\nYou can write the code inside the script tag at the bottom of our page.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-html\"\n  }, \"<script>\\n  let modal = document.getElementById('modal');\\n  let btn = document.getElementById('open-btn');\\n  let button = document.getElementById('ok-btn');\\n</script>\\n\")), mdx(\"p\", null, \"In the code above, we used the \\u201Cdocument.getElementById\\u201D 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 \\u201Copen modal\\u201D button to open the modal dialog when clicked and our \\u201COK \\u201C button on the Tailwind CSS modal dialog to close when clicked.\\nTo do this we have to create a function for both buttons to do. The functions are written below\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"btn.onclick = function() {\\n  modal.style.display = 'block';\\n};\\n\\nbutton.onclick = function() {\\n  modal.style.display = 'none';\\n};\\n\")), mdx(\"p\", null, \"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.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Code:\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"\\nwindow.onclick = function (event) {\\n            if (event.target == modal) {\\n                modal.style.display = \\\"none\\\";\\n            }\\n        }\\n    </script>\\n\\n\")), mdx(\"p\", null, \"With the code above, our Tailwind CSS modal popup is ready. It should look like the image below.\"), mdx(\"p\", null, mdx(\"img\", {\n    parentName: \"p\",\n    \"src\": \"/blog/images/create-tailwind-css-modal3.png\",\n    \"alt\": \"Tailwind CSS Modal\"\n  })), mdx(\"h2\", null, \"Overview of the Tailwind modal with Html\"), mdx(\"p\", null, \"Although we have gone through the code on how to build your own Html modal using Tailwind CSS, you can also create this modal using react and other frameworks. You can check our article on \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/blog/how-to-create-a-react-modal-using-tailwind-css/\"\n  }, \"how to make the Tailwind modal with React.\")), mdx(\"p\", null, \"However, here is the entire code for the Tailwind modal we created using Html and tailwind.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-html\"\n  }, \"<div class=\\\"w-80 mx-auto mt-5 p-7\\\">\\n    <p class=\\\"text-2xl font-medium text-gray-800\\\">\\n        click here to open the modal\\n    </p>\\n    <button class=\\\"bg-purple-500 text-white rounded-md px-8 py-2 text-base font-medium hover:bg-blue-600\\n    focus:outline-none focus:ring-2 focus:ring-green-300\\\" id=\\\"open-btn\\\">\\n        Open Modal\\n    </button>\\n</div>\\n\\n<!---Modal -->\\n<div class=\\\"fixed hidden insert-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full\\\" id=\\\"modal\\\">\\n<div class=\\\"relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white\\\">\\n    <div class=\\\"mt-3 text-center\\\">\\n        <div class=\\\"mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-purple-100\\\">\\n            <svg class=\\\"h-6 w-6 text-purple-600\\\" fill=\\\"none\\\" stroke=\\\"currentColor\\\" viewBox=\\\"0 0 24 24\\\"\\n                xmlnx=\\\"http://www.w.org/2000/svg\\\">\\n                <path stroke-linecap=\\\"round\\\" stroke-linejoin=\\\"round\\\" stroke-width=\\\"2\\\" d=\\\"M5 13l4 4L19 7\\\">\\n                </path>\\n            </svg>\\n        </div>\\n        <h3 class=\\\"text-lg leading-6 font-medium text-gray-900\\\">Successfull</h3>\\n        <div class=\\\"mt-2 px-7 py-3\\\">\\n            <p class=\\\"text-sm text-gray-500\\\">Account has been Successful registered.</p>\\n        </div>\\n        <div class=\\\"items-center px-4 py-3\\\">\\n            <button id=\\\"ok-btn\\\" class=\\\"px-4 py-2 bg-purple-500 text-white\\n                            text-base font-medium rounded-md w-full\\n                            shadow-sm hover:bg-purple-600 focus:outline-none focus:ring-2 focus:ring-purple-300\\\">\\n                OK\\n            </button>\\n\\n        </div>\\n    </div>\\n\\n</div>\\n</div>\\n</div>\\n<script>\\n  let modal = document.getElementById('modal');\\n   let btn = document.getElementById('open-btn');\\n    let button = document.getElementById('ok-btn');\\n\\n    btn.onclick = function () {\\n            modal.style.display = 'block';\\n        };\\n\\n        button.onclick = function () {\\n            modal.style.display = 'none';\\n        };\\n\\nwindow.onclick = function (event) {\\nif (event.target == modal) {\\nmodal.style.display = \\\"none\\\";\\n}\\n}\\n\\n</script>\\n\")), mdx(\"h2\", null, \"Conclusion\"), mdx(\"p\", null, \"Creating a modal using Tailwind CSS is a straightforward process that can be accomplished with just a few lines of code. By following the steps outlined in this article, you can create a modal that is fully customizable and can be integrated into any website or web application.\"));\n}\n;\nMDXContent.isMDXComponent = true;","frontmatter":{"title":"How to create Tailwind CSS Modal:A Step-by-Step Guide","date":"January 10, 2021","image":{"childImageSharp":{"fluid":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACRklEQVQoz2O4e+30m2e358yYuGHdip/f3v///31yX8eBfXtW3brtsGKj59otHmtAyHPNFtdVm8M27brx7v3F129PPX/pvmYLw/sHF/5/eji1u+HIvs3//376////9KkTDuzfDWS0nThrt3y919qtEM1uqzcHbNh++fXbN99/nHj+0mnlRobXb579+Pp2+qTu7s7Wa5fP/f/3ZfrErn17tgM1P/n8BagaqMcTrBloiuPKjTVHTh5+8ixz9yGQ5ld3zv5+d3/+lE5ODnYDFdkrh3bOnjl1546t/8Gg8dhpu+UbgDa7r94MNAVI2q/YaLVsncuqTUDjGN4+vvrl1d2O+hIrU/1gY8XdU5om9bTt2bHx//9fQM2LrlwP2rQzZ9+RlJ0HUnceyN5zuPjAsbBNO93BAcHw9NGtF09ulhVlR4f4xtvrzKvLa6mr2L513f//v4Gal1++GrppV93R06UHj+ftO1J88Hj+/qPwUGT4/frGl+c3E2Oj7ayta2K9kqLDXNw8b9649P/f9x+f3087cSpg446yg8fLD53wW78NFHJrt0KsBWme2N0UHRasqaJkZ2XmZGdpaKCvp69fX99w+eqVXTu2Zazf4rxmKzCogD50hyEPuGZTI0M5SREVBVkleRllBRl1FQUNZVlVLb3qxva+WdM9V25EVo2GGAzUleSkJRRlJBRlJZXkpFQV5WSlJeKD/POzcl3auj037fZYvRmn5oL0aCF+XhlJMWkJERlJUUVZKVFh/rrS3MkHDzquBMWHB24EAB+JeKInqp8DAAAAAElFTkSuQmCC","aspectRatio":1.7730496453900708,"src":"/static/ddc2f0d97eb59edecc549f64ea7f7722/a4bdf/create-tailwind-css-modal.png","srcSet":"/static/ddc2f0d97eb59edecc549f64ea7f7722/84249/create-tailwind-css-modal.png 250w,\n/static/ddc2f0d97eb59edecc549f64ea7f7722/a4bdf/create-tailwind-css-modal.png 384w","sizes":"(max-width: 384px) 100vw, 384px"}}},"imageExternal":null,"lastUpdated":"17 May 2024","author":"Emmanuel Chinonso","role":"Technical Writer/Web developer","authorImage":"/blog/author/author_emmanuel.jpg","metaTitle":"How to create Tailwind CSS Modal","metaDescription":"Tailwind CSS Modals are effective way of grabbing user attention without taking them away from the current page. In this article, we will guide you on how to create a modal using Tailwind CSS, a popular utility-first CSS framework.","previousTitle":"How to create Tailwind CSS dropdown","previousPath":"how-to-create-tailwind-css-dropdown","nextTitle":"How to Create Tailwind CSS table","nextPath":"how-to-create-tailwind-css-tables","blogtype":"tailwindcss"},"fields":{"slug":"/how-to-create-tailwind-css-modal/"}},"previous":null,"next":null},"pageContext":{"id":"2978fc41-8153-5500-b364-16114ec1cb43"}},"staticQueryHashes":["2619113677","2619113677","2619113677","2619113677","2619113677","2619113677","3309287076","3706406642","3706406642","4045616534","4045616534","956403285","956403285"]}