Anchor Positioning
Positioning a tooltip, dropdown, or popover next to another element has traditionally meant measuring both elements' positions with JavaScript, calculating coordinates, and re-measuring on scroll and resize — which is exactly what libraries like Popper.js and its successor Floating UI exist to do. CSS anchor positioning brings that same capability natively into the CSS engine: an element can be positioned relative to a named "anchor" element anywhere else in the document, with no JavaScript measuring logic at all.
Naming an anchor
anchor-name. The element that should be positioned relative to it then declares that name as its position-anchor..info-button {
anchor-name: --info-anchor;
}
.tooltip {
position: fixed; /* anchor positioning requires an absolutely/fixed-positioned box */
position-anchor: --info-anchor;
}Positioning with anchor()
anchor() function resolves to a coordinate derived from the anchor element's box, letting you place the anchored element's edges relative to the anchor's edges — for example, aligning a tooltip's bottom edge to the anchor's top edge to make it appear above the button..tooltip {
position: fixed;
position-anchor: --info-anchor;
/* Place the tooltip's bottom edge at the anchor's top edge
(i.e. the tooltip sits directly above the button) */
bottom: anchor(top);
/* Center the tooltip horizontally over the anchor */
left: anchor(center);
translate: -50% 0;
}<button class="info-button" popovertarget="tooltip">Info</button> <div id="tooltip" class="tooltip" popover> Extra detail about this field. </div>
Automatic fallback positioning
position-try-fallbacks, so you don't need JavaScript to detect the overflow and flip the tooltip manually..tooltip {
position: fixed;
position-anchor: --info-anchor;
bottom: anchor(top);
left: anchor(center);
translate: -50% 0;
/* If the default placement (above the anchor) would overflow,
try flipping below the anchor instead */
position-try-fallbacks: flip-block;
}flip-block— flips the anchored element to the opposite side along the block axis (e.g. above ↔ below).flip-inline— flips along the inline axis (e.g. left ↔ right).A custom list of
@position-tryfallback rules can also be declared for more specific fallback positions than a simple flip.