Sub topic 8
Styling the Dialog and Its Backdrop
This chapter assumes you know basic CSS selectors and pseudo-elements, and that you can open a dialog with showModal(). Here you will style the dialog box, dim the page behind it with ::backdrop, and stop the page from scrolling while the dialog is open.
What the browser gives you by default
A <dialog> has default styles from the browser. It has a border, padding, a white background, and it is centered in the viewport when opened with showModal(). These defaults vary a little between browsers, so most projects reset the parts they care about.
A modal dialog sits in the top layer, so it is drawn above the rest of the page. You do not need z-index for it. Setting one has no effect on its position above the page.
Styling the dialog box
You can style dialog like any other element. The example below removes the default border, rounds the corners, and limits the width.
The dialog gets padding: 0, and an inner wrapper with the class content carries the spacing. This matters later in the chapter, when a click on the dialog element itself will be used to detect a backdrop click.
dialog {
border: none;
border-radius: 8px;
padding: 0;
max-width: min(32rem, 90vw);
}
dialog > .content {
padding: 1.5rem;
}min(32rem, 90vw) picks the smaller of the two values. The dialog never grows past 32rem, and on narrow screens it keeps a margin on each side.
Styling the backdrop
When you open a dialog with showModal(), the browser creates a ::backdrop pseudo-element. It covers the whole viewport, directly behind the dialog and above the page. A non-modal dialog opened with show() has no backdrop.
You style it with the ::backdrop selector:
dialog::backdrop {
background: rgb(0 0 0 / 0.5);
}This paints the page behind the dialog with 50% black. You can also use backdrop-filter: blur(4px) to blur the page.
::backdrop has two limits. First, it does not inherit from the dialog, so custom properties set on dialog are not available inside dialog::backdrop. Declare them on :root if the backdrop needs them. Second, you can only style it. You cannot add child elements to it.
Stopping the page from scrolling
A modal dialog makes the page behind it inert, but the page can still scroll with the mouse wheel. The :has() selector lets you lock scrolling while a dialog is open, with no JavaScript:
body:has(dialog[open]) {
overflow: hidden;
}The rule matches a body that contains a dialog with the open attribute. When the dialog closes, the attribute is removed and scrolling returns.
Putting it together
The following page uses all of the styles above. Open it, and the page dims and stops scrolling. Press Escape or choose a button to close it.
<button id="open">Edit profile</button>
<dialog id="dlg" aria-labelledby="dlg-title">
<div class="content">
<h2 id="dlg-title">Edit profile</h2>
<form method="dialog">
<label>Name <input name="name" required autofocus></label>
<menu>
<button value="cancel" formnovalidate>Cancel</button>
<button value="save">Save</button>
</menu>
</form>
</div>
</dialog>
<style>
dialog {
border: none;
border-radius: 8px;
padding: 0;
max-width: min(32rem, 90vw);
}
dialog > .content { padding: 1.5rem; }
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
body:has(dialog[open]) { overflow: hidden; }
</style>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open').addEventListener('click', () => dlg.showModal());
</script>Common styling mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `dialog { display: flex; }` | A closed dialog stays visible | Use `dialog[open]` |
| Padding on `dialog` itself | A backdrop-click check is unreliable later | Use `padding: 0` and an inner wrapper |
| Using `dialog` custom properties in `::backdrop` | Values are undefined | Declare them on `:root` |
| Adding `z-index` to fix stacking | No effect, the top layer is already above | Remove it |
Exercise
A teammate writes dialog { display: grid; } to lay out a form inside the dialog. Afterward, the dialog is visible on page load before any button is clicked. Why, and how do you fix it?
Show answer
The author rule display: grid overrides the browser's default rule that sets display: none on a dialog without the open attribute, so the closed dialog is rendered. Apply the grid to dialog[open], or put the grid on an inner wrapper such as .content.
Exercise
Write the CSS that dims the page behind a modal dialog with 60% black and stops the page from scrolling while the dialog is open.
Show answer
dialog::backdrop { background: rgb(0 0 0 / 0.6); } and body:has(dialog[open]) { overflow: hidden; }