Sub topic 8
Top-layer elements and the overlay property
This chapter assumes you know how @starting-style supplies an entry style, and how transition-behavior: allow-discrete lets display take part in a transition. You should also know the popover attribute and the dialog element at a basic level.
What the top layer is
The top layer is a browser-managed layer that paints above all other page content. Elements in it ignore z-index and ignore ancestor overflow clipping. The browser puts an element there in these cases:
- A popover is shown with
showPopover(), a popover button, orpopovertarget. - A dialog is opened with
showModal(). - An element enters fullscreen.
The top layer is separate from display. An element can have display: block and still not be in the top layer. When an element closes, the browser removes it from the top layer immediately.
Why exits get cut off
Take a popover that fades out over 0.3 seconds. Two things happen at the moment of closing:
displayflips tonone. Withallow-discrete, you can delay this flip until the transition ends.- The element leaves the top layer right away.
After step 2, the element is a normal element in the page. It can be clipped by an ancestor, hidden behind a sibling, or drawn at the wrong position. The fade-out then plays in the wrong place, or you don't see it at all.
The overlay property
overlay is a property that records whether an element is currently in the top layer. It has two values: auto (in the top layer) and none (not in it).
You cannot set overlay in your own rules, because the browser controls it. You can, however, transition it. The browser treats overlay as a discrete property, so with allow-discrete it keeps its old value of auto until the transition finishes. The element then stays in the top layer for the whole exit animation and leaves when the animation ends.
The pattern
Add overlay and display to the transition list, each with allow-discrete:
[popover] {
opacity: 0;
transition:
opacity 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
}
}Here is the matching HTML:
<button popovertarget="tip">Show tip</button>
<div id="tip" popover>Hello from the top layer.</div>The durations on overlay and display should match the longest of your visual transitions. If opacity takes 0.3s and overlay takes 0.1s, the element leaves the top layer after 0.1s and the rest of the fade is cut off.
The transition shorthand resets transition-behavior for every property it lists. Put allow-discrete inside each item, as above. If you set transition-behavior separately, put it after the shorthand.
Common mistakes
| Symptom | Likely cause |
|---|---|
| The popover fades in but vanishes on close | `overlay` is missing from the transition list |
| The exit is clipped by a parent | The `overlay` duration is shorter than the visual transition |
| `display` flips at once | `allow-discrete` is missing on the `display` item |
| Entry does not animate | The `@starting-style` selector does not match the open state |
The last row matters because the starting style must match the element in its open state, such as :popover-open or [open]. A starting style written for the closed state is never used.
Exercise
A popover fades in correctly, but when closed it disappears instantly. Its CSS has transition: opacity 0.3s, display 0.3s allow-discrete;. What should you add, and why?
Show answer
Add overlay 0.3s allow-discrete to the transition list. Without it, the element leaves the top layer as soon as it closes, so the fade-out has no chance to play where it can be seen. Transitioning overlay keeps it at auto until the transition ends.
Exercise
Why can't you write overlay: auto in your own stylesheet to keep an element in the top layer?
Show answer
The browser controls overlay, so authors cannot set it. You can only list it in transition, which delays the browser's change from auto to none.