Sub topic 1
What the popover attribute does
Prerequisites
This chapter assumes you know basic HTML elements and attributes, basic CSS selectors and the cascade, and how to select an element in JavaScript and add an event listener. You should also be comfortable with how buttons respond to clicks and to the keyboard.
The working model
The popover attribute turns an ordinary element into a popover. A popover is a non-modal overlay. It appears on top of the page, and the rest of the page stays usable underneath it.
Adding the attribute changes three things:
- The browser hides the element by default. It applies
display: noneuntil the popover is opened. - When the popover opens, the browser places it in the top layer. This layer sits above all other content, so you don't need
z-index. - The browser gives the element built-in behavior for opening, closing, and keyboard use.
The simplest case needs no JavaScript. A button with a popovertarget attribute names the id of the popover it controls.
<button popovertarget="help">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>Clicking the button opens the element with the matching id. Pressing Escape or clicking outside the box closes it. You wrote none of that logic.
What happens when it opens
The browser tracks each popover as being in one of two states: showing or hidden. The diagram shows how the page moves between them.
While the popover is showing, the browser matches it with the :popover-open pseudo-class. Later chapters use this for styling.
Default styles to expect
A popover is not styled like a dialog or a tooltip out of the box. The browser's default stylesheet gives it these traits:
| Trait | Default behavior |
|---|---|
| Visibility | Hidden until opened |
| Position | Fixed, centered in the viewport |
| Border and padding | A thin border and some padding |
| Colors | Canvas background and text colors |
Because the position is fixed and centered, a popover does not appear next to its button unless you position it yourself. Positioning is covered in a later chapter.
Common errors
Most first-time problems come from a small set of mistakes:
- A mismatched
id. The value ofpopovertargetmust equal the popover'sidexactly. If it doesn't, the button does nothing and the browser reports no error. - Expecting the popover to be visible. An element with
popoveris hidden on load. If you see nothing, that is correct until a trigger opens it. - Overriding
displayon the popover. Settingdisplay: flexon[popover]makes it show even when closed, because your rule wins over the browser's hiding rule. Apply that rule to[popover]:popover-openinstead. - Putting the attribute on an element that needs a different role. Any element can be a popover, but the attribute adds no meaning. A
<div popover>is still a generic box. Choose an element that fits its content.
/* Wrong: the popover is always displayed */
[popover] {
display: flex;
}
/* Right: the layout applies only while open */
[popover]:popover-open {
display: flex;
}Why this matters
Before this attribute, developers built overlays with extra markup, scripts for open and close state, z-index values, and handlers for Escape and outside clicks. The popover attribute replaces most of that with one attribute and one button attribute. The result is less code and more consistent keyboard behavior.
Exercise
You add popover to a <div id="tips"> and a button with popovertarget="tip". Clicking the button does nothing. What is wrong, and how do you fix it?
Show answer
The button's popovertarget value ("tip") doesn't match the div's id ("tips"). Change one so both are identical, for example popovertarget="tips".
Exercise
A popover has the rule [popover] { display: grid; } and it is visible as soon as the page loads. Why, and what rule should you write instead?
Show answer
The author rule overrides the browser's default display: none for closed popovers. Write [popover]:popover-open { display: grid; } so the layout applies only while the popover is open.