0
0
Tailwindmarkup~7 mins

Modal and overlay patterns in Tailwind

Choose your learning style9 modes available
Introduction

A modal is a box that appears on top of the page to show important information or ask for a choice. An overlay is the dark background behind the modal that helps focus attention on it.

When you want to ask the user to confirm an action, like deleting a file.
When you need to show extra details without leaving the current page.
When you want to display a form or message that requires user interaction.
When you want to pause the main page and get user input.
When you want to highlight a special offer or alert.
Syntax
Tailwind
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
  <div class="bg-white p-6 rounded shadow-lg max-w-md w-full">
    <!-- Modal content here -->
  </div>
</div>

fixed inset-0 makes the overlay cover the whole screen.

bg-opacity-50 makes the black background semi-transparent.

Examples
This shows a basic modal centered on the screen with a dark transparent background behind it.
Tailwind
<!-- Simple modal with overlay -->
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
  <div class="bg-white p-4 rounded">
    <p>This is a modal.</p>
  </div>
</div>
This modal includes a close button with accessible label and better styling.
Tailwind
<!-- Modal with close button -->
<div class="fixed inset-0 bg-black bg-opacity-60 flex items-center justify-center">
  <div class="bg-white p-6 rounded shadow-lg max-w-sm w-full relative">
    <button class="absolute top-2 right-2 text-gray-600 hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-gray-400" aria-label="Close modal">x</button>
    <h2 class="text-lg font-semibold mb-4">Modal Title</h2>
    <p>Modal content goes here.</p>
  </div>
</div>
Sample Program

This example shows a button that opens a modal with an overlay. The modal can be closed by clicking the close button, clicking outside the modal, or pressing Escape. It uses Tailwind CSS classes for styling and accessibility features like aria-modal and focus management.

Tailwind
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Modal Example</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
  <button id="openModal" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400">Open Modal</button>

  <!-- Modal and overlay hidden by default -->
  <div id="modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden" role="dialog" aria-modal="true" aria-labelledby="modalTitle">
    <div class="bg-white p-6 rounded shadow-lg max-w-md w-full relative" tabindex="-1">
      <button id="closeModal" class="absolute top-3 right-3 text-gray-600 hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-gray-400" aria-label="Close modal">x</button>
      <h2 id="modalTitle" class="text-xl font-semibold mb-4">Welcome to the Modal</h2>
      <p class="mb-4">This is a simple modal using Tailwind CSS. Click outside or the close button to dismiss.</p>
      <button id="confirmBtn" class="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-400">Confirm</button>
    </div>
  </div>

  <script>
    const openBtn = document.getElementById('openModal');
    const closeBtn = document.getElementById('closeModal');
    const modal = document.getElementById('modal');

    openBtn.addEventListener('click', () => {
      modal.classList.remove('hidden');
      // Focus the modal for accessibility
      modal.querySelector('div').focus();
    });

    closeBtn.addEventListener('click', () => {
      modal.classList.add('hidden');
      openBtn.focus();
    });

    // Close modal when clicking outside the modal content
    modal.addEventListener('click', (e) => {
      if (e.target === modal) {
        modal.classList.add('hidden');
        openBtn.focus();
      }
    });

    // Optional: Close modal with Escape key
    document.addEventListener('keydown', (e) => {
      if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
        modal.classList.add('hidden');
        openBtn.focus();
      }
    });
  </script>
</body>
</html>
OutputSuccess
Important Notes

Always add aria-modal="true" and role="dialog" to the modal container for screen readers.

Use keyboard controls like Escape to close the modal for better accessibility.

Keep the modal content focused when it opens to help keyboard users.

Summary

Modals show important info or choices on top of the page with a dark overlay behind.

Use Tailwind's fixed positioning and background opacity classes to create overlays.

Make modals accessible with ARIA roles and keyboard controls.