CKTIS / CSS
:modal — modal-state pseudo-class
The :modal pseudo-class selects elements in a modal state, not merely visible or open elements. It can style a dialog opened with showModal() while distinguishing it from a dialog opened with show().
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:modal:modal is a pseudo-class without arguments that matches an element in a state that excludes interaction with elements outside it. The selector dialog:modal restricts matching to dialog elements. A CSS rule only responds to this state—it does not enable modality.
In HTML, matching includes dialog elements whose internal modal state is true and elements whose fullscreen flag is set. showModal() opens a dialog modally; show() opens it non-modally. The open attribute alone therefore does not imply a :modal match: dialog[open] selects both open modal dialogs and open non-modal dialogs.
Do not treat :modal as a synonym for the active window or as a selector exclusively for dialogs. Multiple elements can match :modal simultaneously, although only one is active and able to receive input. Matching fullscreen elements is another reason to use dialog:modal when a style should apply only to dialogs.
The example records matches(':modal') for a closed dialog, after show(), and after clicking the button that calls showModal(). The expected values are false, false, and true, respectively; the modal dialog gets a green border. The example requires local verification in the target browser.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:modal test</title>
<style>
dialog { border: 3px solid gray; }
dialog:modal { border-color: green; }
</style>
<button id="open" type="button">Open modal</button>
<dialog id="panel" aria-label="Modal test">
<p>Modal dialog</p>
<form method="dialog"><button>Close</button></form>
</dialog>
<pre id="result"></pre>
<script>
const panel = document.querySelector('#panel');
const result = document.querySelector('#result');
const lines = [];
function record(label) {
lines.push(`${label}: ${panel.matches(':modal')}`);
result.textContent = lines.join('\n');
}
record('closed');
panel.show();
record('show()');
panel.close();
document.querySelector('#open').addEventListener('click', () => {
panel.showModal();
record('showModal()');
}, { once: true });
</script>
</html>Result
closed: false show(): false showModal(): true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.