Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

What does the popover attribute give you, and how is it different from <dialog>?

Questions in this chapter (7)
  • Draft
  • medium
  • Askedoften
  • Verified October 2026

Answer

popover is a global attribute: any element can have it. A popover is hidden until shown, then rendered in the top layer, above all z-index stacking and outside any overflow clipping. You show it without script from a button, <button popovertarget="menu">, with popovertargetaction="show", "hide" or the default "toggle"; or with showPopover(), hidePopover() and togglePopover(). :popover-open matches while it's shown, and beforetoggle and toggle events report oldState and newState.

Its value picks the behaviour:

  • auto (also popover with no value): light dismiss, meaning a click outside or Escape closes it. Opening one closes every other open auto popover that isn't its ancestor. A popover opened from inside another, or nested in its DOM, keeps the outer one open, so menus and submenus work.
  • manual (and any invalid value): no light dismiss, no Escape, closes nothing else. Use it for toasts and anything you close yourself.
  • hint: light dismiss like auto, but opening it closes only other hints, not auto popovers. It's meant for tooltips. Chrome 151 and Firefox 153 support it; Safari doesn't yet.

How it differs from <dialog>:

  • A popover is never modal. The page stays clickable and focus stays where it was; in tab order, the popover's contents come right after the button that opened it. showModal() makes the page inert and moves focus inside.
  • A popover has no role of its own and no returnValue or method="dialog". It's behaviour you add to an element that keeps its own semantics.
  • <dialog> has no light dismiss unless you set closedby="any".

Pick showModal() for anything that must block the page until answered. Use popover for menus, pickers, tooltips and toasts. <dialog popover> gives a non-modal dialog with light dismiss; its open stays false, so check :popover-open.

Example

<button popovertarget="menu">Menu</button>
<div id="menu" popover>
  <a href="#profile">Profile</a>
  <button popovertarget="help">Help</button>
</div>
<div id="help" popover>Keyboard shortcuts…</div>
<div id="toast" popover="manual">Saved</div>
<script>
  document.addEventListener("toggle", (e) => console.log(e.target.id, e.newState), true);
  document.querySelector("#toast").showPopover();
  // On load:              → toast open
  // Click "Menu":         → menu open
  // Click "Help":         → help open
  // Press Escape:         → help closed
  // Click "Help" again:   → help open
  // Click the page:       → help closed
  //                       → menu closed
  // Press Escape:         (nothing: the toast is manual)
</script>

Chrome 155 and Firefox 158 logged the same sequence. help opened from inside menu, so menu stayed open; Escape closed only the topmost popover.

Pitfalls

  • Expecting a popover to block the page. A click outside reaches its target and light-dismisses the popover: both browsers fired the outside button's click handler while closing it.
  • Putting popovertarget on a type-less button inside a form. It's a submit button, so it submits instead of toggling; add type="button".
  • Calling showPopover() on an element without the attribute. It throws NotSupportedError; on an already-open popover it does nothing.
  • Writing a typo like popover="atuo". Invalid values mean manual, so it silently loses light dismiss; el.popover returned "manual" in both browsers.

Follow-ups

How do you position a popover next to its button?
By default it's position: fixed and centred by inset: 0; margin: auto. Anchoring it to the button is a CSS job, done with anchor positioning or script; the popover attribute itself only handles showing, stacking and dismissal.
Can you stop a popover from opening?
Yes: beforetoggle is cancelable when opening. It isn't when closing (cancelable was false in both browsers), so a light dismiss can't be refused.
What do invoker commands add?
commandfor="id" with command="toggle-popover", "show-popover" or "hide-popover" does what popovertarget does, and the same pair also drives dialogs ("show-modal", "close") and custom --commands. Chrome 135, Firefox 144 and Safari 26.2 support it.

Exercises

Which popovers are open at the end?

// <div id="a" popover>…</div> <div id="b" popover="hint">…</div> <div id="c" popover>…</div>
a.showPopover();
b.showPopover();
c.showPopover();

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.