:modal CSS pseudo-class

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2022.

The :modal CSS pseudo-class matches an element that is in a state in which it excludes all interaction with elements outside it until the interaction has been dismissed. Multiple elements can be selected by the :modal pseudo-class at the same time, but only one of them will be active and able to receive input.

Try it

button {
  display: block;
  margin: auto;
  width: 10rem;
  height: 2rem;
}

:modal {
  background-color: beige;
  border: 2px solid burlywood;
  border-radius: 5px;
}

p {
  color: black;
}
<p>Would you like to see a new random number?</p>
<button id="showNumber">Show me</button>

<dialog id="favDialog">
  <form method="dialog">
    <p>Lucky number is: <strong id="number"></strong></p>
    <button>Close dialog</button>
  </form>
</dialog>
const showNumber = document.getElementById("showNumber");
const favDialog = document.getElementById("favDialog");
const number = document.getElementById("number");

showNumber.addEventListener("click", () => {
  number.innerText = Math.floor(Math.random() * 1000);
  favDialog.showModal();
});

Syntax

css
:modal {
  /* ... */
}

Usage notes

Examples of elements that will prevent user interaction with the rest of the page and will be selected by the :modal pseudo-class include:

  • The dialog element opened with the showModal() API.
  • The element selected by the :fullscreen pseudo-class when opened with the requestFullscreen() API.

Examples

Styling a modal dialog

This example styles a modal dialog that opens when the "Update details" button is activated. This example has been built on top of the <dialog> element example.

CSS

css
:modal {
  border: 5px solid red;
  background-color: yellow;
  box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
}

Result

Specifications

Specification
HTML
# selector-modal
Selectors Level 4
# selectordef-modal

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
:modal
Chrome – Full support
Chrome 105 (Release date: 2022-09-02)
footnote Full support
Edge – Full support
Edge 105 (Release date: 2022-09-01)
footnote Full support
Firefox – Full support
Firefox 103 (Release date: 2022-07-26)
footnote Full support
Opera – Full support
Opera 91 (Release date: 2022-09-14)
footnote Full support
Safari – Full support
Safari 15.6 (Release date: 2022-07-20)
footnote Full support
Chrome Android – Full support
Chrome Android 105 (Release date: 2022-09-02)
footnote Full support
Firefox for Android – Full support
Firefox for Android 103 (Release date: 2022-07-26)
footnote Full support
Opera Android – Full support
Opera Android 72 (Release date: 2022-10-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15.6 (Release date: 2022-07-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 20 (Release date: 2023-02-10)
footnote Full support
WebView Android – Full support
WebView Android 105 (Release date: 2022-09-02)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15.6 (Release date: 2022-07-20)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also