← Back to HTML & CSS, page 5

html-css-0081 · Hard senior

What does the popover attribute give you, and how is it different from building a tooltip or menu with CSS and JavaScript?

The popover attribute plus popovertarget shows an element in the top layer with light dismiss and Escape handling, with no script. It does not make the page inert and does not position itself, so use dialog for modals. It is newer, so check support and provide a fallback.

Full answer

The popover API lets HTML show an element on top of the page without writing the show and hide code yourself. You add the popover attribute to an element, and give a button popovertarget="id" pointing at it. Clicking the button toggles the popover.

What the browser handles:

  • Top layer: the popover is drawn above other content, so you do not fight z-index or overflow: hidden on parents.
  • Light dismiss: with popover (the same as popover="auto"), pressing Escape or clicking outside closes it, and opening one auto popover closes other unrelated ones.
  • Focus and keyboard: Escape closes it, and focus returns to the invoker in typical cases.
  • Relations: the invoking button gets expanded state semantics for assistive technology in browsers that support the feature.
  • popover="manual" turns off light dismiss, so you decide when to close it. Useful for toasts.

Styling hooks: the :popover-open pseudo-class matches while it is shown. Before it is shown, the element is display: none. The ::backdrop pseudo-element exists for popovers too, but only a dialog opened with showModal() makes the page behind inert. A popover does not. Use dialog for real modals.

JavaScript can still help: showPopover(), hidePopover(), togglePopover() and the toggle event.

The popover is a newer feature, so check the support table, and keep a plain fallback for important content. Positioning near the button is not automatic. CSS anchor positioning can do it, or you can place it by hand.

Clicking Info shows the box above everything else, and a second click hides it. Pressing Escape also closes it. Clicking elsewhere closes it too.

Where the attribute is not supported, the element is simply shown as normal content, unless you hide it with a feature check, so test your fallback.

html
<style>
  [popover] { border: 1px solid #999; padding: 8px; margin: 0; }
  #tip { position: fixed; top: 60px; left: 20px; }
  #tip:popover-open { background: #ffd; }
</style>
<button id="open" popovertarget="tip" type="button">Info</button>
<div id="tip" popover>Fees are paid every month.</div>
<button id="other" type="button">Other</button>

Checked in Chrome 156 only, by clicking, pressing Escape and clicking outside. Other browsers were not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • popover and popovertarget toggle an element with no script.
  • The top layer avoids z-index and overflow problems.
  • Auto popovers close on Escape and outside clicks; manual ones do not.
  • :popover-open styles the open state.
  • A popover is not modal; use dialog with showModal() for modals.

Common mistakes

  • Using popover for a modal and expecting the background to become inert. Use dialog with showModal().
  • Using popover="manual" for a menu and forgetting to close it on Escape or outside click.
  • Assuming the popover is placed beside its button automatically.

Follow-ups

  • How does popover="manual" differ from the default?
  • Which pseudo-class matches an open popover?

Tips

  • Say what the browser provides: top layer, light dismiss, Escape and focus handling.
  • Compare it with dialog, and say where each fits.