WEB-27Recommended
Pin menus to their button with CSS
Attach popovers and menus to the element they belong to with CSS anchor positioning. Keep a plain fallback where browsers lack it.
How to do it
Give the trigger anchor-name: --x and the popup position-anchor: --x. Add position-try-fallbacks: flip-block so it flips when there is no room below. Anchor positioning works in every major browser since January 2026. Wrap it in @supports (anchor-name: --x) and keep a plain fixed position as the fallback for older browsers. Build the menu itself as Build menus with popover says.
Examples
CSS example
#menu { position: fixed; top: 4rem; right: 1rem; }
@supports (anchor-name: --menu) {
.menu-button { anchor-name: --menu; }
#menu {
position-anchor: --menu;
top: anchor(bottom);
right: anchor(right);
position-try-fallbacks: flip-block;
}
}
How to check
A browser test opens the popup, scrolls the page by 200 px and compares bounding boxes. The popup's top edge stays within 1 px of the trigger's bottom edge.
Sources
Related rules
- WEB-25 Build menus with popover in Website
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/interface/anchor-positioning">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.