Published on

How To Make A Open/Close Multiple Model On Same Page With Tailwind CSS In 5 Easy Steps

Open/Close Multiple Model on same page

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 Open/Close Multiple Model on same page ui component

Open/close multiple model based on data-target

Why use Tailwind CSS to create a Open/Close Multiple Model on same page ui component?

  • It can make the building process of Open/Close Multiple Model on same page ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Open/Close Multiple Model on same page component file.

The preview of Open/Close Multiple Model on same page ui component

Free download of the Open/Close Multiple Model on same page's source code

The source code of Open/Close Multiple Model on same page ui component

<div>
  <a class='bg-blue-500 py-2 px-5 text-white rounded modal-open' data-toggle="modal" data-target="firstModal">Open First Modal</a>
  <a class='bg-blue-500 py-2 px-5 text-white rounded modal-open' data-toggle="modal" data-target="secondModal">Open Second Modal</a>
</div>

<div class="modal opacity-0 pointer-events-none fixed w-full h-full top-0 left-0 flex items-center justify-center" id="firstModal">
    <div class="modal-overlay absolute w-full h-full bg-gray-900 opacity-50"></div>
    
    <div class="modal-container bg-white w-11/12 md:max-w-md mx-auto rounded shadow-lg z-50 overflow-y-auto">
    
    <div class="modal-close absolute top-0 right-0 cursor-pointer flex flex-col items-center mt-4 mr-4 text-white text-sm z-50">
        <svg class="fill-current text-white" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
        <path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
        </svg>
        <span class="text-sm">(Esc)</span>
    </div>

    <!-- Add margin if you want to see some of the overlay behind the modal-->
    <div class="modal-content py-4 text-left px-6">
        <!--Title-->
        <div class="flex justify-between items-center pb-3">
        <p class="text-2xl font-bold">First Modal</p>
        <div class="modal-close cursor-pointer z-50">
            <svg class="fill-current text-black" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
            <path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
            </svg>
        </div>
        </div>
    </div>
    </div>
</div>

<div class="modal opacity-0 pointer-events-none fixed w-full h-full top-0 left-0 flex items-center justify-center" id="secondModal">
    <div class="modal-overlay absolute w-full h-full bg-gray-900 opacity-50"></div>
    
    <div class="modal-container bg-white w-11/12 md:max-w-md mx-auto rounded shadow-lg z-50 overflow-y-auto">
    
    <div class="modal-close absolute top-0 right-0 cursor-pointer flex flex-col items-center mt-4 mr-4 text-white text-sm z-50">
        <svg class="fill-current text-white" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
        <path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
        </svg>
        <span class="text-sm">(Esc)</span>
    </div>

    <!-- Add margin if you want to see some of the overlay behind the modal-->
    <div class="modal-content py-4 text-left px-6">
        <!--Title-->
        <div class="flex justify-between items-center pb-3">
            <p class="text-2xl font-bold">Second Modal</p>
            <div class="modal-close cursor-pointer z-50">
                <svg class="fill-current text-black" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
                <path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
                </svg>
            </div>
            </div>
        </div>
    </div>
</div>

<script>
   var openmodal = document.querySelectorAll('.modal-open')
   let selectedModalTargetId = ''
   for (var i = 0; i < openmodal.length; i++) {
     openmodal[i].addEventListener('click', function(event){
       selectedModalTargetId = event.target.attributes.getNamedItem('data-target').value
       event.preventDefault()
       toggleModal()
     })
   }

  const overlay = document.querySelector('.modal-overlay')
  overlay.addEventListener('click', toggleModal)

  var closemodal = document.querySelectorAll('.modal-close')
  for (var i = 0; i < closemodal.length; i++) {
    closemodal[i].addEventListener('click', toggleModal)
  }

  document.onkeydown = function(evt) {
    evt = evt || window.event
    var isEscape = false
    if ("key" in evt) {
      isEscape = (evt.key === "Escape" || evt.key === "Esc")
    } else {
      isEscape = (evt.keyCode === 27)
    }
    if (isEscape && document.body.classList.contains('modal-active')) {
      toggleModal()
    }
  }

  function toggleModal () {
    if(!selectedModalTargetId) {
      return
    }
    const body = document.querySelector('body')
    const modal = document.getElementById(selectedModalTargetId)
    modal.classList.toggle('opacity-0')
    modal.classList.toggle('pointer-events-none')
    body.classList.toggle('modal-active')
  }
</script>

How to create a Open/Close Multiple Model on same page 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/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to create a Open/Close Multiple Model on same page component

  • fixed
  • w-full
  • h-full
  • top-0
  • left-0
  • flex
  • absolute
  • bg-gray-900
  • bg-white
  • w-11/12
  • md:max-w-md
  • mx-auto
  • z-50
  • overflow-y-auto
  • right-0
  • flex-col
  • mt-4
  • mr-4
  • text-white
  • text-sm
  • py-4
  • text-left
  • px-6
  • pb-3
  • text-2xl
  • text-black

26 steps to create a Open/Close Multiple Model on same page component with Tailwind CSS

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

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

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

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

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

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

  7. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  8. Control the background color of an element to gray-900 using the bg-gray-900 utilities.

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

  10. Use w-11/12 to set an element to a fixed width(11/12).

  11. Set the maximum width/height of an element using the md:max-w-md utilities at only medium screen sizes.

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

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

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

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

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

  17. Control the margin on top side of an element to 1rem using the mt-4 utilities.

  18. Control the margin on right side of an element to 1rem using the mr-4 utilities.

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

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

  21. Control the vertical padding of an element to 1rem using the py-4 utilities.

  22. Control the text color of an element to left using the text-left utilities.

  23. Control the horizontal padding of an element to 1.5rem using the px-6 utilities.

  24. Control the padding on bottom side of an element to 0.75rem using the pb-3 utilities.

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

  26. Control the text color of an element to black using the text-black utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Open/Close Multiple Model on same page components, learn and follow along to implement your own components.