3.3 · Popovers & positioning
Tether elements: anchor positioning
Tether a tooltip to any element and let the browser flip it at the edges.
Give an element anchor-name: --target, then position
another one relative to it with
position-anchor: --target. The simplest way to place it
is position-area, which chooses a cell in an imaginary
3×3 grid around the anchor, such as block-start (above)
or inline-end (to the right).
position-try-fallbacks: flip-block tells the browser to
flip the tooltip to the other side when it would overflow. This used
to be the main job of libraries like Popper and Floating UI.
const rect = anchor.getBoundingClientRect()
tip.style.top = rect.top - tip.offsetHeight + 'px'
tip.style.left = rect.left + 'px'
// …again on scroll, on resize, and flip
// when it would leave the viewport
.anchor { anchor-name: --target; }
.tooltip {
position: absolute;
position-anchor: --target;
position-area: block-start;
position-try-fallbacks: flip-block, flip-inline;
}