Sub topic 1
What the popover attribute does
Prerequisites
This chapter assumes you already know:
- Basic HTML elements and attributes.
- Basic CSS selectors and how the cascade resolves conflicts.
- Basic JavaScript: selecting an element and adding an event listener.
- How buttons and keyboard focus work in a page or form.
You do not need to know anything about popovers, the top layer, or the <dialog> element. Later chapters cover those in detail.
The problem the attribute solves
Menus, tooltips, help boxes, and notices all float above the page and appear only when needed. Before the popover attribute, each of these needed a script. The script toggled a class, managed z-index, listened for clicks outside the box, and listened for the Escape key. Every project wrote that code again, and each version handled keyboard users and screen readers a little differently.
The popover attribute moves this work into the browser. Add the attribute to any element and the browser:
- Hides the element until something opens it.
- Shows it above the rest of the page when opened.
- Closes it when the user clicks outside it or presses Escape (for the default type).
- Connects it to a button so that no script is needed for the basic case.
A popover is a non-modal overlay. Non-modal means the rest of the page stays usable while the popover is open. That separates it from a modal dialog, which blocks the page behind it.
The smallest working example
The next example needs no JavaScript.
<button popovertarget="help">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>Two attributes do the work:
popoveron the<div>marks it as a popover. With no value, it behaves aspopover="auto".popovertarget="help"on the<button>names theidof the popover to control.
The button is the invoker, which is the element that opens or closes a popover. Clicking it shows the <div>. Pressing Escape or clicking elsewhere closes it again.
The next chapter covers popovertarget and popovertargetaction in more detail. For now, treat the button as a way to open the popover.
What the browser does when you add the attribute
Adding popover changes an element in four ways.
It is hidden by default
An element with popover has display: none applied by the browser's built-in stylesheet until it is open. You do not write a hidden attribute or a CSS rule to hide it. If you remove the popover attribute from the element, it becomes a regular visible element again.
It gets default styles
When open, the browser gives the popover some default styling. In most browsers it is positioned in the center of the viewport, with a border, padding, and a background color. These defaults are intentionally plain. You can override them with ordinary CSS, and a later chapter on styling covers this.
It has a showing state and a hidden state
Every popover is in one of two states: showing or hidden. The browser tracks this state. CSS can read it through the :popover-open pseudo-class, which matches only while the popover is showing. JavaScript can read it through events, and later chapters cover those.
It moves to the top layer when shown
A showing popover is placed in the top layer. The top layer is a browser-managed layer that sits above all other content on the page, regardless of z-index or overflow on ancestor elements. A popover inside a container with overflow: hidden is not clipped by that container. You do not need z-index: 9999. A later chapter explains the top layer more fully.
What the attribute accepts
The popover attribute takes a small set of values.
| Markup | Meaning |
|---|---|
| `popover` | Same as `popover="auto"`. |
| `popover="auto"` | Closes on outside click and on Escape. Opening it also closes other auto popovers that are not its ancestors. |
| `popover="manual"` | Does not close on outside click or on Escape. Only an explicit action closes it. |
An empty value, such as popover="", also means auto. An invalid value, such as popover="banana", is treated as manual in current specifications. Avoid relying on that behavior. Use one of the documented values.
The next example uses manual.
<div id="notice" popover="manual">
<p>Your changes were saved.</p>
<button popovertarget="notice" popovertargetaction="hide">Dismiss</button>
</div>
<script>
const notice = document.getElementById('notice');
notice.showPopover();
</script>This popover stays open until the user clicks Dismiss or code closes it. Clicking elsewhere on the page or pressing Escape does nothing. The script opens the popover on page load with showPopover(), which later chapters cover.
A later chapter in this tutorial, "Choosing between auto and manual popovers", compares the two types in depth.
Popover is not the same as hidden
Beginners often confuse a popover with an element that is simply hidden. They differ in several ways.
- A hidden popover is not rendered, so it does not take up space in the layout. A popover that is showing also does not take up space in the normal flow, because it is in the top layer.
- A popover can only be opened in two ways: through an invoker button or through the JavaScript methods. You cannot open one by adding a class.
- The browser manages closing for
autopopovers. You do not write that code.
Which elements can be popovers
Most HTML elements can have the popover attribute. Common choices are:
<div>for general content.<ul>or<nav>for menus.<p>or<span>for short notes.<dialog>is allowed, but it has its own opening methods and a separate modal mode. Use the dialog chapter to decide.
Choose the element that matches the content's meaning. A list of links is a list, so <ul> or <nav> describes it better than <div>. The popover attribute does not add any role or meaning of its own.
Common errors
The following mistakes cause most first-time problems.
- The
iddoes not match.popovertarget="help"needs an element withid="help". A typo means the button does nothing, and the browser shows no error. - The invoker is not a button.
popovertargetworks on<button>elements and on<input>elements of typebutton,reset, orsubmit. It does not work on<div>or<a>. - Assuming the popover is positioned near the button. By default the popover appears in the center of the viewport, not next to the invoker. Positioning is a separate topic.
- Expecting a click on a manual popover's backdrop to close it. Manual popovers ignore outside clicks. Add a hide button.
- Setting
displayin CSS for the popover. A rule such as[popover] { display: flex; }overrides the browser'sdisplay: noneand makes the popover visible all the time. Applydisplayrules only to[popover]:popover-open. - Using a popover for something that must block the page. A popover does not stop the user from interacting with the page behind it. Use a modal dialog for that.
The fifth error is worth a closer look. This CSS looks harmless, but it breaks the hiding behavior.
/* Wrong: the popover is now always visible */
[popover] {
display: flex;
gap: 0.5rem;
}
/* Right: the layout applies only while the popover is open */
[popover]:popover-open {
display: flex;
gap: 0.5rem;
}The browser's own rule that hides the element has low priority. Any author rule that sets display on the element wins over it.
Browser support and a first fallback
The popover attribute is supported in current versions of Chrome, Edge, Safari, and Firefox. Older browsers ignore the attribute, so the element would stay visible as plain content. A later chapter covers fallbacks in full. This check tells you whether the browser supports popovers.
const supportsPopover =
typeof HTMLElement !== 'undefined' &&
Object.prototype.hasOwnProperty.call(HTMLElement.prototype, 'popover');
if (!supportsPopover) {
document.documentElement.classList.add('no-popover');
}If the property is missing, the script adds a no-popover class to the root element. Your CSS can then hide the content or show it in another way.
Summary of the model
Keep these points in mind as you move to the next chapters.
popoverturns any element into a hidden, non-modal overlay.- A button with
popovertargetopens it without JavaScript. - A showing popover lives in the top layer, above all other content.
autopopovers close on outside click and Escape.manualpopovers do not.- CSS reads the open state with
:popover-open. - Never set
displayon the bare[popover]selector.
Exercise
A developer writes <div id="tip" popover>Hello</div> and <a popovertarget="tip">Show tip</a>. Clicking the link does nothing. Give two changes that would fix it, and say what the popover's default behavior will be once it works.
Show answer
First, popovertarget works only on <button> and button-like <input> elements, so change the <a> to <button popovertarget="tip">Show tip</button>. The id already matches, so no change is needed there. Second, as an alternative, keep the link and call document.getElementById('tip').showPopover() from a click handler. Once it works, the popover is type auto, because popover has no value. It appears in the top layer, usually centered in the viewport. It closes when the user presses Escape or clicks outside it.
Exercise
You write [popover] { display: grid; } to lay out a popover's content. The popover is now visible when the page loads. Why, and how do you fix the rule?
Show answer
The browser hides a popover with a low-priority display: none rule. Your [popover] rule sets display on the element at all times, so it overrides the browser rule and the popover is always rendered. Change the selector to [popover]:popover-open { display: grid; }. The layout then applies only while the popover is showing, and the browser's hiding rule still applies when it is closed.