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
autoandmanualpopovers - 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
detailsis 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.
- Popover Lab
- Auto or Manual?
- The Code
An auto popover. Press Escape, or click outside it, to close it.
Opening this one closes card A, because only one auto popover stays open.
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.
Scenario 1 of 6: A help card that should close when the visitor clicks elsewhere or presses Escape.
<!-- 1. The button that opens it -->
<button type="button" popovertarget="tips">Tips</button>
<!-- 2. The content: the popover attribute hides it until opened -->
<div id="tips" popover>
<h2>Quick tips</h2>
<p>Press Escape or click outside to close this.</p>
</div>
<!-- A manual popover with its own close button -->
<button type="button" popovertarget="banner">Show status</button>
<div id="banner" popover="manual">
<p>Upload finished.</p>
<button type="button" popovertarget="banner" popovertargetaction="hide">Close</button>
</div>
[popover] {
margin: 0; /* the default centres it, so reset this */
padding: 1rem;
border: 1px solid #888;
border-radius: 8px;
max-width: 20rem;
}
The Three Piecesโ
- The content: any element with the
popoverattribute and anid. It is hidden until opened. - The invoker: a
buttonwithpopovertargetset to thatid. Clicking it toggles the popover. - The behaviour: chosen by the attribute's value,
auto(the default) ormanual.
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:
| Behaviour | Details |
|---|---|
| Hidden until opened | Without the open state, the popover is not displayed at all |
| Top layer | It is shown above everything, with no z-index games, and is not clipped by parent boxes |
| Light dismiss | A click outside it, or the Escape key, closes it |
| One at a time | Opening one auto popover closes other unrelated ones. A popover opened from inside another stays with its parent |
| Toggle button | The invoker button opens and closes it, and browsers expose its expanded state to assistive technology |
| Focus handling | Closing 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 it | Yes | No |
| Escape closes it | Yes | No |
| Closes other popovers | Yes, unrelated ones | No |
| Who closes it | The browser, or the visitor | Only your buttons or script |
| Good for | Menus, help cards, filters | Status 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:
- Reset the defaults, with
margin: 0, and set your own padding and border, as in the code above. - 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?
beforetogglefires 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.togglefires after the change. Use it to update labels or analytics.:popover-openis a CSS pseudo-class for styling the open state.::backdropexists 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.
Menus and Tooltipsโ
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-labelledbyor 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
autofocuson 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?โ
| Need | Best fit |
|---|---|
| An answer is required, and the page must wait | Modal dialog |
| A temporary panel that does not block the page, and closes on outside click | Popover (auto) |
| A panel that stays until a button is pressed | Popover (manual) |
| Content that expands in place | Details and Summary |
| A message that needs no interaction | A plain element in a live region |
Checking Your Setupโ
- Open the popover with the mouse and the keyboard. Is the button reachable, and does Enter or Space work?
- Press Escape, and click outside. Does an auto popover close, and a manual one stay?
- Open a second popover. Does the first close, as you expect?
- Check where focus goes when it opens and when it closes.
- In DevTools, look for the
popoverattribute, and check:popover-openin the styles panel. - Test in a second browser, and with the feature unavailable, if you can.
- Test on a phone and at 200% zoom, and in right-to-left text.
Best Practicesโ
- Start with
popover(auto), and usemanualonly when needed. - Use a real
buttonwithpopovertarget. - Reset the default margin and style, and position the popover on purpose.
- Give each popover a heading or an accessible name.
- Do not hide essential information.
- Keep the content small and focused.
- Plan for browsers without support.
- Use a dialog for tasks that need an answer, and
detailsfor in-place expansion. - Respect reduced motion if you animate.
- 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, notinnerHTML. 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
sandboxand 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โ
- Wondering why the popover is centred, and not resetting the default margin.
- Using
manualwhenautowas enough, then adding script to close it. - Hiding essential information in a popover.
- No accessible name for an icon-only invoker.
- Using a popover for a task that needs an answer, instead of a dialog.
- Expecting menu keyboard behaviour for free.
- Building hover-only tooltips that fail on touch screens.
- Ignoring browsers without support, so the content appears inline by surprise.
- Relying on anchor positioning alone without a fallback.
- Putting secrets in a closed popover.
Practice Timeโ
- In the lab, open Help card A, then Help card B. What does the log say about A?
- Open the Status banner, click outside it, and press Escape. What happens?
- Close the banner with its own button. What is logged?
- Complete the Auto or Manual? picker.
- Build a popover with a button and three links, and reset its margin.
- Place it under the button with the script above.
- Add an
autofocuselement inside it, and test the keyboard. - 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. autogives you light dismiss and one-at-a-time behaviour.manualleaves 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
detailswhen 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.