Sub topic 10
Animating the ::backdrop
This chapter assumes you know the dialog element and its showModal() method, transition-behavior: allow-discrete, and how @starting-style and overlay work for top-layer elements from the earlier chapters.
What the backdrop is
When you call showModal(), the browser places the dialog in the top layer. It also draws a ::backdrop pseudo-element, a full-viewport box behind the dialog. By default it is transparent. Most designs give it a dimming color.
The backdrop is a separate box with its own styles. Animating the dialog does not animate the backdrop. You must give the backdrop its own transition and its own starting style.
The same pattern, applied twice
The backdrop needs three things, the same ones the dialog needed:
- A closed-state value, such as a fully transparent color.
- An open-state value under
dialog[open]::backdrop. - An
@starting-styleblock for the open state, so the first style update has a "before" value.
Here is a complete page you can save and open:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Backdrop fade</title>
<style>
dialog {
opacity: 0;
transition:
opacity 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
dialog[open] {
opacity: 1;
}
@starting-style {
dialog[open] {
opacity: 0;
}
}
dialog::backdrop {
background-color: rgb(0 0 0 / 0);
transition:
background-color 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0.5);
}
@starting-style {
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0);
}
}
</style>
</head>
<body>
<button id="open">Open dialog</button>
<dialog id="dlg">
<p>Hello from the top layer.</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>What happens on open and close
The sequence below shows the timing. Notice that the backdrop's exit depends on the dialog staying in the top layer.
On open, the backdrop and dialog are rendered for the first time. Each takes the matching @starting-style value as its before-change style and transitions to the [open] value.
On close, the open attribute is removed. The dialog and backdrop now match the base rules again, so they transition back to transparent. Without overlay and display in the transition lists, the browser would remove the dialog from the top layer at once and the fade-out would never be seen.
Common mistakes
- Putting the starting style on
dialog::backdropinstead ofdialog[open]::backdrop. The starting style must match the selector that applies when the dialog is open, or it has no effect. - Forgetting the closed-state color. If the base
::backdroprule already hasrgb(0 0 0 / 0.5), there is nothing to fade from or to on close. - Animating only the dialog. The panel fades, but the page darkens or lightens abruptly.
- Using a long duration on the dialog and a short one on the backdrop. The backdrop can finish before the dialog does, which looks disjointed. Keep the durations equal or deliberately staggered.
Exercise
You add a fade-in to a modal dialog and its backdrop. The dialog fades in correctly, but the backdrop jumps straight to its dark color. Which rule is most likely missing, and why?
Show answer
An @starting-style { dialog[open]::backdrop { background-color: rgb(0 0 0 / 0); } } block, plus a transition on dialog::backdrop that includes background-color. The backdrop is a separate pseudo-element. It has no before-change style on its first render until you give it one, so it cannot transition.