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.
:modal
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.
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
: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
dialogelement opened with theshowModal()API. - The element selected by the
:fullscreenpseudo-class when opened with therequestFullscreen()API.
Examples
Styling a modal dialog
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 | |
:modal |
105 | 105 | 103 | 91 | 15.6 | 105 | 103 | 72 | 15.6 | 20.0 | 105 |
See also
-
dialogelement - Other element display state pseudo-classes:
:fullscreenand:picture-in-picture - Complete list of pseudo-classes
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/:modal