The Popover API: Tooltips, Menus, and Dropdowns Without JavaScript Libraries

The HTML popover API explained: the popover attribute, popovertarget buttons, auto vs manual, top layer, light dismiss, styling and accessibility.

Executive Summary: Add the popover attribute to any element and point a button at it with popovertarget, and the browser gives you a toggleable layer that renders above everything, closes on outside click or Escape, closes other open popovers, and exposes the expanded state to screen readers. With Firefox 125 shipping it in April 2024, the Popover API now works in every major browser. It handles showing, hiding and stacking. Positioning the popover next to its button is still up to you, and the CSS feature for that is only just arriving.
Menu button with an open popover list of links

Every dropdown menu, account menu, share sheet and filter panel needs the same plumbing: toggle on click, close when clicking elsewhere, close on Escape, close the other open menu when this one opens, appear above sticky headers and escape any parent with overflow: hidden. Teams have written this by hand thousands of times, usually with a global click listener, a z-index of 9999 and a bug report about the menu getting clipped inside a card.

The Popover API is a set of HTML attributes and JavaScript methods for showing content on top of the page. An element with the popover attribute is hidden by default. When shown, it is placed in the browser’s top layer, the same layer modal dialogs use, above all normal content regardless of z-index or ancestor overflow. A button with popovertarget toggles it with no script.

How do you build a dropdown with zero JavaScript?

<button popovertarget="account-menu">Account</button>

<div id="account-menu" popover>
  <a href="/profile">Profile</a>
  <a href="/billing">Billing</a>
  <form action="/logout" method="post"><button>Sign out</button></form>
</div>

Click the button and the menu appears. Click it again, click anywhere outside, or press Escape, and it closes. Open a different popover and this one closes. Keyboard focus order is adjusted so pressing Tab after the button moves into the popover. The button is exposed to screen readers as expanded or collapsed. All of that comes from two attributes.

Support became universal in April 2024: Chrome and Edge shipped the API in version 114 in May 2023, Safari in version 17 that September, and Firefox in version 125.

What is the difference between auto and manual popovers?

Behavior popover or popover="auto" popover="manual"
Closes on outside click Yes No
Closes on Escape Yes No
Opening one closes other auto popovers Yes, except its own ancestors No
Several can be open at once Only a nested chain Yes
Typical use Menus, dropdowns, pickers, share sheets Toasts, persistent help panels

The “except its own ancestors” rule makes nested menus work: if a submenu popover is opened from a button inside a parent popover, the parent stays open. Opening an unrelated popover closes the whole chain.

Manual popovers suit things like toast notifications, where several may be visible at once and a click elsewhere should not dismiss them. You close them yourself with hidePopover() or a button with popovertargetaction="hide".

Buttons and methods

<button popovertarget="filters">Filters</button>                              <!-- toggle -->
<button popovertarget="filters" popovertargetaction="show">Open filters</button>
<button popovertarget="filters" popovertargetaction="hide">Close</button>

<script>
  const panel = document.getElementById("filters");
  panel.showPopover();
  panel.hidePopover();
  panel.togglePopover();
  panel.matches(":popover-open");   // true when shown
</script>

Declarative buttons are preferable where possible, because they also wire up the accessibility relationship between the button and the popover. A popover opened only through showPopover() from a click handler has no invoker, so screen readers do not get the expanded state and focus order is not adjusted; set aria-expanded yourself in that case.

Events

panel.addEventListener("beforetoggle", (event) => {
  if (event.newState === "open") loadFilterCounts();     // fetch fresh data
});

panel.addEventListener("toggle", (event) => {
  analytics.track("filters_panel", { state: event.newState });
});

beforetoggle fires before the change, and calling preventDefault() on it cancels an opening. toggle fires after.

Styling

The browser’s default popover styles center the element in the viewport with position: fixed, inset: 0 and margin: auto, give it a border and some padding, and set display: none while closed. Override what you need:

[popover] {
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  padding: 0.5rem;
  box-shadow: 0 12px 32px rgb(15 23 42 / 0.18);
}

[popover]::backdrop {
  background: transparent;   /* auto popovers get a backdrop too; keep it invisible for menus */
}

#account-menu:popover-open {
  display: grid;
  gap: 0.25rem;
}

