css and layoutsaccessibility

Anchor Positioning: A Tooltip Without Floating UI

A product card has an "i" icon, and hovering it shows a tooltip with delivery information. The code is textbook:

css

.info {
position: relative;
}
.info__tooltip {
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
translate: -50% 0;
}

It works – until the card ends up in a carousel. The carousel has overflow: hidden (otherwise the slides would stick out of it), and an absolutely positioned tooltip is part of the card, so it gets clipped along with it. You see the bottom half of the tooltip, or nothing at all. A dropdown in a scrolling table or a context menu in a sidebar ends the same way.

Until now the answer was a library – Popper, later Floating UI. The pattern was always the same: move the tooltip to the end of <body> (to escape the overflow), then compute its position in JavaScript from the button's getBoundingClientRect():

javascript

import { computePosition, autoUpdate, flip, offset } from "@floating-ui/dom"
const cleanup = autoUpdate(button, tooltip, () => {
computePosition(button, tooltip, {
placement: "top",
middleware: [offset(8), flip()],
}).then(({ x, y }) => {
tooltip.style.left = x + "px"
tooltip.style.top = y + "px"
})
})

autoUpdate listens to scroll on every scrollable ancestor, to resize, and to size changes via ResizeObserver. It works well, but it means a dozen kilobytes of library, code on the main thread on every scroll, and having to remember cleanup() when the component unmounts. All of that just to tell the browser something you can say in one sentence: "this element should sit above that one".

Popover solves half the problem

In the article about the Popover API I wrote about the top layer – a layer above the whole document that no overflow: hidden or z-index can reach. A tooltip with the popover attribute therefore stops being clipped by the carousel:

html

<button popovertarget="delivery-info">i</button>
<div id="delivery-info" popover>Delivered tomorrow if you order by 2 pm</div>

The catch: an element in the top layer no longer has anything to do with where it sits in the HTML. The browser's default styles for a popover are position: fixed; inset: 0; margin: auto, so the tooltip lands in the middle of the screen. It escaped the overflow, but it escaped the button along with it. That second half is exactly what Anchor Positioning closes.

The anchor and the anchored element

The mechanism has two sides. The element you want to attach to gets a name via anchor-name. The positioned element points at that name via position-anchor and says where it should go:

css

.info {
anchor-name: --delivery;
}
#delivery-info {
position-anchor: --delivery;
position-area: top;
margin: 0 0 8px;
inset: auto; /* reset inset: 0 from the popover's default styles */
}

That's all the code. The tooltip sits above the icon, centred horizontally, with an 8px gap – and it follows the icon on scroll, window resize or carousel movement, without a single line of JavaScript. The browser computes the position in the same layout pass as the rest of the page, so there's none of the one-frame lag you sometimes see with libraries.

Anchor names must start with two dashes, like custom properties. The anchored element must be absolutely positioned or fixed – a popover is fixed by definition, so it meets that requirement without an extra declaration.

position-area: a 3×3 grid around the anchor

position-area is the simplest way to describe a position. Picture a three-by-three grid where the middle cell is the anchor. The value says which cells the element should occupy:

css

position-area: top; /* above the anchor, centred */
position-area: bottom span-right; /* below the anchor, from its left edge to the right */
position-area: right; /* to the right, centred vertically */
position-area: top left; /* in the top-left corner, diagonally */

bottom span-right is a typical dropdown: the menu starts flush with the button's left edge and extends to the right. top is the classic tooltip. You can also use logical values (block-start, inline-end), which flip automatically in right-to-left languages.

anchor(): when the grid isn't enough

If you need finer control, the anchor() function returns the position of a chosen edge of the anchor, and you can use it in top, left, right and bottom:

css

.dropdown {
position: absolute;
position-anchor: --menu-button;
top: anchor(bottom);
left: anchor(left);
min-width: anchor-size(width);
}

top: anchor(bottom) reads like a sentence: the menu's top edge at the button's bottom edge. anchor-size(width) is a bonus that libraries never gave you directly – the menu is at least as wide as the button that opens it, without measuring it in JS. To centre relative to the anchor with this approach, use justify-self: anchor-center.

Flipping at the edge of the screen

A tooltip above an icon in the first row of products, right under the header, has nowhere to fit and runs off the screen. Floating UI has the flip() middleware for that. CSS has position-try-fallbacks:

css

#delivery-info {
position-anchor: --delivery;
position-area: top;
position-try-fallbacks: flip-block, flip-inline;
}

The browser tries the base position first. If the element would overflow, it tries the next options: flip-block mirrors the position vertically (top to bottom), flip-inline horizontally. The first option that fits wins. You can also define your own options with @position-try:

css

@position-try --beside {
position-area: right;
margin: 0 0 0 8px;
}
#delivery-info {
position-area: top;
margin: 0 0 8px;
position-try-fallbacks: flip-block, --beside;
}

