Skip to main content

The popover Attribute

Think of every small panel that opens above a page: a help card, a share menu, a list of filters, a notification. For years, each one needed JavaScript to open it, a lot of CSS to place it, and more code to close it when the visitor clicked elsewhere or pressed Escape. The popover attribute does all of that for you, in a few lines of HTML.

What You Will Learnโ€‹

  • The three pieces of a popover: the attribute, the button, and the content
  • The difference between auto and manual popovers
  • What the browser handles for you
  • Why a popover appears in the middle of the screen, and how to place it
  • The JavaScript API and its events
  • What happens in browsers without support
  • When a dialog or details is a better choice

The Big Ideaโ€‹

A popover is content that sits in the top layer, above the page, until the visitor dismisses it. Unlike a modal dialog, it does not block the rest of the page. It is a light, temporary layer, not an interruption.

http://127.0.0.1:5500/index.html
Help card A

An auto popover. Press Escape, or click outside it, to close it.

Help card B

Opening this one closes card A, because only one auto popover stays open.

Status banner

A manual popover. Escape and outside clicks do nothing. It closes only with its own button.

Event log (newest first)

Open A, then B. Then open the banner and click outside it, and press Escape.

These are real popovers. The few lines of script place each one under its button when it opens. Nothing here is stored or sent.

The Three Piecesโ€‹

  1. The content: any element with the popover attribute and an id. It is hidden until opened.
  2. The invoker: a button with popovertarget set to that id. Clicking it toggles the popover.
  3. The behaviour: chosen by the attribute's value, auto (the default) or manual.

Add popovertargetaction to a button to say what it does: show, hide, or toggle (the default). A "Close" button inside the popover usually uses hide.

What the Browser Does for Youโ€‹

For an auto popover, with no JavaScript:

BehaviourDetails
Hidden until openedWithout the open state, the popover is not displayed at all
Top layerIt is shown above everything, with no z-index games, and is not clipped by parent boxes
Light dismissA click outside it, or the Escape key, closes it
One at a timeOpening one auto popover closes other unrelated ones. A popover opened from inside another stays with its parent
Toggle buttonThe invoker button opens and closes it, and browsers expose its expanded state to assistive technology
Focus handlingClosing returns focus sensibly, and autofocus inside the popover is respected

Try it in the lab: open Help card A, then Help card B, and read the log.

auto or manual?โ€‹

popover (auto)popover="manual"
Outside click closes itYesNo
Escape closes itYesNo
Closes other popoversYes, unrelated onesNo
Who closes itThe browser, or the visitorOnly your buttons or script
Good forMenus, help cards, filtersStatus messages, persistent panels, script-driven panels

When in doubt, start with auto. Choose manual only when you want the panel to stay.

Why Is It in the Middle of the Screen?โ€‹

A surprise for most beginners: by default a popover is shown centred in the viewport, with its own border and padding. That is the browser's default style, and it is not attached to the button.

To change it:

  1. Reset the defaults, with margin: 0, and set your own padding and border, as in the code above.
  2. Place it near the button. You have two options.

Option 1: CSS anchor positioning (newer)โ€‹

Newer browsers can tie a popover to the button with CSS:

.menu-button { anchor-name: --menu; }

#menu {
position-anchor: --menu;
position-area: block-end span-inline-end;
inset: auto;
margin: 0;
}

Support is still spreading, and the syntax has changed over time, so check MDN before you rely on it. Where it is missing, the popover falls back to its default position, so build the rest to cope with that.

Option 2: A few lines of scriptโ€‹

On the beforetoggle event, read the button's position and set the popover's top and left. This is what the lab does:

popover.addEventListener("beforetoggle", (event) => {
if (event.newState !== "open") return;
const box = button.getBoundingClientRect();
popover.style.inset = "auto";
popover.style.margin = "0";
popover.style.top = box.bottom + 6 + "px";
popover.style.left = Math.max(8, box.left) + "px";
});

This works in every browser that supports popovers. For a popover that must stay glued to a button during scrolling or resizing, anchor positioning is better, or a small positioning library.

The JavaScript APIโ€‹

You rarely need it, but it is there:

