Modal

Overlay

Modal

A modal is a dialog that floats above the screen in its own layer, dimming and blocking the background to pull your attention onto one task.

Click the backdrop (scrim), the X, or Cancel to close.

Definition

A modal is a dialog that floats up as a fresh layer above the screen you were just looking at, pulling your attention firmly inside it. When it opens, it covers the background behind it with a translucent scrim that dims everything, and the elements underneath can no longer even be clicked. So the user can't wander off elsewhere until they finish the task inside the modal, whether that means making an important confirmation or entering a value the app truly needs. A window asking "Are you sure you want to delete this?" or a short login form is a classic modal. In short, a modal declares to the whole screen that this one decision comes before anything else.

Why does it matter?

A modal's power lies in interrupting the flow, and its weakness sits in exactly the same place. Because it blocks the background and springs to the front, it's unbeatable at holding on to a confirmation you can't afford to miss or an action you can't undo. But that very forcefulness is why overusing it makes people feel boxed in, as if a window blocks every single thing they try to do. So a modal is a strong tool you should save for the rare moment that genuinely deserves to stop the flow. A modal is also a composite pattern, where several principles click into place at once: it hides the background with a scrim, raises its elevation so it sits above other elements, and traps keyboard focus inside while it's open. Only when these pieces move together does a modal really do its job.

Common mistakes

  • Firing off a modal for even trivial notices. When you use a modal for something that just needs to be announced, like a successful save, the user has to click a button to dismiss the window every time. Light news like this belongs in a toast or an inline message instead. The more you overuse modals, the more the user tires of closing them and starts brushing past even the important confirmations.
  • Giving only one way to close it, or leaving one out entirely. When clicking outside or pressing ESC is blocked, the user feels trapped, with no way out of a window they opened by mistake.
  • Making the confirm button for a risky action, like a delete, look identical to an ordinary button. When color and position don't set it apart, the user can absent-mindedly hit the destructive option and make a mistake they can't undo.

Practical tips

  • A modal is a strong device that cuts the flow, so use it only when you really must, and always offer several ways to close it: an X button, a click on the outside area, and the ESC key together. Users shouldn't feel trapped, so they need to be able to leave whenever they want.
  • For actions you can't undo, like a delete, mark the confirm button clearly in a color that signals danger. When color alone isn't enough, make the button text itself spell out what will happen, like "Delete."
  • When a modal opens, trap keyboard focus inside it, and when it closes, send focus back to the spot you originally clicked from. This one bit of handling transforms the experience for keyboard and screen reader users.

Ask your AI

When your modal's close and keyboard handling is sloppy

Audit the modal code I'll paste below and fix it for accessibility. Make sure it has all three ways to close — an X button, a click on the scrim (outside area), and the ESC key — and while it's open, trap keyboard focus inside the modal, then return focus to the button that opened it when it closes. Give the container role="dialog" and aria-modal="true", link its title with aria-labelledby, and make the scrim sit above other elements (via z-index) so it blocks clicks and scrolling on the background. List what was missing and briefly why each fix matters.

Building a can't-undo confirmation like delete

Build a confirmation modal for deleting [what's being deleted — e.g. the entire project]. Since this can't be undone, make the confirm button a danger red that clearly stands apart from the cancel button, and place the two buttons apart so no one hits the destructive one by accident. Have the button text spell out the action — "Delete", not "OK" — and state in one line exactly what will be lost. Make the X button, an outside click, and ESC all close it as a safe cancel where nothing happens.

Checking whether you've overused modals

Go through the screen/code I'll paste below and review everything you currently show as a modal, keeping only what genuinely has to stop the flow and pulling the rest out. For 'just letting them know' news like a successful save or a copied link, swap the modal — which forces the user to click a button to dismiss it every time — for a toast or an inline message. Judge each one by 'is this an irreversible or must-not-miss confirmation?', and give me a table of each modal with whether it stays, becomes a toast, or becomes inline, and why.

Related concepts