A custom option can override position, margins and size, which is why it's worth moving the margin into it – a "bottom" gap on a tooltip flipped to the right would make no sense.

The second useful property is position-visibility: anchors-visible. When the user scrolls the card so that the icon disappears past the carousel edge, the tooltip hides too, instead of floating in mid-air above nothing. Just like with the scroll-driven animations I wrote about earlier, the browser tracks the scrolling of every ancestor itself – what used to need an IntersectionObserver or a scroll listener becomes a single declaration.

Pitfall: every tooltip next to the last card

This is where the part you won't find in single-button demos begins. A product list, each product with its own icon and tooltip, and CSS written once for the component:

css

.product__info {
anchor-name: --delivery;
}
.product__tooltip {
position-anchor: --delivery;
position-area: top;
}

The result: whichever tooltip you open, it appears above the last card in the list. An anchor name is not local to the component – it's visible across the whole document, and when several elements share the same name, position-anchor picks the last one in tree order. It's exactly the same global-namespace problem we solve for ordinary class names with BEM or CSS Modules.

The most reliable fix is a unique name per element, set via an inline style – in a React component it's one line:

jsx

function ProductInfo({ id, text }) {
const anchor = "--delivery-" + id
return (
<>
<button popovertarget={"tip-" + id} style={{ anchorName: anchor }}>
i
</button>
<div id={"tip-" + id} popover="hint" style={{ positionAnchor: anchor }}>
{text}
</div>
</>
)
}

The spec also provides an anchor-scope property for this, which limits a name's visibility to a subtree (.product { anchor-scope: --delivery } and every card gets its own anchor). It's an elegant solution, but it reached browsers later than the rest of the mechanism – check support in your target browsers before you build a whole component on it.

There's a tempting third route too: a popover opened via popovertarget has, according to the spec, an implicit anchor – the button that opened it – so in theory you wouldn't need anchor-name at all. In practice this behaviour isn't consistent across browsers yet, and even the polyfill doesn't support it. An explicit anchor name costs one declaration and works everywhere.

Second pitfall: tree order

The anchor must come before the anchored element in the tree, or live in a different containing block. If the tooltip comes before the button in the HTML within the same positioned container, the browser treats the anchor as unavailable and silently ignores position-anchor – the element ends up wherever a plain position: absolute would put it. DevTools shows no warning, so it looks like a bug in position-area. With popovers the problem practically never occurs, because the top layer is its own containing block, but with plain position: absolute it's worth keeping in mind.

When Floating UI still makes sense

Anchor Positioning doesn't replace every library feature. Two things are still harder in plain CSS:

  • The tooltip arrow after a flip. When flip-block moves the tooltip below the icon, an arrow drawn with a pseudo-element still points down. CSS gives you no simple way to react to which option from position-try-fallbacks was chosen. The simplest workaround is a design without an arrow, or an arrow built as a separate element anchored to the same anchor.
  • Virtual anchors. A context menu at the mouse click position or a tooltip next to selected text has no DOM element to attach anchor-name to. Here a library with a "virtual element" is still more convenient.

For ordinary tooltips, dropdowns, user menus or form hints, CSS is more than enough.

Pitfalls and best practices

  • Combine Anchor Positioning with popover. The popover gives you the top layer, light dismiss and Escape handling; the anchor gives you the position. Together they replace most of what we used Floating UI for.
  • Reset inset and set your own margin on the popover. The default inset: 0; margin: auto from the user-agent stylesheet can shift the element in surprising ways.
  • Anchor names are global. In lists, use unique names via inline styles, or anchor-scope where it's supported. The same component in a loop with one name will attach everything to the last element.
  • Don't rely on the popover's implicit anchor. Explicit anchor-name and position-anchor work everywhere; the implicit anchor doesn't yet.
  • Add position-try-fallbacks: flip-block to every tooltip near the edge of the screen, and position-visibility: anchors-visible to elements inside scrolling containers.
  • Check tree order when position-anchor "does nothing" with plain position: absolute.
  • Support: Chrome 125, Safari 26 and Firefox 147 – the mechanism has been Baseline since January 2026. For older browsers, @supports (anchor-name: --a) with a simpler fallback (e.g. a tooltip below the element without flipping) or the OddBird polyfill is enough.

Summary

For years, positioning a tooltip relative to a button meant choosing one of two compromises: leave it in the tree and accept clipping by overflow, or move it to the end of the document and compute its position in JavaScript on every scroll. Popover solved the first part – nothing clips the top layer. Anchor Positioning solves the second: anchor-name on the button, position-anchor and position-area on the tooltip, and position-try-fallbacks takes over the role of the flip() middleware. The one thing to remember from the very first line is that anchor names are global – and a component used ten times on a page needs ten different names.