Sub topic 10
Animating the ::backdrop
This chapter assumes you have read the earlier chapters on @starting-style, transition-behavior: allow-discrete, and the overlay property. It also assumes you know the dialog element and its showModal() and close() methods. Here you will apply those pieces to the dimmed layer behind a modal dialog.
What the ::backdrop is
::backdrop is a pseudo-element that the browser creates for an element in the top layer. It is a box the size of the viewport, drawn directly beneath its originating element and above everything else on the page. Three kinds of element get one:
- A
dialogopened withshowModal(). - An element shown with the Popover API (
popoverattribute) while it is open. - An element displayed in fullscreen with
requestFullscreen().
A dialog opened with show() is not modal and has no backdrop. The backdrop exists only while its originating element is in the top layer. When the element leaves the top layer, the backdrop disappears with it.
Browsers give the backdrop a default style, usually a faint dark tint. You normally replace that with your own color, and you may add backdrop-filter for blur.
The same two problems, again
A backdrop has the same two animation problems as the dialog it belongs to.
- Entry. The backdrop is created when the dialog opens. Like any newly rendered element, it has no before-change style, so no transition runs. It appears at its final color instantly. You fix this with
@starting-style. - Exit. When you call
close(), theopenattribute is removed. The browser setsdisplay: noneand removes the dialog from the top layer in the same style update. The backdrop vanishes immediately. You fix this by transitioningdisplayandoverlaywithallow-discrete.
The backdrop is a separate box with its own computed style, so it needs its own transition and its own starting style. Transitions on the dialog do not carry over to it.
The lifecycle of a modal and its backdrop
The diagram shows what happens to the backdrop when you open and close a modal dialog that has the transitions from the next section.
A complete example
This page runs as written in a browser that supports @starting-style, transition-behavior, and discrete transitions of display and overlay. Current Chrome, Edge, Safari, and Firefox do.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated dialog backdrop</title>
<style>
dialog {
opacity: 0;
transform: translateY(1rem);
transition:
opacity 0.3s,
transform 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
dialog[open] {
opacity: 1;
transform: none;
}
@starting-style {
dialog[open] {
opacity: 0;
transform: translateY(1rem);
}
}
dialog::backdrop {
background-color: rgb(0 0 0 / 0);
backdrop-filter: blur(0);
transition:
background-color 0.3s,
backdrop-filter 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0.5);
backdrop-filter: blur(4px);
}
@starting-style {
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0);
backdrop-filter: blur(0);
}
}
@media (prefers-reduced-motion: reduce) {
dialog,
dialog::backdrop {
transition-duration: 0.01ms;
}
}
</style>
</head>
<body>
<button id="open">Open dialog</button>
<dialog id="dlg">
<p>The backdrop fades and blurs in and out with this dialog.</p>
<button id="close">Close</button>
</dialog>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open').addEventListener('click', () => dlg.showModal());
document.getElementById('close').addEventListener('click', () => dlg.close());
</script>
</body>
</html>The example has three parts.
- Closed-state values on the base rule.
dialog::backdropsets a fully transparent color and no blur. This overrides the browser's default tint, so closing fades toward transparent and not toward the default. It also lists the properties to animate. - Open-state values on
dialog[open]::backdrop. These are the values the backdrop settles at while the dialog is open. - A starting style for the open selector. The
@starting-styleblock gives the backdrop its before-change style on the first update after it is created. The backdrop starts transparent and transitions to the open values.
Why the backdrop lists display and overlay
On close, the dialog gets display: none and leaves the top layer. Those are discrete changes. Without allow-discrete, they happen at the start of the transition, and the fade-out never shows.
display 0.3s allow-discretekeeps the olddisplayvalue for the whole 0.3 seconds, then switches tonone.overlay 0.3s allow-discretekeeps the element in the top layer for the same time, then removes it.
The backdrop's visibility depends on the same top-layer membership. The example repeats both entries on dialog::backdrop so that it has a transition running for the full duration and its exit is not cut short. The durations on the dialog and the backdrop do not have to match. If the dialog's display and overlay transitions are shorter than the backdrop's color transition, the backdrop is cut off when the dialog leaves the top layer. Make the dialog's discrete transitions at least as long as the longest transition on either box.
Order matters: specificity and source order
@starting-style adds no specificity of its own. A rule inside it has the specificity of its selector, and it takes part in the normal cascade. This has two consequences for backdrops.
- The starting-style rule
dialog[open]::backdrophas the same specificity as the main ruledialog[open]::backdrop. The starting style must come later in the source, or it must be more specific. Otherwise the main rule wins and the backdrop has no distinct starting value. - Starting styles only supply values for the first style update. After that, the normal rules apply. If a later rule overrides the base rule at higher specificity, your transition may animate toward values you did not expect.
If a utility class or a framework reset sets background on ::backdrop with higher specificity, the starting style loses. Raise the starting-style specificity, or move the overriding rule earlier.
Custom properties and a single timing value
Repeating 0.3s in four places invites mistakes. In current browsers, ::backdrop inherits from its originating element. Older versions did not, so check support before relying on it. Where inheritance works, you can declare a custom property on the dialog and use it in both boxes.
dialog {
--fade: 0.3s;
--dim: rgb(0 0 0 / 0.5);
}
dialog::backdrop {
background-color: rgb(0 0 0 / 0);
transition:
background-color var(--fade),
overlay var(--fade) allow-discrete,
display var(--fade) allow-discrete;
}
dialog[open]::backdrop {
background-color: var(--dim);
}
@starting-style {
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0);
}
}If the backdrop shows no color in a browser that does not inherit, define the custom properties on :root instead. That works everywhere.
Popover backdrops
Popovers in the open state also get a ::backdrop. It is transparent by default and shows no color unless you set one. The pattern is the same, with :popover-open in place of [open].
[popover]::backdrop {
background-color: rgb(0 0 0 / 0);
transition:
background-color 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
[popover]:popover-open::backdrop {
background-color: rgb(0 0 0 / 0.3);
}
@starting-style {
[popover]:popover-open::backdrop {
background-color: rgb(0 0 0 / 0);
}
}A popover with popover="auto" closes when the user clicks outside it. That is light dismiss. The close animation then runs the same way as with hidePopover().
Common pitfalls
| Symptom | Likely cause |
|---|---|
| Backdrop appears instantly, dialog fades in | No `@starting-style` rule for `dialog[open]::backdrop` |
| Backdrop vanishes on close, dialog fades out | The backdrop has no `transition` of its own, or no `display` and `overlay` entries |
| Backdrop cuts off before the dialog finishes fading | The dialog's `display` and `overlay` durations are shorter than the other transitions |
| No backdrop at all | The dialog was opened with `show()`, not `showModal()` |
| Backdrop fades toward a faint gray, not to nothing | The base rule does not set an explicit transparent color, so the browser default applies |
| Starting style has no effect | A rule with equal or higher specificity comes later in the source |
In DevTools, you can inspect ::backdrop under the dialog node in the Elements panel. Chromium shows it as a child pseudo-element in the top layer section. To slow the effect for debugging, temporarily set --fade to 3s and watch the Animations panel.
Reduced motion
A dimming backdrop is not a large movement, but a blur or a long fade can still bother users who ask for reduced motion. The media query in the example sets transition-duration to 0.01ms on both boxes. The final states still apply, and the display and overlay flips still happen. Do not use transition: none, which can leave a closed dialog on screen if your exit relies on the discrete transition.
Exercise
A modal dialog fades in correctly, but when you call close() its backdrop disappears immediately while the dialog still fades out. Which two additions to dialog::backdrop are most likely to fix this, and why is a transition on dialog itself not enough?
Show answer
Add display and overlay with allow-discrete to the transition list of dialog::backdrop, alongside the property that fades (such as background-color). The backdrop is its own box with its own computed style, so it does not use the dialog's transition declaration. Without its own display and overlay entries, its exit is not delayed, and it is removed from the top layer as soon as the open attribute is removed.
Exercise
You wrote @starting-style { dialog[open]::backdrop { background-color: transparent; } } before the rule dialog[open]::backdrop { background-color: rgb(0 0 0 / 0.5); }. The backdrop still appears at full color with no fade. What is wrong and how do you fix it?
Show answer
Both rules have the same specificity, and the later one wins. The starting style comes first in the source, so the main rule overrides it and the backdrop has no distinct before-change value. Move the @starting-style block after the main rule, or give its selector higher specificity.