Sub topic 12
Anchoring popovers and menus
This chapter assumes you know anchor-name, position-anchor, position-area, and position-try-fallbacks from earlier chapters. It also assumes basic HTML. Prior experience with the popover attribute helps but is not required.
Why popovers pair well with anchors
A tooltip or menu often needs two things. It must sit above other content, and it must sit next to a specific element. The Popover API handles the first need. Anchor positioning handles the second.
An element with the popover attribute is hidden until opened. When it opens, the browser puts it in the top layer. The top layer is a browser-managed layer that draws above the rest of the page, so you don't need z-index or a special container. The browser also gives it light dismiss, which means pressing Escape or clicking outside closes it.
Before anchor positioning, you had to measure the button in JavaScript to place the popover. Now CSS does it.
The markup
A button with the popovertarget attribute toggles the popover with the matching id. No script is needed.
<button id="menu-btn" popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>The styles
The button becomes the anchor. The popover points at it with position-anchor and picks a cell with position-area.
#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;
}Each declaration has a job:
position-anchor: --menu-btnlinks the popover to the button.position-area: bottom span-rightplaces the popover below the anchor. It starts at the anchor's left edge and extends to the right.inset: autoandmargin: 0reset browser defaults.position-try-fallbacks: flip-blockmoves the popover above the button when there is no room below.
You did not write position: absolute. The browser's default style for [popover] already sets position: fixed, and anchor positioning works with fixed and absolute elements.
Matching the menu width to the button
You can combine position-area with anchor-size() to keep the menu at least as wide as the button.
#menu {
position-anchor: --menu-btn;
position-area: bottom span-right;
min-width: anchor-size(width);
}How the pieces connect
Common errors
- Anchor name is missing or misspelled. If
position-anchornames an anchor that does not exist, the popover is not anchored. Check that both names match, including the leading--. - Defaults not reset. Leftover
insetormarginvalues shift the popover away from the anchor. - Several buttons share one anchor name. If every row in a list uses
--menu-btn, the popover can attach to the wrong one. Useanchor-scopeto limit which anchors an element can see. - Not accounting for fallback. A menu that flips above the button needs margins that suit both positions.
Accessibility
Anchoring changes where the popover appears. It does not change how assistive technology understands the page. Keep these points in mind:
- The popover's position in the DOM still defines its reading order. Put it right after its button where you can.
- Use a native
<button>as the invoker. It is focusable and responds to keyboard input. - The
popovertargetattribute links the button and popover for assistive technology. For a menu, test that focus and announcements make sense. - Do not rely on position alone to show a relationship. A tooltip that only appears near an element needs a programmatic link, such as
aria-describedby.
Exercise
A popover anchored with position-area: bottom span-right appears in the center of the viewport instead of below its button. The anchor-name and position-anchor values match. What is the most likely cause, and how do you fix it?
Show answer
The popover's default inset and margin values, which center it in the viewport, override the anchor placement. Add inset: auto and margin: 0 to the popover so position-area controls where it goes.
Exercise
Which declaration moves a popover above its button when there is no room below it, and which feature closes the popover when the user clicks elsewhere?
Show answer
position-try-fallbacks: flip-block moves the popover above the button. Light dismiss, a built-in behavior of the Popover API, closes it when the user clicks outside or presses Escape.