跪拜 Guibai
← All articles
CSS · Frontend · JavaScript

CSS Anchor Positioning Reaches Production: No More Manual Coordinate Math for Popups

By 牧艺 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Once the anchor is a real DOM box and your browser baseline meets early 2026 versions, the browser's native layout engine replaces the main-thread measurement and scroll listeners that Floating UI and hand-written code depend on. That simplification removes a whole category of listener leaks, but only if you correctly handle the new traps around name scoping, Popover UA reset, and list recycling.

Summary

CSS Anchor Positioning has officially become Baseline Newly Available as of January 2026, with Chrome 125+, Safari 26+, and Firefox 147+ all supporting the same core features. That means the browser can now handle popup placement, flipping, and overflow avoidance directly in its layout pass, replacing the getBoundingClientRect loops and scroll listeners that front-end devs have cobbled together for a decade.

The technique requires an explicit anchor name and a positioned element pointing at it. With `position-area` you pick a side of a nine-grid, and `position-try-fallbacks` gives the browser a list of alternative placements when the default overflows. List components need `anchor-scope` to avoid same-name collisions, and Popover integration demands resetting `inset` and `margin` that the UA styles set for centering.

There are still clear limits. The browser only flips when the box doesn't fit; it won't apply business priorities like "avoid covering this specific input." Non-element references (text selection, mouse coordinates) and complex arrow strategies continue to need Floating UI's middleware. The article ends with a practical checklist for adopting CSS Anchor Positioning while keeping a graceful fallback for older browsers.

Takeaways
— CSS Anchor Positioning became Baseline Newly Available on 2026-01-13 with Chrome/Edge 125, Safari 26, and Firefox 147.
— Always write an explicit `position-anchor` name; the initial value differed across engines until Chrome 151 aligned to `normal`.
— `position-area` uses a nine-grid (e.g., `block-end center`) to position relative to an anchor, replacing magic `top`/`left` numbers.
— `position-try-fallbacks: flip-block` mirrors along the block axis, and margins mirror too, so you don't write a separate above-position margin.
— Duplicate anchor names in a list resolve to the last in tree order; `anchor-scope` scopes the name to a component subtree.
— When integrating with the Popover API, reset `inset: unset` and the UA's centering `margin` or the `position-area` won't take effect.
— The browser handles overflow by trying fallback positions, but it won't implement business priorities like "don't cover a specific input"; that's still middleware territory.
— For non-element anchors (text selection, mouse coordinates, canvas projections), you must create a proxy element and update its coordinates from JavaScript.
— Use `position-visibility: anchors-visible` to hide when the anchor scrolls out of view; it does not reset `popover` state, so call `hidePopover()` explicitly on scroll/virtualization.
— Set a static fallback position outside `@supports (anchor-name: --a) and (position-area: top)` and use `CSS.supports` checks to conditionally load Floating UI only when Anchor Positioning is unavailable.
Conclusions

The cost saving is not just fewer main-thread calculations; it's the elimination of listener management — a whole class of dashboard bugs that come from forgetting to remove a `scroll` listener after a component is recycled.

The browser's flip mechanism is deliberately narrow: it only repositions when the box overflows, and it won't adjust arrow orientation or other visual details. That boundary keeps the CSS API simple but means design systems still need a coordination layer for anything beyond a plain tooltip.

The author's recommendation to explicitly name the anchor (`--row-menu` on the button) instead of relying on implicit anchors is a wise nod to code readability: implicit lookup is convenient but breaks down the moment a row has multiple triggers.

Using a zero-size proxy element for Canvas labels shows that Anchor Positioning's value is not in saving coordinate computation itself, but in offloading the flip and visibility logic — a trade-off that only pays off when the number of hotspots is small.

Concepts & terms
anchor-name / position-anchor
`anchor-name` gives an element a dashed identifier (e.g., `--my-anchor`); `position-anchor` on a positioned element points to that identifier, establishing an anchor relationship between the two.
position-area
A CSS property that positions an element relative to an anchor using a nine-grid (e.g., `block-end center` places it below and centered). It replaces manual `top`/`left` coordinates for common placement.
position-try-fallbacks
A CSS property that lists alternative placements (like `flip-block`, `flip-inline`, or named `@position-try` blocks) for when the default position overflows its containing block; the browser picks the first one that fits.
anchor-scope
A CSS property that limits the visibility of anchor names to a specific subtree, preventing name collisions in repeated list components. It's distinct from Shadow DOM encapsulation.
position-visibility
A CSS property that controls when a positioned element is shown relative to its anchor's visibility, e.g., `anchors-visible` hides the element when the anchor scrolls out of view.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