Sub topic 8
Top-layer elements and the overlay property
This chapter assumes you know how @starting-style supplies entry values, and how transition-behavior: allow-discrete lets display take part in a transition. You should also be comfortable with the popover attribute and the <dialog> element. Here we look at why elements in the top layer need one more property, overlay, before their exit animations will play.
What the top layer is
Normally the paint order of a page follows the stacking rules: z-index, stacking contexts, and DOM order. An element inside a container with overflow: hidden or a low z-index cannot escape that container, however large its own z-index is.
The top layer is a separate rendering layer that sits above the whole document. Elements in it paint above everything else, and they are not clipped by ancestors' overflow or affected by ancestors' stacking contexts. The browser, not your CSS, decides which elements are in it.
An element enters the top layer when:
- A popover is shown, with
showPopover(),togglePopover(), or apopovertargetbutton. - A
<dialog>is opened withshowModal(). A dialog opened withshow()or theopenattribute is not in the top layer. - An element enters fullscreen with
requestFullscreen().
When several elements are in the top layer, the most recently added one is on top. Each top-layer element can also have a ::backdrop pseudo-element. The backdrop is painted directly below its element and above the rest of the page.
Why exits break without help
Entry animations work with @starting-style and a transition on display, as in the previous chapters. Exits are harder, because closing a top-layer element does two things at once:
- Its
displaybecomesnone. - The browser removes it from the top layer.
With transition-behavior: allow-discrete on display, the first problem is solved. The element stays rendered for the transition's duration. The second problem is not solved by that. The browser removes the element from the top layer at once, so during the fade-out it is drawn in its normal place in the page, perhaps under other content or clipped by a parent. The animation either looks wrong or is invisible.
The overlay property fixes this.
The overlay property
overlay is a property that reports whether an element is currently rendered in the top layer. Its values are none and auto. The browser sets it to auto while the element is in the top layer and to none otherwise.
Two facts shape how you use it:
- You cannot set it yourself. It is controlled by the user agent, so writing
overlay: autoin a rule has no effect. You can only name it in a transition. - It is a discrete property. Its value flips between
noneandautowith no in-between values, so it only transitions when you opt in withallow-discrete.
If you transition overlay with allow-discrete, the browser holds the old value (auto) for the transition's duration and flips to none only when the transition ends. The element stays in the top layer until then. display works the same way: with allow-discrete, a change to none is held back until the end of the transition.
The result is that your element keeps its top-layer position, with its backdrop, for the whole exit animation, then is removed.
A complete popover example
This page is complete and runnable in a Chromium-based browser that supports overlay.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Popover exit animation</title>
<style>
[popover] {
opacity: 0;
transform: scale(0.95);
transition:
opacity 0.3s,
transform 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
transform: none;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: scale(0.95);
}
}
@media (prefers-reduced-motion: reduce) {
[popover] {
transition-duration: 0.01ms;
}
}
</style>
</head>
<body>
<button popovertarget="tip">Show tip</button>
<div id="tip" popover>Popovers live in the top layer.</div>
</body>
</html>Read the rules in this order:
- The base rule
[popover]describes the closed state: transparent and slightly smaller. It also lists all four transitions. :popover-opendescribes the open state.- The
@starting-styleblock gives the entry transition its before-change values. The popover has just changed fromdisplay: noneto displayed, so there is no earlier style to start from. - On exit, the browser computes the closed values from the base rule.
opacityandtransformanimate to them.displayandoverlaykeep their old values until the 0.3 s ends.
The shorthand overlay 0.3s allow-discrete means: property overlay, duration 0.3 s, behavior allow-discrete. You can also write the behavior as a separate transition-behavior declaration.
Timeline of an exit
The diagram shows what happens between a close request and the element's removal.
The matching rule for durations
display and overlay must stay in place at least as long as the visible animation. If one finishes earlier, the element vanishes or leaves the top layer before the fade ends.
The safest approach is to use one duration for all four properties, as in the example. If your opacity fade lasts 0.3 s and your transform lasts 0.5 s, set display and overlay to 0.5 s, the longest one.
Delays count as well. A transition-delay on opacity extends the visible animation, so give display and overlay the same delay or a longer total time.
A shortcut with transition-property: all
If you want every changing property to animate, you can use a single declaration:
dialog {
transition: all 0.3s allow-discrete;
}With transition-property: all and allow-discrete, the discrete properties display and overlay are included. This is short and keeps the durations in sync. The cost is that any other property that changes will also transition, including ones you did not plan to animate. For components that change many properties, list the properties explicitly.
Dialogs
A modal dialog follows the same rules, and its ::backdrop needs its own transitions. The backdrop is a separate pseudo-element, and it has its own display and overlay values (covered in the next chapter), so it gets its own @starting-style and transition list. For a dialog with no backdrop styling, only the dialog itself needs the pattern:
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;
}
}The [open] attribute selector works because the browser sets the open attribute when showModal() runs and removes it on close(). Pressing Escape also closes a modal dialog, and the same exit transition runs.
Browser support and fallback
overlay is a newer property, and browser support is not uniform. Check a current compatibility table, such as the one on MDN, before relying on it.
When a browser does not recognize overlay in a transition list, the declaration is handled in one of two ways:
- If you wrote
overlayin the sametransitionshorthand as the other properties, a browser that fails to parseoverlayas a property may drop the whole declaration. Check how your target browsers treat unknown names intransition-property. Unknown property names in a transition list are normally accepted, so this is rarely a problem, but test it. - If the browser supports
allow-discretebut notoverlay, the element's exit still animates. It just may not be in the top layer during the fade, so it can be covered or clipped.
For the strongest fallback, keep the base CSS usable without the animation. The popover or dialog should work correctly when the transition does nothing. An exit animation is an enhancement, not a requirement.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Exit fade never shows | `display` is not in the transition list, or lacks `allow-discrete` | Add `display 0.3s allow-discrete` |
| Exit plays but element is hidden behind other content | `overlay` is missing from the transition list | Add `overlay 0.3s allow-discrete` |
| Element pops out before the fade ends | `display` or `overlay` duration is shorter than the longest visible animation | Match the durations |
| Setting `overlay: auto` changes nothing | `overlay` is user-agent controlled | Only reference it in `transition` |
| Backdrop vanishes instantly while the dialog fades | The `::backdrop` has no transition of its own | Add its own `background-color`, `display`, and `overlay` transitions |
| Nothing animates on open | Missing `@starting-style` for the open state | Add a starting style for the open selector |
Respecting reduced motion
Users who ask for reduced motion should not see large movement. Setting transition-duration: 0.01ms in a prefers-reduced-motion: reduce block keeps the display and overlay logic working. The state changes still complete, and the transitions end almost at once. Using transition: none instead is also valid for these properties, but it removes the timing work done by display and overlay, and the element is removed right away. That is fine for reduced motion, as long as you have tested it.
Exercise
A modal dialog fades in correctly but, on close, it disappears instantly. Its CSS lists transition: opacity 0.3s, display 0.3s allow-discrete; on dialog. What is missing, and what visible problem does it cause besides the lack of a fade?
Show answer
overlay 0.3s allow-discrete is missing from the transition list. Without it, the dialog is removed from the top layer immediately on close, even though display is held for 0.3 s. During the fade-out the dialog is drawn in normal page order, where it can be covered or clipped by other content. The backdrop also needs its own display and overlay transitions.
Exercise
A popover's opacity transition lasts 0.4 s, transform lasts 0.6 s, and display and overlay both last 0.4 s. What goes wrong on close, and how do you fix it?
Show answer
The transform animation is still running when display and overlay complete at 0.4 s, so the popover leaves the top layer and becomes display: none before the movement finishes. Set display and overlay to 0.6 s, the longest visible duration, or make the three durations equal.