Be careful with display: setting display: grid on the popover unconditionally overrides the hidden state, and the popover is always visible. Apply layout display values only under :popover-open, as above.

Animating open and close

Animating from display: none used to be impossible in CSS. Two new features fix that: @starting-style defines the values to transition from when an element first appears, and transition-behavior: allow-discrete lets display and overlay stay in place until an exit transition finishes. Chrome supports both, and Safari added them in 17.4 in March 2024. In Firefox, the popover simply appears without the animation, which is a fine fallback.

[popover] {
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 150ms, transform 150ms, display 150ms allow-discrete, overlay 150ms allow-discrete;
}

[popover]:popover-open {
  opacity: 1;
  transform: none;
}

@starting-style {
  [popover]:popover-open { opacity: 0; transform: translateY(-4px); }
}

How do you position a popover?

Because popovers default to position: fixed and appear centered, a dropdown needs to be moved next to its button. Today there are three options:

  • Measure and place with JavaScript. In the toggle event, read the button’s getBoundingClientRect() and set top and left on the popover, with inset: auto and margin: 0 to remove the centering. Reposition on scroll and resize while open.
  • Use a positioning library such as Floating UI for flipping and collision handling. Its job is now only positioning; the Popover API handles everything else it used to need code for.
  • CSS anchor positioning, which lets you write position-anchor and position-area to tether the popover to its button in CSS, ships in Chrome 125 this month. Safari and Firefox have not shipped it yet, so treat it as an enhancement for now.

Is the Popover API accessible?

  • A popover has no role of its own. The popover attribute controls behavior, not semantics. A list of navigation links in a popover is still just links, which is often exactly right. Only add role="menu" if you also implement the full ARIA menu keyboard pattern with arrow keys; an incomplete menu role is worse than none.
  • Focus is not moved automatically. Unlike a modal dialog, opening a popover leaves focus on the button. The next Tab goes into the popover. For pickers where the user must interact immediately, move focus in the toggle event.
  • It is not modal. The rest of the page stays interactive. For content that must block the page, such as a confirmation, use a <dialog> with showModal(), as described in the dialog element guide.
  • Hover tooltips still need script. popovertarget responds to clicks and key presses, not hover or focus. For tooltips, show and hide a manual popover from mouseenter, focus, mouseleave and blur, and close it on Escape to meet WCAG 1.4.13.

Popover or dialog?

Need Use
Menu, dropdown, date picker, share sheet popover
Toast notifications popover="manual"
Confirmation that must be answered <dialog> with showModal()
Form in an overlay, with the page blocked <dialog> with showModal()
Non-blocking panel that should still be a dialog semantically <dialog popover>, combining both

Frequently Asked Questions

What is the HTML Popover API?

It is a set of HTML attributes and JavaScript methods for showing content above the page. An element with the popover attribute is hidden until shown, then rendered in the top layer, and auto popovers close on outside click or Escape. A button with popovertarget toggles it without JavaScript.

Which browsers support the Popover API?

All major browsers. Chrome and Edge added it in version 114 in May 2023, Safari in version 17 in September 2023, and Firefox in version 125 in April 2024.

What is the difference between popover auto and manual?

Auto popovers, the default, close when the user clicks outside or presses Escape, and opening one closes other unrelated auto popovers. Manual popovers only close when your code or a hide button closes them, and several can be open at once.

Should I use a popover or a dialog?

Use a popover for non-modal UI such as menus, dropdowns and pickers, where the rest of the page stays usable. Use a dialog opened with showModal() when the user must respond before continuing, such as a confirmation or a blocking form.

The plumbing is now built in

The hard, repetitive parts of dropdowns, menus and pickers, which are stacking, outside clicks, Escape, one-at-a-time behavior and expanded state, are now an attribute. Use popovertarget buttons wherever you can, keep layout styles under :popover-open, position with a little JavaScript or a library until anchor positioning lands everywhere, and reach for <dialog> when the interaction should block the page. For tethering a popover to its button in CSS, see the anchor positioning guide. For the full reference, see the MDN Popover API guide and the popover section of the HTML Standard. Related reading on this site: details and summary.

Originally posted on 14 May 2024 and post last modified on 14 May 2024

Accessibility Dropdowns Front-End Development HTML5 Popover API

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *