3.1 · Popovers & positioning
Popovers without JavaScript
Menus and toasts that open, close and sit on top with one attribute.
Add popover to any element and
popovertarget="id" to a button. The browser handles the
rest: opening and closing, Esc to dismiss, clicking outside to dismiss
(called light dismiss), focus, and accessibility wiring.
Open popovers render in the top layer, above the rest
of the page, so z-index and
overflow: hidden on ancestors can’t clip them.
popover="manual" turns off light dismiss, which suits
toasts.
button.onclick = () => menu.hidden = !menu.hidden
document.onclick = (e) => { /* outside? */ }
document.onkeydown = (e) => { /* Esc? */ }
/* + z-index: 9999, aria-expanded, focus… */
<button popovertarget="menu">Menu</button>
<div id="menu" popover>…</div>
#menu::backdrop {
background: rgb(0 0 0 / 0.3);
}