Sub topic 3
Why popovers appear above everything else: the top layer
This chapter explains the browser mechanism that puts a popover in front of the rest of the page, and what that means for your CSS. It assumes you can write basic HTML and CSS and know how position, overflow, and z-index behave. It also assumes you have seen the popover attribute and a popovertarget button from earlier chapters.
The problem the top layer solves
Before the popover attribute, a dropdown or tooltip was usually an absolutely positioned div. It broke in predictable ways:
- An ancestor with
overflow: hiddenclipped it. - A
z-indexof 9999 lost to a sibling whose ancestor created a higher stacking context. - You had to move the element to the end of
<body>by hand, or render it in a "portal", to escape its parent.
The top layer removes all three problems. It is a browser-managed list of elements that are painted above the rest of the document. An element in that list ignores the clipping and stacking rules of its DOM ancestors.
What the top layer is
The top layer is a part of the page's rendering, not a DOM element. It sits above the root element's normal painting, including everything with a z-index. Three kinds of element can enter it:
- Elements shown with the Fullscreen API.
- Modal dialogs opened with
showModal(). - Popovers that are currently showing.
A popover joins the top layer when it opens and leaves it when it closes. A popover that is hidden is display: none and is not in the layer. A showing popover matches the :popover-open pseudo-class.
Your DOM does not change. The <div popover> stays exactly where you wrote it. Only the rendering moves.
How a top-layer element is rendered
Three rules cover most of what you will run into.
- Ancestor clipping does not apply. The browser renders a top-layer element as if it were a child of the viewport, so
overflow: hidden,overflow: clip, andcontain: painton ancestors do not cut it off. - Ancestor stacking contexts do not apply. An ancestor's
z-index,opacity, ortransformdoes not trap the element, and neither does afilter. - Order of insertion decides stacking. Among top-layer elements, the one added last is painted on top.
z-indexhas no effect between them.
Inheritance still follows the DOM. A popover inside a <section> with color: teal inherits that color. It also inherits custom properties and fonts. Selectors such as section > .tip still match. The element has left the rendering tree's position, not the DOM tree.
Seeing the difference
This page puts an old-style absolutely positioned tip and a popover inside the same clipping card.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Clipping versus the top layer</title>
<style>
.card {
position: relative;
width: 16rem;
height: 5rem;
overflow: hidden;
border: 2px solid #444;
padding: 0.5rem;
}
.old-tip {
position: absolute;
top: 3rem;
left: 1rem;
width: 12rem;
height: 5rem;
background: #ffe9a8;
z-index: 9999;
}
</style>
</head>
<body>
<div class="card">
<button popovertarget="new-tip">Open popover</button>
<div class="old-tip">Old tip: clipped by the card</div>
<div id="new-tip" popover>New tip: not clipped</div>
</div>
</body>
</html>The old tip is cut off at the card's border even with z-index: 9999. When you open the popover, its full box appears. By default it is centered in the viewport, not near the button. Positioning is covered in a later chapter.
Default styles that come from the top layer
The browser's built-in style sheet gives [popover] elements rules that suit top-layer rendering. They are approximately these:
[popover] {
position: fixed;
inset: 0;
width: fit-content;
height: fit-content;
margin: auto;
border: solid;
padding: 0.25em;
overflow: auto;
color: CanvasText;
background-color: Canvas;
}
[popover]:not(:popover-open) {
display: none;
}The position: fixed and inset: 0 pair, combined with margin: auto, explains the centering. Fixed positioning for a top-layer element is relative to the viewport. A transform on an ancestor does not change that, which is a common surprise with non-top-layer fixed elements.
The display: none rule explains why a closed popover is invisible. If you write display: flex on [popover] without scoping it to :popover-open, your rule overrides the hiding rule and the popover is always visible. Scope display changes like this:
[popover]:popover-open {
display: flex;
flex-direction: column;
gap: 0.5rem;
}Stacking order among popovers
Because order of insertion decides stacking, a later popover covers an earlier one. Giving the earlier one a higher z-index does not help. This page shows it with two manual popovers, which do not close each other.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Top layer order</title>
<style>
.box {
width: 12rem;
height: 6rem;
margin: 0;
padding: 1rem;
}
#a { inset: 40% auto auto 20%; z-index: 9999; background: #cde; }
#b { inset: 45% auto auto 25%; background: #fdc; }
</style>
</head>
<body>
<button popovertarget="a">Toggle A (z-index 9999)</button>
<button popovertarget="b">Toggle B (no z-index)</button>
<div id="a" class="box" popover="manual">Popover A</div>
<div id="b" class="box" popover="manual">Popover B</div>
</body>
</html>Open A, then B: B covers A. Close A and open it again: A now covers B, because it was added to the top layer last. The z-index: 9999 on A never mattered.
When one popover opens, the browser puts it on top of everything already in the layer. You cannot reorder the layer from CSS. To change the order, close the popovers and open them again in the order you want.
The backdrop
Each element in the top layer can have a ::backdrop pseudo-element. It is a box the size of the viewport, painted directly beneath the element in the layer. Popovers have a transparent backdrop by default, unlike modal dialogs, which get a faint dark one. You can style it:
[popover]::backdrop {
background: rgb(0 0 0 / 0.2);
}You cannot style ::backdrop through inheritance from the popover. Custom properties set on the popover are not visible inside the backdrop rule. Set them on :root, or write the values directly.
A backdrop on an auto popover does not make the popover modal. Content behind it can still be reached with the keyboard. A click on the backdrop counts as an outside click, so it closes an auto popover. The next chapters cover that behavior.
Edge cases and gotchas
- Modal dialogs and inertness. While a modal
<dialog>is open, everything outside it is inert. A popover opened from inside the dialog works, as long as it is a descendant of the dialog. Keep popovers meant for dialog content inside the dialog's markup. - Exit animations. When a popover closes, the browser removes it from the top layer and applies
display: noneright away. A transition would therefore have no time to run. The animation chapter shows howoverlayanddisplaywithallow-discretedelay that removal. - Ancestors do not matter, but descendants do. A popover's own
overflow: autostill clips its own content. If you add a pseudo-element arrow outside the box,overflow: autoon the popover will cut it off. - No
z-indexis needed. Addingz-indexto a popover to "make sure" it is visible does nothing useful and hides the real ordering rule from the next reader of your code. - Fixed ancestors. A popover inside a sticky or fixed header still renders against the viewport, not the header.
Inspecting the layer
Chrome DevTools shows a #top-layer node at the end of the Elements panel. It lists each element currently in the layer, and each entry links back to the element's real place in the DOM. If a popover seems to be missing, check that it is listed. If it is not, it was never shown, or it was hidden by another popover opening.
You can also check from script:
const tip = document.getElementById('new-tip');
tip.showPopover();
console.log(tip.matches(':popover-open')); // true, so it is in the top layer
tip.hidePopover();
console.log(tip.matches(':popover-open')); // false, so it has left the top layerSummary
The top layer is the reason a popover needs no z-index, no portal, and no escape from overflow: hidden. Keep four rules in mind: the DOM position is unchanged, ancestor clipping and stacking do not apply, later insertion paints on top, and inheritance still follows the DOM.
Exercise
A popover sits inside a div with overflow: hidden and transform: scale(0.9). You give the popover z-index: 1. A sibling div elsewhere on the page has z-index: 100. Which of these three settings affect how the open popover is painted, and why?
Show answer
None of them. An open popover is in the top layer, so it is not clipped by the ancestor's overflow: hidden, not scaled or trapped by the ancestor's transform, and not ranked against the sibling's z-index. The popover paints above the sibling regardless of z-index values.
Exercise
You open popover A (popover="manual"), then popover B (popover="manual"), and they overlap. A has z-index: 500 and B has no z-index. Which one is on top? What do you do to bring A to the front?
Show answer
B is on top, because the element added to the top layer last is painted last, and z-index does not apply between top-layer elements. To bring A to the front, call hidePopover() on A and then showPopover() again, which adds it to the layer after B.
Exercise
You add [popover] { display: grid; } to your style sheet, and every popover is now visible on page load. What happened, and what is the fix?
Show answer
Your rule overrides the browser's [popover]:not(:popover-open) { display: none; } rule, so closed popovers are no longer hidden. Move the rule to [popover]:popover-open { display: grid; }.