const panel = document.getElementById("tips");

panel.showPopover(); // open
panel.hidePopover(); // close
panel.togglePopover(); // switch

panel.addEventListener("beforetoggle", (event) => {
console.log("about to become", event.newState); // "open" or "closed"
});
panel.addEventListener("toggle", (event) => {
console.log("is now", event.newState);
});

panel.matches(":popover-open"); // is it open right now?
  • beforetoggle fires before the change. When a popover is about to open, you can cancel the event to stop it, and it is a good place to prepare content or position the popover.
  • toggle fires after the change. Use it to update labels or analytics.
  • :popover-open is a CSS pseudo-class for styling the open state.
  • ::backdrop exists for popovers too, if you want to dim the page, but popovers are usually used without one.

Nested Popoversโ€‹

A popover opened by a button inside another auto popover counts as its child. Opening it does not close the parent, so you can build a menu with a sub-menu. Closing the parent closes the children.

A few honest notes about two popular uses:

  • Menus. A popover does not give you menu behaviour. A list of links inside a popover works well, and is a good, simple "disclosure" pattern. If you want arrow-key navigation like an application menu, you must add the roles and keyboard handling yourself. See the accessibility section.
  • Tooltips. A tooltip that opens on hover is difficult, because touch screens have no hover, and keyboard users need an equivalent. Prefer visible text, or a button that opens a small help card. Newer features for hover-style popovers are still being rolled out, so check support before relying on them.

Accessibilityโ€‹

A popover adds no meaning by itself. It is only a container that shows and hides, so you must supply the meaning.

  • Use real buttons as invokers, with clear text such as "Open filters", not just an icon. If it is an icon, give it an accessible name with aria-label.
  • Give the content a heading or a name, so people know what opened. Use aria-labelledby or a heading.
  • Keep it reachable by keyboard. Make sure Tab moves into the popover after the button, and that every control inside works.
  • Move focus when it helps, for example with autofocus on the first field of a small form.
  • Do not hide essential information in a popover. Anything the visitor needs, such as an error, should be visible in the page.
  • Test Escape and outside clicks, and with a screen reader if you can.
  • Make touch targets large enough, because popovers are used a lot on phones.

Support and Fallbacksโ€‹

The popover attribute is supported in current versions of the major browsers, but older versions do not have it. Check its Baseline label and compatibility table on MDN.

In a browser without support:

  • the content shows as ordinary text on the page, because the browser ignores the attribute,
  • and the button does nothing.

That is a reasonable result if the content is harmless when visible, such as a help note. Plan for it:

if (!("popover" in HTMLElement.prototype)) {
// Give older browsers a simple alternative, such as a link to a help page,
// or a tiny script that toggles a class.
}

A polyfill, which is a script that adds the feature, exists for popovers, but it adds weight. Use one only if you truly need the behaviour in older browsers.

Popover, Dialog, or Details?โ€‹

NeedBest fit
An answer is required, and the page must waitModal dialog
A temporary panel that does not block the page, and closes on outside clickPopover (auto)
A panel that stays until a button is pressedPopover (manual)
Content that expands in placeDetails and Summary
A message that needs no interactionA plain element in a live region

Checking Your Setupโ€‹

  1. Open the popover with the mouse and the keyboard. Is the button reachable, and does Enter or Space work?
  2. Press Escape, and click outside. Does an auto popover close, and a manual one stay?
  3. Open a second popover. Does the first close, as you expect?
  4. Check where focus goes when it opens and when it closes.
  5. In DevTools, look for the popover attribute, and check :popover-open in the styles panel.
  6. Test in a second browser, and with the feature unavailable, if you can.
  7. Test on a phone and at 200% zoom, and in right-to-left text.

Best Practicesโ€‹

  1. Start with popover (auto), and use manual only when needed.
  2. Use a real button with popovertarget.
  3. Reset the default margin and style, and position the popover on purpose.
  4. Give each popover a heading or an accessible name.
  5. Do not hide essential information.
  6. Keep the content small and focused.
  7. Plan for browsers without support.
  8. Use a dialog for tasks that need an answer, and details for in-place expansion.
  9. Respect reduced motion if you animate.
  10. Test with the keyboard, a phone, and a screen reader.

