<div class="h-screen w-screen flex items-center justify-center">
  <button 
    class="bg-indigo-700 text-3xl text-white rounded-full px-8 hover:bg-indigo-600"
    data-modal-trigger="form">Open my modal</button>
  <button>
</div>

<div 
  class="fixed inset-0 transition-all transition-250 modal" 
  data-modal="form"
 >
  <div class="h-full w-full flex items-center justify-center">
    <div class="bg-white rounded-lg p-5 modal-body max-w-full md:max-w-2xl overflow-y-scroll">
      <div class="text-right border-b mb-3 py-1">
        <button class="close-modal">Close [x]</button>
      </div>
      <h2 class="text-center text-2xl leading-none">This is my modal title</h2>
      
      <p class="my-3"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin interdum velit eget nibh lacinia, cursus commodo libero efficitur. Cras iaculis hendrerit interdum. Suspendisse potenti. Nunc eu felis neque. Nunc pulvinar aliquet neque, sed vestibulum felis dapibus eget. Nam hendrerit, sem ac tincidunt ultrices, lorem neque vestibulum purus, et imperdiet lectus nisi eu nisl. Pellentesque vitae semper sem, ac rhoncus justo. Suspendisse pellentesque fringilla velit, ut vehicula magna vehicula vel. Sed vel dapibus enim, et gravida magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec quis libero urna.
 </p>
      <p class="my-3">Vestibulum eu ex interdum, dignissim magna nec, maximus mi. In hac habitasse platea dictumst. Donec tristique faucibus urna, id lacinia odio ullamcorper eget. Quisque ornare ex id facilisis dictum. Morbi non sapien sit amet massa sodales venenatis ut at ante. Morbi ullamcorper porttitor viverra. Praesent lobortis congue fermentum. Nullam sit amet rhoncus lorem, eu convallis libero. Pellentesque bibendum fermentum est, nec viverra nisi. Vivamus luctus interdum erat vel sodales. Fusce lectus felis, fringilla sollicitudin lacinia dignissim, ornare ut lacus. Aenean lorem nibh, aliquam sed dapibus at, sodales et augue. Sed tempor tortor sit amet urna varius vehicula. Etiam ligula dui, pretium porttitor nibh vitae, commodo euismod urna.</p>
    </div>
  </div>
</div>
/* Components */
.modal {
  display: none;
  background: rgba(0, 0, 0, 0.5);
  animation: fadeInFromNone 0.5s ease-out;
}

.modal-open {
  display: block;
}

/* Utilities */
.transition-all {
  transition: all;
}

.transition-250 {
  transition-duration: 250ms;
}

/* Animations */
@keyframes fadeInFromNone {
    0% {
        display: none;
        opacity: 0;
    }

    1% {
        display: block;
        opacity: 0;
    }

    100% {
        display: block;
        opacity: 1;
    }
}
const modalService = () => {
  const d = document;
  const body = d.querySelector('body');
  const buttons = d.querySelectorAll('[data-modal-trigger]');
  
  // attach click event to all modal triggers
  for(let button of buttons) {
    triggerEvent(button);
  }
  
  function triggerEvent(button) {
    button.addEventListener('click', () => {
      const trigger = button.getAttribute('data-modal-trigger');
      const modal = d.querySelector(`[data-modal=${trigger}]`);
      const modalBody = modal.querySelector('.modal-body');
      const closeBtn = modal.querySelector('.close-modal');
      
      closeBtn.addEventListener('click', () => modal.classList.remove('modal-open'))
      modal.addEventListener('click', () => modal.classList.remove('modal-open'))
      
      modalBody.addEventListener('click', (e) => e.stopPropagation());

      modal.classList.toggle('modal-open');
      
      // Close modal when hitting escape
      body.addEventListener('keydown', (e) => {
        if(e.keyCode === 27) {
          modal.classList.remove('modal-open')
        }
      });
    });
  }
}

modalService();
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.