The top layer is an ordered set of elements that the browser paints after the document. When an element is rendered in it, CSS Positioned Layout 4 says:
- It generates boxes as if it were a sibling of the root element, not where it sits in the DOM.
- It's its own stacking context whose parent is the root stacking context, painted atomically above the document. Ancestors'
overflow,opacity,transform,filterandz-indexcan't touch it. - Its containing block is the viewport if it's
position: fixed, otherwise the initial containing block. Apositionother thanabsoluteorfixedcomputes toabsolute. - Top-layer elements are painted in the order they were added: the last one opened is on top.
z-indexdoesn't reorder them.
Authors can't put elements there directly. The HTML and Fullscreen APIs do it: dialog.showModal(), showPopover() (or a popovertarget button), and requestFullscreen(). dialog.show() and the open attribute don't; that dialog is laid out in place and gets buried like anything else.
Each element rendered in the top layer has a ::backdrop pseudo-element, painted directly beneath it and above everything earlier. The UA gives it position: fixed; inset: 0; HTML styles a modal dialog's backdrop rgba(0, 0, 0, 0.1) and a popover's transparent with pointer-events: none.
Leaving is the subtle part. Closing takes the element out of the top layer at the next rendering update, which cuts off exit animations. The overlay property, set only by the browser, says whether an element in the top layer is actually rendered there. Authors can transition it with transition: overlay 0.3s allow-discrete (alongside display) so the element stays on top until its fade-out finishes. Chrome supports overlay since 117; Firefox 158 and Safari don't, and there the element drops out of the top layer immediately.