Sub topic 13
Positioning a popover next to its invoker
This chapter assumes you can open a popover with popovertarget, you know that popovers render in the top layer, and you are comfortable with basic CSS positioning. Its focus is placing a popover beside the button that opened it.
Why popovers appear in the middle
A popover is not placed relative to its invoker. The browser's default style sheet gives [popover] these properties:
position: fixedinset: 0margin: auto
Together they center the element in the viewport. The popover also lives in the top layer, so it sits outside the normal flow, and its parent's position: relative has no effect on it. Placing it next to a button therefore means overriding those defaults and telling the browser where the button is.
Choosing an approach
CSS anchor positioning
Anchor positioning ties one element to another by name. Give the invoker an anchor-name, then point the popover at it with position-anchor.
<button popovertarget="tip" class="invoker">Details</button>
<div id="tip" popover class="tip">
Shown below the button.
</div>
<style>
.invoker {
anchor-name: --details-btn;
}
.tip {
position-anchor: --details-btn;
position-area: bottom span-right;
margin: 0;
inset: auto;
padding: 0.5rem 0.75rem;
position-try-fallbacks: flip-block;
}
</style>anchor-namemust start with two dashes, like a custom property.position-area: bottom span-rightplaces the popover below the button, with its left edge aligned to the button's left edge. The popover extends to the right.margin: 0andinset: autoremove the centering defaults. Without them the popover may still look centered or stretched.position-try-fallbacks: flip-blockmoves the popover above the button when there is no room below. It overrides thebottompart ofposition-area.
Each invoker and popover pair needs its own anchor name. If two buttons share a name, the popover anchors to the last one in the document.
A JavaScript fallback
Where anchor positioning is missing, measure the invoker and write the coordinates yourself. The popover is position: fixed, so use viewport coordinates from getBoundingClientRect(). Do not add scroll offsets.
const button = document.querySelector('.invoker');
const tip = document.getElementById('tip');
tip.addEventListener('beforetoggle', (event) => {
if (event.newState !== 'open') return;
const rect = button.getBoundingClientRect();
tip.style.margin = '0';
tip.style.inset = 'auto';
tip.style.top = `${rect.bottom + 4}px`;
tip.style.left = `${rect.left}px`;
});Doing this in beforetoggle places the popover before it first paints, which avoids a visible jump. This code does not follow the button if the page scrolls or resizes while the popover is open. For a short-lived tooltip that is usually acceptable. For a long-lived popover, use anchor positioning.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Popover stays centered | `margin: auto` and `inset: 0` were not reset |
| Popover anchors to the wrong button | Two invokers share one `anchor-name` |
| Popover is offset by the scroll distance | Scroll offsets were added to viewport coordinates |
| Popover is clipped at the screen edge | No `position-try-fallbacks` is set |
Exercise
A popover is positioned with position-area: bottom, but it still appears in the center of the screen. Name two things to check.
Show answer
Check that the invoker has an anchor-name and that the popover's position-anchor matches it exactly. Also check that the popover's default margin: auto and inset: 0 were reset with margin: 0 and inset: auto.