# CSS Anchor Positioning vs JS Floating Positioning Engines (Floating UI)

- **Canonical URL:** https://frontend.rocks/vs/css-anchor-positioning-vs-js-floating-positioning-engines/
- **Relation:** Replaces
- **Last Verified:** 2026-11-15
- **Verdict:** Declarative anchor() and position-try-fallbacks replace JS getBoundingClientRect() measurement and repositioning loops.

## Comparison Matrix

| Dimension | CSS Anchor Positioning | JS Floating Positioning Engines (Floating UI) |
| :--- | :--- | :--- |
| **Maturity** | Experimental | Baseline · Widely |
| **Domain** | CSS & Layout | CSS & Layout |
| **Mental Model** | anchor-name and anchor() tether popovers and tooltips to trigger elements natively in Layout, including viewport-collision flips. | Floating positioning engines compute coordinates via getBoundingClientRect() and middleware pipelines (flip, shift, offset) synced to scroll and resize. |
| **Prerequisites** | Render Tree & Layout (Anchor positioning resolves inset coordinates during Layout without JS getBoundingClientRect() reads.) | Layout Thrashing (Forced Synchronous Layout) (Positioning loops must batch DOM rect reads before style writes to avoid layout thrashing.); JS ResizeObserver Pattern for Adaptive Layout (Floating engines observe reference and floating element resizes to keep coordinates accurate.) |
| **When Not to Use** | When not to use CSS Anchor Positioning: If you require 100% cross-browser parity today without a polyfill, use a lightweight JS positioning engine. | When not to use a JS positioning engine: For static centered modals, use Native <dialog>; in Chromium/Safari-only apps, prefer native CSS Anchor Positioning. |

## CSS Anchor Positioning — Under the Hood
Instead of JavaScript calling `getBoundingClientRect()` after paint and patching `top`/`left` on the next frame, the layout engine solves the tether in one pass.

1. **Anchor Registration**: `anchor-name: --trigger` exposes a box's geometry to later or Top Layer elements.
2. **Tethering**: `position-anchor: --trigger` with `position-area: bottom span-right` (or `top: anchor(bottom)`) locks the overlay to the anchor's edges.
3. **Collision Flip**: `position-try-fallbacks: flip-block, flip-inline` tests alternative placements during Layout when the overlay overflows the viewport.

```css
.menu-btn { anchor-name: --menu-btn; }

.menu-popover {
  position: fixed;
  position-anchor: --menu-btn;
  position-area: bottom span-right;
  position-try-fallbacks: flip-block, flip-inline;
}
```

Pairs cleanly with [Popover API](/concepts/popover-api-light-dismiss) and contrasts with [JS Floating Positioning Engines](/concepts/js-floating-positioning-engines).

## JS Floating Positioning Engines (Floating UI) — Under the Hood
A positioning engine acts like a surveyor taking one snapshot of two boxes, computing collision offsets in memory, and placing the overlay without triggering [Layout Thrashing](/concepts/layout-thrashing).

1. **Ancestor Discovery**: `autoUpdate` walks up the DOM to attach passive scroll listeners to every scrollable ancestor plus a [ResizeObserver](/concepts/js-resize-observer-pattern).
2. **Middleware Pipeline**: `offset(8)` adds gap, `flip()` checks overflow against the clipping boundary, and `shift()` nudges the overlay back inside the viewport.
3. **Transform Output**: Writing `transform: translate3d(x, y, 0)` rounded to device pixels avoids re-running Layout and keeps text crisp.

```js
const { x, y } = await computePosition(trigger, popover, {
  placement: 'bottom-start',
  middleware: [offset(6), flip(), shift({ padding: 12 })],
});
Object.assign(popover.style, {
  transform: `translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0)`,
});
```

## Interactive Labs & Dissections
- [CSS Anchor Positioning Lab](https://frontend.rocks/concepts/css-anchor-positioning/#lab) ([Markdown](https://frontend.rocks/concepts/css-anchor-positioning.md))
- [JS Floating Positioning Engines (Floating UI) Lab](https://frontend.rocks/concepts/js-floating-positioning-engines/#lab) ([Markdown](https://frontend.rocks/concepts/js-floating-positioning-engines.md))

## Related Architecture Autopsies
- [Autopsy #05: The z-index 9999 Modal Trap (and Its Native Exit)](https://frontend.rocks/autopsies/zindex-9999-tooltip-war/) ([Markdown](https://frontend.rocks/autopsies/zindex-9999-tooltip-war.md)): Tooltip or modal remains trapped behind a sticky header despite z-index: 9999 while JS positioning thrashes layout.

## Primary Sources
- https://drafts.csswg.org/css-anchor-position-1/
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_anchor_positioning
- https://floating-ui.com/docs/computePosition
- https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
