In this lesson, you placed the cordless drill’s Options menu on Kwame’s catalog page right under the button that opens it, instead of in the middle of the window:
- Name the anchor. Anchor positioning places one element relative to another, its anchor. The drill’s Options button gets
anchor-name: --options, a name that starts with two dashes, like a custom property. It’s only a name, though. You don’t declare it or read it withvar(). - Point the popover at it. The menu’s
position-anchor: --optionsties it to that button. On its own, nothing moves. Open the menu again, and it’s still in the middle. Watch out: these properties only place an element that’s absolutely or fixed positioned. A popover is alreadyposition: fixedfrom the browser’s own styles, but a paragraph in normal flow stays where it is. - Pick a side. Picture a grid of three rows and three columns around the button.
position-area: bottomputs the menu in the row below it, centered on the button. Other values pick other places, likerightfor the column to its right. Save and open the menu again to see it under the button. - Flip when there’s no room. With the button near the bottom of the window, the menu still opens below it and runs off the bottom.
position-try-fallbacks: flip-blocklets the browser try the other end of the block direction, so the menu opens above the button. If that doesn’t fit either, it stays below. When you try this, scroll first, then open the menu. In Chrome, a menu that’s already open keeps its side as you scroll. - Browser support. Anchor positioning isn’t Baseline yet. A browser without it skips these lines, and the menu opens in the middle of the window, as before, still usable.
To anchor a popover, name its button, point the popover at the name, pick a side with position-area, and give it a side to flip to. Here are the two rules added at the end of styles.css:
[popovertarget="cordless-drill-options"] {
anchor-name: --options;
}
#cordless-drill-options {
position-anchor: --options;
position-area: bottom;
position-try-fallbacks: flip-block;
}