Quick Checklistโ€‹

  • The invoker is a real button with popovertarget
  • The content has an id, a heading or name, and a clear purpose
  • The default margin is reset, and placement is deliberate
  • Escape and outside click behave as I intended (auto or manual)
  • Nothing essential is hidden inside
  • The page still makes sense where the feature is missing
  • It works with the keyboard and on a phone
  • A popover is really the right choice

Security and Privacy Notesโ€‹

  • A popover is not a security boundary. Its content is in the page source even when it is closed. Never put secrets there.
  • Build content safely. If visitor text goes inside a popover, set it with textContent, not innerHTML. See XSS Basics.
  • The top layer can cover the whole page. That is what makes popovers useful, and it is also why a script that you did not intend to run could use one to imitate a sign-in prompt. A strict Content Security Policy and careful use of third-party scripts reduce the risk.
  • Do not imitate browser or system messages in a popover. It confuses visitors, and is a common phishing trick.
  • Third-party content inside a popover still needs sandbox and a trusted source. See iFrame Security.

Global Audience Notesโ€‹

  • Placement should follow the writing direction. Use logical properties such as inset-inline-start, and test right-to-left languages, so a popover does not open off the screen.
  • Translated text is longer. Use max-width, and let content wrap and scroll, instead of fixing a width.
  • Touch is the main input for many visitors. Make buttons comfortable to tap, and avoid anything that needs hover.
  • Older and low-cost devices may lack the feature. A page that still works without it serves everyone.
  • Clear, plain wording in the button and the content helps people reading in a second language.

Common Mistakes to Avoidโ€‹

  1. Wondering why the popover is centred, and not resetting the default margin.
  2. Using manual when auto was enough, then adding script to close it.
  3. Hiding essential information in a popover.
  4. No accessible name for an icon-only invoker.
  5. Using a popover for a task that needs an answer, instead of a dialog.
  6. Expecting menu keyboard behaviour for free.
  7. Building hover-only tooltips that fail on touch screens.
  8. Ignoring browsers without support, so the content appears inline by surprise.
  9. Relying on anchor positioning alone without a fallback.
  10. Putting secrets in a closed popover.

Practice Timeโ€‹

  1. In the lab, open Help card A, then Help card B. What does the log say about A?
  2. Open the Status banner, click outside it, and press Escape. What happens?
  3. Close the banner with its own button. What is logged?
  4. Complete the Auto or Manual? picker.
  5. Build a popover with a button and three links, and reset its margin.
  6. Place it under the button with the script above.
  7. Add an autofocus element inside it, and test the keyboard.
  8. Bonus: view your page with the popover feature unavailable, and decide what visitors should see.

Quick Recapโ€‹

Click a question to check your answer.

1. What do you need to build a popover?

An element with the popover attribute and an id, and a button with popovertarget pointing to that id.

2. What is the difference between auto and manual?

An auto popover closes on Escape, on an outside click, and when another unrelated auto popover opens. A manual popover closes only through your own buttons or script.

3. Why does a popover appear in the middle of the screen?

That is the browser's default style. Reset the margin, and place it near the button with CSS anchor positioning or a little script.

4. Is a popover modal?

No. It does not block the rest of the page. A modal dialog does.

5. What happens in a browser without popover support?

The content shows as ordinary text, and the button does nothing. Plan the content so that is acceptable, or provide a simple alternative.

6. When should you use a dialog or details instead?

A dialog when the visitor must answer before continuing. details when content expands in place.

Key Takeawaysโ€‹

  • A popover is a light, temporary layer in the top layer, opened by a button with popovertarget.
  • auto gives you light dismiss and one-at-a-time behaviour. manual leaves control to you.
  • Reset the default margin, and place it on purpose.
  • The JavaScript API and events let you prepare, position, and react.
  • A popover adds no meaning. Supply names, labels, and keyboard access.
  • Plan for missing support, and never hide essential information.
  • Choose a dialog or details when they fit better.

Next up: inert, where you will learn how one attribute switches off part of a page for the mouse, the keyboard, and screen readers.