Sub topic 3
Why popovers appear above everything else: the top layer
This chapter assumes you know basic HTML and CSS, including how z-index and stacking contexts work, and that you can add event listeners in JavaScript. It explains the top layer, the browser feature that lets a popover appear above the rest of the page.
The problem the top layer solves
Before popovers, a menu or tooltip had to fight for visibility with z-index. A z-index value only competes inside its own stacking context. A parent with transform, opacity below 1, filter, or position plus z-index creates a new stacking context. A child inside it can never rise above a sibling of that parent, whatever number you give it. A parent with overflow: hidden can also clip the child.
The top layer removes this problem for popovers. It is a browser-managed layer that sits above the whole document. Elements in it are painted over everything else, and ancestors cannot clip or reorder them.
How an element enters the top layer
A popover enters the top layer when it opens. When it closes, it leaves. You never place it there yourself.
<button popovertarget="tip">What is this?</button>
<div id="tip" popover>
<p>This box is painted in the top layer while it is open.</p>
</div>While #tip is closed, it is not rendered at all, because the browser's default style for [popover]:not(:popover-open) is display: none. When you click the button, the browser shows the element and adds it to the top layer.
<br>
Two details follow from this:
- The element can sit deep inside a clipped, transformed, or low
z-indexcontainer in your HTML. Its position in the DOM does not affect its paint order. - No
z-indexis needed or used. If several top-layer elements are open, the one added most recently is painted on top.
What the top layer does not change
The top layer controls paint order. It does not change the following:
- Inheritance. The element is still a child of its DOM parent, so inherited properties such as
colorandfont-familystill flow down to it. - Event bubbling. Events bubble through its DOM ancestors as usual.
- Layout position. A popover is taken out of normal flow and does not start next to its button. By default, the UA stylesheet centers it in the viewport. Placing it near the invoker is covered in a later chapter.
Seeing the top layer in action
This example puts a popover inside a container that would normally clip and trap it.
<div style="overflow: hidden; height: 40px; transform: scale(1); position: relative; z-index: 0;">
<button popovertarget="escape">Open</button>
<div id="escape" popover>
I am not clipped by the container.
</div>
</div>Without popover, a child in this container would be cut off at 40px. With popover, the box shows in full when opened.
Styling the backdrop
Elements in the top layer can have a ::backdrop pseudo-element. This is a box the size of the viewport, painted directly behind the element and above everything else. Popovers do not dim the page by default, but you can add it:
[popover]::backdrop {
background: rgb(0 0 0 / 0.2);
}Popovers are non-modal. The page behind the backdrop stays interactive. A backdrop on an auto popover also receives outside clicks, which is how light dismiss closes it. Light dismiss is covered in a later chapter.
Common errors
- Adding
z-indexto fix a popover. It has no effect on stacking against normal content, because the popover is already above it. - Expecting
display: noneto be removed by your own styles. If you setdisplay: blockordisplay: flexdirectly on[popover], you override the default hiding rule and the popover is always visible. Put that style on[popover]:popover-openinstead. - Assuming the DOM position sets the position. The top layer sets paint order only. Use CSS to place the box.
Exercise
A popover sits inside a parent that has overflow: hidden and transform: translateZ(0). Will the open popover be clipped or hidden behind a sibling that has z-index: 9999? Explain why.
Show answer
No. When the popover opens, the browser moves it into the top layer, which is painted above all normal page content regardless of ancestors, stacking contexts, clipping, or z-index values. The parent's overflow and transform do not affect it.