Sub topic 8
Styling the dialog and its backdrop
This chapter assumes you can write basic CSS selectors and pseudo-elements, and that you can open a dialog with showModal() and close() from earlier chapters. Here you will learn what the browser styles by default, how to override it without breaking the open and closed states, and how to style the backdrop that sits behind a modal dialog.
What the browser gives you by default
A <dialog> is an ordinary element with a small built-in stylesheet. Knowing that stylesheet explains most styling surprises. The values below are approximate and differ slightly between browsers.
| Rule | Effect |
|---|---|
| `dialog:not([open])` is `display: none` | A closed dialog takes no space. |
| `margin: auto`, `width` and `height: fit-content` | The dialog sizes to its content and centers. |
| `border`, `padding: 1em`, `background: Canvas`, `color: CanvasText` | A plain bordered box that follows the page color scheme. |
| `dialog:modal` is `position: fixed` | A modal dialog is positioned against the viewport. |
| `max-width` and `max-height` of about `100% - 6px - 2em` | A modal dialog stays inside the viewport and scrolls if it is taller. |
| `::backdrop` with a translucent black background | A modal dialog dims the page behind it. |
A non-modal dialog opened with show() is position: absolute. It appears in normal page flow and has no backdrop. The difference between the two is the :modal pseudo-class, which matches only a dialog that was opened with showModal().
Do not override display on the base selector
The most common styling bug is making a dialog permanently visible. The browser hides a closed dialog with a low-specificity display: none rule. If you write display: flex or display: grid on the bare dialog selector, your rule wins and the closed dialog appears on the page.
/* Wrong: the dialog is now visible even when closed */
dialog {
display: grid;
}
/* Right: only change display while the dialog is open */
dialog[open] {
display: grid;
}Apply the same rule to anything else that changes layout mode, such as display: flex. Scope it to dialog[open] or dialog:modal.
Styling the box
Start by resetting what you plan to replace. A reliable pattern is to remove the dialog's own padding and put padding on an inner wrapper. This pays off in the backdrop-click chapter, because a click whose target is the dialog element itself can then only come from outside the visible content.
dialog {
border: none;
border-radius: 12px;
padding: 0;
width: min(32rem, 90vw);
max-height: 85dvh;
overflow: auto;
box-shadow: 0 20px 50px rgb(0 0 0 / 0.3);
}
dialog > .content {
padding: 1.5rem;
}Some details are worth noting:
- Use
width: min(32rem, 90vw)ormax-width, not a fixed width, so the dialog fits phones. - Use
dvhunits for height limits on mobile. Thedvhunit tracks the visible viewport as browser toolbars show and hide. - Set
overflow: autoon the dialog so long content scrolls inside the box. A modal dialog already has this in most browsers, but a non-modal one does not. box-shadowis clipped by nothing. A dialog in the top layer is not affected byoverflow: hiddenorz-indexon its ancestors, so you never needz-indexon it.
Positioning
Auto margins center the dialog both ways. To place it elsewhere, change the margins. Keep position as the browser set it.
/* Side sheet: pinned to the right edge, full height */
dialog.sheet {
margin: 0 0 0 auto;
height: 100dvh;
max-height: none;
border-radius: 0;
}
/* Bottom sheet on narrow screens */
@media (max-width: 40rem) {
dialog.sheet-bottom {
margin: auto 0 0;
width: 100%;
max-width: none;
border-radius: 12px 12px 0 0;
}
}max-height: none matters on the side sheet. Without it, the browser's viewport limit leaves a gap at the top and bottom.
Styling the backdrop
::backdrop is a pseudo-element that the browser creates only for elements in the top layer. For a dialog, that means one opened with showModal(). A dialog opened with show() has no backdrop, so a dialog::backdrop rule does nothing for it.
dialog::backdrop {
background: rgb(15 23 42 / 0.6);
backdrop-filter: blur(3px);
}backdrop-filter blurs whatever is behind the backdrop. It can cost performance on large pages and on low-end devices, so test it before relying on it.
Limits of ::backdrop
The backdrop is a sibling-like box that covers the viewport, not a child of the dialog. That has consequences:
- You cannot style it with a descendant selector.
dialog.danger::backdropworks, but.page.dark dialog ::backdropdoes not behave as you might expect. - Older browsers do not let
::backdropinherit from its dialog. A custom property set ondialogmay not be visible inside::backdrop. Recent browsers inherit from the originating element, but to be safe, define shared values on:root. - You cannot put content in it or attach event listeners to it. Clicks on the backdrop are delivered to the dialog element.
:root {
--overlay: rgb(15 23 42 / 0.6);
}
dialog::backdrop {
background: var(--overlay);
}Locking page scroll
A modal dialog makes the rest of the page inert, meaning it ignores clicks, focus, and typing. The page can still scroll with the mouse wheel or touch in some browsers. You can stop that with :has(). Use :modal so that a non-modal dialog does not lock the page.
html:has(dialog:modal) {
overflow: hidden;
scrollbar-gutter: stable;
}scrollbar-gutter: stable reserves the scrollbar's space. Without it, hiding the scrollbar makes the page jump sideways when the dialog opens. If you need to support a browser without :has(), toggle a class on <body> from your open and close code instead.
Styling by state
Because :modal is a real pseudo-class, one stylesheet can handle both ways of opening a dialog.
dialog:modal {
box-shadow: 0 20px 50px rgb(0 0 0 / 0.35);
}
dialog:not(:modal)[open] {
box-shadow: 0 4px 12px rgb(0 0 0 / 0.2);
border: 1px solid #ccc;
}You can also style the form controls inside, for example dialog :user-invalid for fields the user has touched and left invalid.
Putting it together
This page is complete. Save it as an .html file and open it in a current browser.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Styled dialog</title>
<style>
:root {
color-scheme: light dark;
--overlay: rgb(15 23 42 / 0.6);
}
dialog {
border: none;
border-radius: 12px;
padding: 0;
width: min(28rem, 90vw);
max-height: 85dvh;
overflow: auto;
box-shadow: 0 20px 50px rgb(0 0 0 / 0.35);
}
dialog[open] {
display: grid;
}
dialog > .content {
padding: 1.5rem;
}
dialog h2 {
margin-top: 0;
}
dialog menu {
display: flex;
justify-content: flex-end;
gap: 0.5rem;
padding: 0;
margin: 1.5rem 0 0;
}
dialog::backdrop {
background: var(--overlay);
backdrop-filter: blur(3px);
}
html:has(dialog:modal) {
overflow: hidden;
scrollbar-gutter: stable;
}
</style>
</head>
<body>
<button id="open">Delete project</button>
<dialog id="dlg" aria-labelledby="dlg-title">
<div class="content">
<h2 id="dlg-title">Delete this project?</h2>
<p>This action cannot be undone.</p>
<form method="dialog">
<menu>
<button value="cancel" autofocus>Cancel</button>
<button value="delete">Delete</button>
</menu>
</form>
</div>
</dialog>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open').addEventListener('click', () => dlg.showModal());
dlg.addEventListener('close', () => console.log('closed with:', dlg.returnValue));
</script>
</body>
</html>Three choices in this page are deliberate. The display: grid is attached to dialog[open]. The padding lives on .content. The overlay color lives in a :root variable, so it works even where ::backdrop does not inherit from the dialog.
Common styling mistakes
- Dialog visible on load. A
displayvalue on the baredialogselector. Move it todialog[open]. - No dimming behind the dialog. The dialog was opened with
show(), or with theopenattribute, so there is no backdrop. UseshowModal(). - Backdrop color ignores your variable. The custom property is set on
dialogor a class, and the browser does not inherit it into::backdrop. Define it on:root. - Dialog stuck behind other content. This should not happen in the top layer. If it does, the dialog is probably not modal and is being clipped by an ancestor with
overflow: hidden. Open it withshowModal(). - Gap above a full-height sheet. The default
max-heightstill applies. Setmax-height: none.
Exercise
You write dialog { display: flex; flex-direction: column; } and the dialog now shows on page load, before any button is clicked. Why, and how do you fix it?
Show answer
The browser hides closed dialogs with a dialog:not([open]) { display: none } rule in its default stylesheet. Your dialog { display: flex } rule has higher priority because author styles beat the browser's, so the dialog is always displayed. Scope the rule to the open state, as in dialog[open] { display: flex; flex-direction: column; }.
Exercise
A teammate styles dialog::backdrop with a dark overlay and tests it by calling dlg.show(). No overlay appears. What is wrong, and what else changes when the call is fixed?
Show answer
::backdrop exists only for elements in the top layer, and show() opens a non-modal dialog that stays in normal layout. Calling showModal() creates the backdrop. It also puts the dialog in the top layer, makes the rest of the page inert, and makes the dialog match :modal.
Exercise
Write CSS that turns a dialog with the class sheet into a full-height panel attached to the right edge of the viewport. Explain why max-height: none is included.
Show answer
Use dialog.sheet { margin: 0 0 0 auto; height: 100dvh; max-height: none; border-radius: 0; }. The auto left margin pushes the dialog to the right. max-height: none removes the browser's modal limit of roughly the viewport minus a small margin, which would otherwise leave a gap above and below the panel.