Modal Dialog

Centered modal with backdrop blur and close button.

Delete item?

This action cannot be undone. The item will be permanently removed.

Tailwind CSS
<div class="relative max-w-sm w-full rounded-3xl bg-white p-6 shadow-2xl">
  <button aria-label="Close" class="absolute top-4 right-4 w-8 h-8 rounded-full bg-slate-100 hover:bg-slate-300 flex items-center justify-center text-slate-500 transition-colors">
    <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/></svg>
  </button>
  <div class="w-12 h-12 rounded-2xl bg-indigo-100 text-indigo-600 flex items-center justify-center mb-4">
    <svg class="w-6 h-6" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
  </div>
  <h3 class="text-lg font-bold text-slate-900">Delete item?</h3>
  <p class="mt-2 text-sm text-slate-500">This action cannot be undone. The item will be permanently removed.</p>
  <div class="mt-6 flex gap-3">
    <button class="flex-1 px-4 py-2.5 rounded-xl border border-slate-200 text-sm font-bold text-slate-700 hover:bg-slate-50 transition-colors">Cancel</button>
    <button class="flex-1 px-4 py-2.5 rounded-xl bg-red-600 text-sm font-bold text-white hover:bg-red-500 transition-colors">Delete</button>
  </div>
</div>

Free to use in personal and commercial projects. No attribution required.

About this modal dialog

Modals demand attention. They dim the background, center a dialog, and force a decision before the visitor can continue. A tailwind modal like this one is the standard pattern for confirmations, forms, and detail views that should not navigate away from the page.

This snippet shows the dialog itself. In production, wrap it in a fixed inset-0 container with a backdrop (bg-black/50 backdrop-blur-sm) and center it with flex. The close button sits top-right, the icon sets context, and the actions are right-aligned with the destructive option visually distinct.

Use modals sparingly. Every modal is an interruption, and interruptions erode trust if overused. Reserve them for destructive confirmations, critical forms, and content that genuinely needs focus. For non-critical messages, a toast is kinder.

Some actions cannot be undone and some forms need full attention. Inline UI lets users ignore both. A modal removes the rest of the page from the equation: the blurred backdrop dims everything behind it, which is a visual contract saying nothing else exists until you decide. That forced focus is why confirmations, destructive actions, and short critical forms belong in modals.

The dialog is relative max-w-sm w-full rounded-3xl bg-white p-6 shadow-2xl. In production it sits inside a fixed inset-0 flex container with bg-black/50 backdrop-blur-sm that centers it and blocks the page. The close button is absolute top-4 right-4; a w-12 h-12 rounded-2xl icon block sets the context; the title and description follow; actions align right, with the dangerous action styled to stand apart from the safe one.

Animate the entrance from scale-95 opacity-0 over 150 to 200ms; reverse it on close before unmounting so it never blinks out. Keep the width under 512px for readability. On mobile, anchor the dialog to the bottom as a sheet instead of centering it; bottom sheets suit thumbs and small screens far better than floating cards.

The keyboard mistake is forgetting focus entirely: opening without moving focus into the dialog, and closing without returning focus to the button that opened it. Keyboard users get stranded, tabbing through a page they cannot see. Store document.activeElement on open, move focus inside, and restore it on close.

Design notes

When to use

Use a modal when the user must finish one focused task before doing anything else, like confirming a delete or completing a short form. The blurred backdrop dims the page behind it, which signals that the rest of the interface is temporarily off limits. Keep the content short enough to fit without scrolling, since a modal that scrolls starts to feel like a page trapped in a box.

What to avoid

Do not open a modal on page load or use one for content users might want to reference while reading the page, like terms or help text, because the backdrop blocks exactly that. Stacking a second modal on top of the first is another common mistake that confuses focus and the close button. If the flow has more than two steps, build a real page instead.

Accessibility

Trap keyboard focus inside the modal while it is open and return focus to the trigger when it closes. The close button needs a clear aria-label, and pressing Escape should dismiss the dialog.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Success

Use the success modal for confirmations like completed orders.

Order placed

Order #4821 is confirmed. A receipt is on its way to your inbox.

Tailwind CSS

Dark

The dark modal matches dark-mode apps and media overlays.

Delete item?

This action cannot be undone. The item will be permanently removed.

Tailwind CSS

Newsletter

Use the newsletter modal for email capture with a clear value promise.

Get product updates

One short email a month. New features, no spam.

Tailwind CSS

Where to use it

  • ✓ Delete confirmations
  • ✓ Login/signup forms
  • ✓ Image lightboxes
  • ✓ Onboarding wizards

Frequently asked questions

How do I build a modal in Tailwind?

Create a fixed inset-0 wrapper with flex centering and a semi-transparent backdrop. Place the dialog card inside. Toggle visibility with JavaScript. Add Escape-to-close and backdrop-click-to-close.

Modal vs new page, when to use which?

Use modals for quick tasks that relate to the current context (confirmations, quick forms). Use a new page for complex multi-step flows that deserve their own URL.

Are modals bad for mobile?

Full-screen modals work well on mobile (bottom sheets are even better). Centered small modals can feel cramped; consider a bottom sheet pattern on small screens.

How do I close a modal when clicking outside it?

Attach the click handler to the backdrop element and only close when event.target is the backdrop itself, not the dialog panel inside it. With the native <dialog> element, listen for clicks on the dialog and compare target to currentTarget. This keeps clicks on buttons and form fields inside from closing it.

How do I close a modal with the Escape key?

The native <dialog> element closes on Escape automatically when opened with showModal(). For custom div-based modals, add a keydown listener on the document that checks for the Escape key and only fires while the modal is open. Move focus back to the trigger button after closing.

How do I stop the page scrolling behind an open modal?

Set document.body.style.overflow to hidden when the modal opens and restore it on close. If the page has a visible scrollbar, add an equal padding-right to the body so content does not shift when the scrollbar disappears. The native <dialog> handles scroll locking for you.

Customization tips

Add scale-in animation with a keyframe from scale-95 opacity-0. Trap focus inside the modal for accessibility. Close on backdrop click and Escape key. Keep modal width under 512px for readability.