3.6 · Popovers & positioning

Match the anchor’s size: anchor-size()

Size an element to match its anchor, like a dropdown as wide as its button.

Anchor positioning places an element next to another. anchor-size() also lets it take the anchor’s measurements: inline-size: anchor-size(width) makes a menu exactly as wide as the button that opens it.

It works anywhere a size goes, including min-inline-size, max-block-size and inside calc(). A tooltip can be at most twice its anchor’s width with calc(anchor-size(width) * 2).

Before
const { width } = button.getBoundingClientRect()
menu.style.width = width + 'px'
// …and again on resize
Now
.menu {
  position-anchor: --trigger;
  position-area: block-end span-inline-end;
  inline-size: anchor-size(width);
}