Agent Wranglers

WEB-25Recommended

Build menus with popover

Build menus, tooltips and simple overlays with the built-in popover attribute, instead of JavaScript modal code.

How to do it

Mark the menu with popover and point a button at it with popovertarget. The browser handles closing on outside click and Escape, focus return and the top layer. Popover works in every major browser since 2024. Style the backdrop with ::backdrop.

Examples

HTML example

<button popovertarget="menu">Menu</button>
<nav popover id="menu">
  <a href="/">Home</a>
  <a href="/pricing">Pricing</a>
</nav>

How to check

A browser test focuses the button and presses Enter. The menu matches :popover-open. After Escape it no longer matches, and focus is back on the button.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/interface/popover-api">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.