Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Anchor a Popover to Its Button

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 with var().
  • Point the popover at it. The menu’s position-anchor: --options ties 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 already position: fixed from 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: bottom puts the menu in the row below it, centered on the button. Other values pick other places, like right for 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-block lets 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:

Language: CSS
[popovertarget="cordless-drill-options"] {
  anchor-name: --options;
}

#cordless-drill-options {
  position-anchor: --options;
  position-area: bottom;
  position-try-fallbacks: flip-block;
}

00:00 The ladder’s help tip opened in the middle of the window, right over the About safety button. In this video, you’ll place a popover next to the button that opens it instead.

00:11 Kwame’s catalog has the same problem. Each tool card has an Options button that opens a menu, an auto popover with three links. Amara Okafor clicks the Options button on the cordless drill’s card, and the menu opens in the middle of the window.

00:27 It covers the drill’s own details instead of sitting by her button. It should open right under it.

00:34 Anchor positioning places one element relative to another, called its anchor. First, give the anchor a name. In styles.css, the drill’s Options button gets anchor-name, with a name that starts with two dashes, like a custom property.

00:49 It’s only a name, though. You don’t declare it or read it with var(). Then, with position-anchor, the menu points at that name.

00:58 Save and open the menu again, and it’s still in the middle. It knows its anchor, but not where to sit. Watch out: these properties only place an element that’s absolutely or fixed positioned.

01:11 A popover is already fixed, from the browser’s own styles, but a paragraph in normal flow would stay where it is. Now say where it goes. Picture a grid of three rows and three columns around the button, with the button in the middle cell.

01:28 The property position-area, set to bottom, names the row below the button. The menu goes in that row, centered on the button. Other values pick other places, like right for the column to its right.

01:41 Save and open the menu again, and it sits right under the Options button.

01:48 With the menu closed, Amara scrolls up a little, so the drill’s Options button sits near the bottom of the window. With these three lines, where does the menu go when she opens it now?

01:59 Your turn: pause the video and predict.

02:07 Below the button anyway, and off the bottom of the window. Only the top of its first link shows. The browser puts it in the row you named, whether it fits or not.

02:19 So give it a fallback: position-try-fallbacks, set to flip-block. When the menu doesn’t fit below, the browser tries the other end of the block direction, above the button.

02:30 If that doesn’t fit either, it stays below. Amara opens the menu again, and this time it opens above her button. When you try this, scroll first, then open the menu.

02:43 In Chrome, a menu that’s already open keeps its side as you scroll.

02:49 Anchor positioning isn’t Baseline yet. A browser without it skips these lines.

02:56 The menu then opens in the middle of the window, as before, so Amara can still use it.

03:02 So 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.

03:12 Next, in Scope Anchors and Plan a Fallback, you’ll give the other three cards’ menus their own anchors, and check support for yourself.

Become a Member to join the conversation.