StealThis .dev

CSS Anchor Positioning

Tether tooltips and popovers to their trigger with pure CSS anchor-name and position-anchor, place them around the anchor via position-area, and let position-try-fallbacks flip them into view near a viewport edge. A live placement grid, an interactive playground, and a JS-driven fallback keep it working even in browsers without native support.

Open in Lab
css
Targets: JS HTML

Code

CSS Anchor Positioning

A demonstration of the CSS Anchor Positioning API — the browser feature that lets any absolutely-positioned element attach itself to an “anchor” element without JavaScript coordinate math. Each trigger declares an anchor-name, and its tooltip references it through position-anchor plus position-area, so the popover is laid out relative to the anchor’s box wherever it lands on the page.

The placement grid shows all nine position-area values (top/bottom/left/right and the corners, plus center) firing on hover and focus. The playground lets you pick a placement, toggle position-try-fallbacks, and drag the anchor toward a viewport edge to watch the tooltip automatically flip to the opposite side so it never clips. A live readout prints the exact CSS being applied.

When the browser lacks native support (detected with CSS.supports("anchor-name: --x")), a small vanilla-JS polyfill takes over: it measures the anchor with getBoundingClientRect(), positions the tooltip manually, and runs the same edge-flip logic — so the component degrades gracefully while the CSS path stays the primary implementation.