Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :modal pseudo-class

Reference for the CSS :modal pseudo-class: style a dialog only when it is open as a modal with showModal(), with a live example and browser support.

The :modal pseudo-class matches an element that is in a modal state: it blocks interaction with the rest of the page until it is dismissed. In practice that means a <dialog> opened with showModal() and an element shown in fullscreen.

It lets you give a dialog one look when it is modal and another when it is opened non-modally with show() or the open attribute.

CSS
dialog:modal {
  border: 0;
  border-radius: 12px;
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.3);
}

Try it

Modal versus non-modal dialog

Syntax

CSS
:modal { /* declarations */ }

No arguments; specificity (0,1,0).

Syntax and arguments

Part Description
dialog:modal A dialog opened with showModal().
dialog:not(:modal) A dialog opened with show() or the open attribute.
Other matches An element that is currently in fullscreen.

Usage notes

  • Same element, two modes. The open attribute is present in both modes, so dialog[open] can't tell them apart; :modal can.
  • Modal means inert page. While a modal dialog is open, the rest of the document is inert: it can't be clicked, focused or read by screen readers. That's built into the browser, not something CSS does.
  • Use ::backdrop for the dimmed layer. The area behind a modal dialog is styled with ::backdrop.
  • Top layer. Modal dialogs are placed in the top layer, above all other content regardless of z-index.
  • Styling hook, not a trigger. :modal only reflects state; it cannot open or close anything.

Browser support

Supported in all current browsers and Baseline widely available.