CSS anchor positioning: tooltips and dropdowns without a positioning library
A tooltip has to sit next to its button, stay inside the viewport and move to the other side when there is no room. JavaScript libraries like Popper and Floating UI exist only for this, and they recalculate positions on every scroll and resize. CSS anchor positioning lets one element name another as its anchor and position itself relative to it, including fallbacks when it would overflow the screen.
Problem: A theme has help tooltips in a form and a dropdown in the header, both positioned with a JavaScript library of 10 KB. The tooltips lag behind when the page scrolls on phones and sometimes stick out of the viewport.
Solution: Give the trigger an anchor-name, position the tooltip with position-anchor and the anchor() function (or position-area), let it flip to the other side with position-try-fallbacks, and show it with the Popover API. Keep the library only as a fallback for browsers without anchor positioning.
Anchor and positioned element
<button type="button" class="ha-help" popovertarget="ha-tip-vat" style="anchor-name: --ha-tip-vat">?</button>
<div id="ha-tip-vat" class="ha-tooltip" popover style="position-anchor: --ha-tip-vat">
Enter the VAT number with the country code, for example DE123456789.
</div>
.ha-tooltip {
position: fixed; /* popovers are in the top layer, fixed is their default */
margin: 0;
inset: auto;
position-area: top; /* above the anchor, centered */
margin-block-end: .5rem;
max-inline-size: 18rem;
position-try-fallbacks: flip-block, flip-inline;
}
anchor-name declares a name (it must start with --); position-anchor tells the tooltip which anchor to use. position-area: top places it in the area above the anchor, like a 3×3 grid around it. When it does not fit above, position-try-fallbacks: flip-block tries below, flip-inline mirrors left and right. The browser recalculates on scroll and resize itself, without script.
The popover attribute and popovertarget show and hide the tooltip on click, close it with Escape or a click outside, and put it in the top layer, above everything else, with no z-index needed.
Precise placement with anchor()
.ha-dropdown {
position: absolute;
position-anchor: --ha-account;
top: anchor(bottom); /* top edge of the menu at the bottom edge of the button */
left: anchor(left);
min-inline-size: anchor-size(width);
}
anchor( bottom ) resolves to the position of the anchor’s bottom edge, anchor-size( width ) to its width, so the dropdown is at least as wide as its button.
Many anchors with the same names
In a loop (product cards, comment actions), each trigger needs a unique name, or every tooltip uses the last anchor with that name. In PHP:
foreach ( $products as $product ) {
$anchor = '--ha-tip-' . $product->get_id();
printf(
'<button type="button" popovertarget="ha-tip-%1$d" style="anchor-name:%2$s">%3$s</button>
<div id="ha-tip-%1$d" popover class="ha-tooltip" style="position-anchor:%2$s">%4$s</div>',
(int) $product->get_id(),
esc_attr( $anchor ),
esc_html__( 'Details', 'textdomain' ),
esc_html( $product->get_short_description() )
);
}
Fallback
if ( ! CSS.supports( 'anchor-name: --a' ) ) {
import( './tooltip-fallback.js' ); // positions the open popovers with Floating UI
}
Without support, the popover still opens (the Popover API is widely supported), just in the default position: centered in the viewport. That is usable; the fallback script only restores the placement next to the trigger. Popovers on their own: entry animations for popovers and dialogs.
NOTE: Anchor positioning works in Chrome and Edge since version 125 (May 2024); position-try-fallbacks got its final name in Chrome 128 (earlier: position-try-options). Firefox and Safari do not support it yet (May 2025), so for now the CSS version is a progressive enhancement and the script fallback is needed for a large share of visitors.
Update (2025): Safari 26 (September 2025) supports anchor positioning.