Sub topic 12
Anchoring popovers and menus
This chapter assumes you know the position property, inset properties, and the basics of anchor-name, position-anchor, position-area, and position-try-fallbacks from earlier chapters. Knowing the HTML popover attribute helps but is not required, because the chapter introduces what you need.
Why popovers pair well with anchors
Menus, tooltips, and dropdowns have two jobs. They must appear and disappear (toggling, closing on Escape, closing on outside click), and they must sit next to something (a button or a field). Anchor positioning handles the second job. The Popover API handles the first.
The Popover API also removes a problem that used to complicate anchored UI. A popover is shown in the top layer, a browser-managed layer that paints above all other content. Ancestor overflow: hidden, z-index stacking contexts, and transform containing blocks cannot clip it or cover it. You no longer need to move the menu to the end of <body> to escape clipping.
The anchor does not have to be an ancestor of the popover. Only the visual placement is tied to the anchor. The DOM relationship stays whatever you wrote.
The markup
This is the structure used through the chapter.
<button id="menu-btn" popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>The popovertarget="menu" attribute makes the button an invoker. A click toggles the element with id="menu". The popover attribute with no value means popover="auto", which gives you these behaviors:
- Pressing Escape closes the popover.
- Clicking outside it closes it (light dismiss).
- Opening another auto popover closes this one, unless one is nested in the other.
Place the popover directly after its button in the DOM. This keeps reading order and keyboard order sensible, as the accessibility section explains.
Connecting the popover to its button
#menu-btn {
anchor-name: --menu-btn;
}
#menu {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
position-try-fallbacks: flip-block;
}The button declares the anchor name. The popover refers to it with position-anchor. position-area: bottom span-right puts the popover in the row below the anchor, starting at the anchor's left edge and extending to the right. flip-block moves it above the button if there is no room below.
The popover does not declare position: absolute. The browser's built-in popover style sheet already sets position: fixed, and fixed positioning also works with anchors.
Resetting popover defaults
The inset: auto and margin: 0 lines are not decoration. A [popover] element ships with default styles meant to center it in the viewport.
| Default | Value | Effect on anchoring |
|---|---|---|
| `position` | `fixed` | Fine. Anchoring works with fixed and absolute positioning. |
| `inset` | `0` | Pins the popover to all four viewport edges. Reset it with `inset: auto` so `position-area` controls placement. |
| `margin` | `auto` | Auto margins center the box inside its area. Reset it with `margin: 0`. |
| `width`, `height` | `fit-content` | Fine. The popover sizes to its content. |
| `border`, `padding`, `background` | Visible defaults | Style these to match your design. |
If you use anchor() functions instead of position-area, you set explicit top and left values. Those override the default inset: 0 on their own side. You still need to clear the sides you do not use, and inset: auto first is the safest start.
What happens when the menu opens
Placement is a layout step, not a script. If the user scrolls or resizes the window, the browser re-runs the check. The popover can switch between your fallbacks without any event handler.
Sizing the menu from the button
A menu narrower than its button looks broken. The anchor-size() function reads the anchor's dimensions:
#menu {
min-width: anchor-size(width);
}This keeps the menu at least as wide as the button, and it tracks the button if the button's width changes. You can also use anchor-size() for max-height or width, but a minimum width is the common case.
A complete, runnable example
This page combines the pieces. Save it as an HTML file and open it in a browser that supports anchor positioning.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Anchored menu</title>
<style>
body { font-family: system-ui, sans-serif; padding: 2rem; }
#menu-btn {
anchor-name: --menu-btn;
padding: 0.5rem 1rem;
}
#menu {
padding: 0.5rem;
border: 1px solid #888;
border-radius: 6px;
background: white;
}
#menu a {
display: block;
padding: 0.25rem 0.5rem;
}
@supports (position-area: bottom) {
#menu {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
min-width: anchor-size(width);
position-try-fallbacks: flip-block;
}
}
</style>
</head>
<body>
<button id="menu-btn" popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>
</body>
</html>In a browser without anchor positioning, the @supports block is skipped and the menu opens centered in the viewport, which is the default popover layout. It remains usable.
To see the fallback work, scroll the page so the button sits near the bottom edge of the viewport and open the menu. The menu appears above the button because flip-block swaps bottom for top.
Custom fallbacks for menus
flip-block is enough for a simple dropdown. For a menu near the right edge, add a horizontal flip. Fallbacks are tried in order, and a combined keyword flips both axes.
#menu {
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
}If you need a placement that is not a flip, define it with @position-try. A named option can contain position-area, inset and margin properties, and sizing properties. It cannot contain other properties.
@position-try --menu-right {
position-area: right span-bottom;
margin: 0 4px;
}
#menu {
position-try-fallbacks: flip-block, --menu-right;
}When an option such as --menu-right applies, its declarations replace the matching declarations on the element for as long as that option is in use.
Several menus on one page
Anchor names are matched by name across the document. If three buttons all declare anchor-name: --menu-btn, an element that references --menu-btn anchors to the last one in tree order. This is a frequent bug in menus generated from a loop.
There are two fixes. You can give each pair its own name, usually through an inline style attribute such as style="anchor-name: --menu-1". Or you can use anchor-scope on a wrapper so the same name resolves independently inside each wrapper.
.menu-group {
anchor-scope: --menu-btn;
}
.menu-group > button {
anchor-name: --menu-btn;
}
.menu-group > [popover] {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
}Each .menu-group now has its own --menu-btn. A popover inside a group sees only the button in that group. You write the CSS once and repeat the markup freely.
Keeping the menu attached while scrolling
For a popover anchored to a button inside a scrolling container, the popover follows the button as it scrolls. The browser accounts for the scroll offset of the anchor's scroll container when it lays out the popover. No script is involved.
If the button scrolls out of view inside its container, the popover can end up floating over empty space. Use position-visibility to hide it in that case:
#menu {
position-visibility: anchors-visible;
}With anchors-visible, the popover is hidden while its anchor is clipped out of view by a scroll container. Use this for a menu that should not outlive its button on screen.
Accessibility
Anchor positioning changes only where a box is painted. It does not change the accessibility tree, the focus order, or any relationship between the two elements. Keep these points in mind:
- Use a real button as the invoker.
popovertargeton a<button>gives you keyboard activation, and browsers expose the open or closed state of the popover to assistive technology. - Keep DOM order logical. Put the popover right after its invoker. Tab order follows the DOM, not the screen position, so a menu that appears under a button but sits at the end of the document can confuse keyboard users.
- Do not add
role="menu"casually. That role promises arrow-key navigation and a roving focus, which you must build yourself. A group of links in a popover is a valid navigation pattern without it. - Do not rely on position to convey meaning. If a menu flips from below to above, nothing about its content should depend on that.
- Respect users who reduce motion. If you animate the opening, wrap the animation in a
prefers-reduced-motioncheck.
Feature detection recap
Guard with @supports (anchor-name: --a) or @supports (position-area: bottom). Put the layout that works everywhere outside the block, and the anchored layout inside it. For popovers, the default centered layout is a reasonable baseline, so the unguarded rules only need to style the menu itself.
Exercise
A page has five "Options" buttons in a list, each with its own popover menu. All five buttons use anchor-name: --opt, and every menu uses position-anchor: --opt. All five menus appear next to the last button. Give two ways to fix it, and name the property that lets you keep the same name.
Show answer
Fix 1: give each button and menu pair a unique anchor name, for example with an inline style such as style="anchor-name: --opt-1" on the button and a matching position-anchor on its menu. Fix 2: wrap each button and menu pair in an element with anchor-scope: --opt. Then the shared name resolves inside each wrapper to that wrapper's own button. The property that scopes a name is anchor-scope.
Exercise
You add position-area: bottom and position-anchor to a [popover] menu, but it still opens in the center of the viewport. Which two default popover declarations are the likely cause, and what should you set instead?
Show answer
The default inset: 0 and margin: auto that the browser applies to [popover] elements. Set inset: auto and margin: 0 on the popover so position-area controls its placement.