Page 03: The overlay
Retires z-index: 99999, the positioning library and the focus
trap
Putting one box on top of another was the hardest easy thing on the web.
A tooltip has to be drawn above everything else, so it could not live
where it belonged in the markup. Any ancestor with
overflow: hidden would cut it off, and any ancestor that
formed a stacking context would cap its z-index no matter how many nines
you typed. Libraries got round this by moving the element to the end of
the body with JavaScript, measuring the trigger, positioning the tooltip
by hand, and listening for scroll and resize in order to do it all
again.
Three additions took that apart. The top layer is a plane above the
whole document that z-index cannot reach and overflow cannot clip. The
popover attribute puts an element there, and gives it light
dismiss, the Escape key and focus return. Anchor positioning ties it to
the thing it points at.
Out of a clipped box
The box below clips whatever is inside it. Both buttons open a menu that lives inside the box. The first is the old approach without its JavaScript rescue: an absolutely positioned child with a z-index of 9999. The second is a popover.
Your browser does not know the popover attribute, so the
second menu below is simply printed in place and the box around it has
been left unclipped. In a current browser the box clips its contents,
the first menu is cut off by it and the second one is not.
Open with z-index: 9999
document.body.appendChild(menu);
const r = btn.getBoundingClientRect();
menu.style.top = r.bottom + scrollY + "px";
menu.style.left = r.left + "px";
addEventListener("scroll", reposition);
addEventListener("resize", reposition);
addEventListener("keydown", onEscape);
addEventListener("click", onOutside);
<button popovertarget="menu">Open</button>
<div id="menu" popover>...</div>
#menu {
position-anchor: --button;
position-area: bottom span-right;
position-try-fallbacks: flip-block;
}
Notes that find their own place
Anchor positioning reached the third engine when Firefox 147 shipped it. A positioned element names an anchor and says which side of it to sit on. It can also list fallbacks, and the browser tries them in order when the first choice would run off the screen.
This paragraph has three notes in it. Open the first one
I am anchored to the red marker with position-area: top.
Scroll until the marker is near the top edge of the window and open me
again: position-try-fallbacks: flip-block will put me
underneath it instead.
when its marker is in the middle of your window, then again when the
marker is close to the top edge. The second
I fade in from a @starting-style rule, and I can fade
out because display is allowed to wait for the transition
with allow-discrete. Firefox does not transition
display yet, so there I arrive with the fade and leave
without it.
is about how a note appears and disappears. The third
Without the popover attribute I am an ordinary span and
sit in the sentence as small grey text. Without anchor positioning I
open in the middle of the window, which is the browser's default for a
popover. Either way nothing is lost.
is about what you see when none of this is supported.
Your browser does not position by anchor yet, so these notes open in the centre of the window.
The modal
A modal dialog makes one more promise: while it is open, the rest of the
page is out of reach. Doing that by hand meant trapping the Tab key
inside the dialog, hiding everything else from screen readers, and
putting focus back afterwards. The dialog element does all three. What
it lacked was a way to open it as a modal without calling
showModal() from a script. Invoker commands are that way: a
button names its target and the command to send it. They are in Chrome
135, Safari 26.2 and Firefox 144.
There are two openers in the markup and you were shown one of them. CSS
cannot ask whether a browser understands an HTML attribute, so the
stylesheet asks about something else: every engine shipped
sibling-index() after invoker commands or in the same
release. Where that test passes you get the command button and a real
modal. Where it fails you get a link to #modal, and the
dialog opens itself with :target, as lightboxes did fifteen
years ago. That version is an overlay and says so. The
link is here if you want to compare the two.