Sub topic 11
Animating a Dialog with CSS
This chapter assumes you can open and close a <dialog> with showModal() and close(), and that you know how to style ::backdrop. You also need basic CSS transitions.
Why dialogs resist animation
A closed dialog has display: none. Opening it switches display to block and puts it in the top layer. A normal transition cannot run in either direction.
- Entering: the element has no "before" style to transition from, because it did not exist in the rendering tree a moment earlier.
- Leaving:
display: noneapplies immediately, so the element vanishes before any fade-out can play.
Three CSS features fix this. @starting-style supplies the "before" style for the entry animation. transition-behavior: allow-discrete (written as allow-discrete in the transition shorthand) lets display take part in a transition. The overlay property keeps the element in the top layer until the exit finishes.
A complete example
The page below fades and slides the dialog in and out, and fades the backdrop with it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated dialog</title>
<style>
dialog {
opacity: 0;
transform: translateY(1rem);
transition:
opacity 200ms ease,
transform 200ms ease,
overlay 200ms allow-discrete,
display 200ms allow-discrete;
}
dialog[open] {
opacity: 1;
transform: translateY(0);
}
@starting-style {
dialog[open] {
opacity: 0;
transform: translateY(1rem);
}
}
dialog::backdrop {
background: rgb(0 0 0 / 0);
transition:
background 200ms ease,
overlay 200ms allow-discrete,
display 200ms allow-discrete;
}
dialog[open]::backdrop {
background: rgb(0 0 0 / 0.5);
}
@starting-style {
dialog[open]::backdrop {
background: rgb(0 0 0 / 0);
}
}
</style>
</head>
<body>
<button id="open">Open</button>
<dialog id="dlg" aria-labelledby="t">
<h2 id="t">Animated dialog</h2>
<form method="dialog">
<button>Close</button>
</form>
</dialog>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open')
.addEventListener('click', () => dlg.showModal());
</script>
</body>
</html>How the pieces fit
The base dialog rule describes the closed look. The dialog[open] rule describes the open look. The browser transitions between them when the open attribute changes.
The @starting-style block is used only on the first frame after the dialog opens. Without it, the dialog would already match dialog[open] when it first renders, and nothing would animate.
The display and overlay entries with allow-discrete matter on the way out. When close() removes open, display stays block and the dialog stays in the top layer for 200ms, while opacity and transform finish. Then both flip.
Give the backdrop its own transition. ::backdrop is a separate box, and it needs its own @starting-style rule.
Respecting reduced motion
Some users ask their system to minimize motion. Turn off the movement for them. Keep or drop the fade as you prefer.
@media (prefers-reduced-motion: reduce) {
dialog,
dialog::backdrop {
transition-duration: 1ms;
}
dialog {
transform: none;
}
}Common errors
- The dialog opens with no animation. The
@starting-stylerule is missing, or its selector does not match the open state. Match the selector of the final state exactly. - The dialog fades in but disappears instantly on close.
displayoroverlayis missing from thetransitionlist, or one of them lacksallow-discrete. - The backdrop pops in. The
::backdroprules need their own transition and starting style. - Nothing animates in an older browser. These features are recent. Browsers that do not support them ignore the declarations, so the dialog still works and simply appears and disappears without animation. Check current support for
@starting-style,allow-discrete, andoverlaybefore relying on the effect.
Exercise
A dialog fades in correctly, but when close() is called it disappears immediately with no fade-out. Which two properties should you add to the transition list, and with what keyword?
Show answer
Add display and overlay to the dialog's transition list, each with the allow-discrete keyword. This keeps the dialog rendered and in the top layer until the opacity transition finishes.