How to Use CSS Anchor Positioning (No More Popper.js)
CSS Anchor Positioning tethers a floating element (like a tooltip or dropdown) to a reference target without JavaScript coordinate math. Name the anchor using 'anchor-name: --my-anchor;', link the floating element with 'position-anchor: --my-anchor;', and position it using 'top: anchor(bottom); left: anchor(center);'. To flip automatically when hitting screen edges, add 'position-try-fallbacks: flip-block;'.
Step 1: Why CSS Anchor Positioning Replaces Popper.js and Floating UI
For over a decade, tethering dropdown menus, tooltips, and contextual action bars to buttons required heavy JavaScript libraries (like Popper.js or Floating UI). These scripts constantly listen to scroll, resize, and DOM mutations, calculating pixel offsets and updating inline styles on the main thread. CSS Anchor Positioning delegates all tethering calculations to the browser's native layout engine, keeping tooltips glued to their anchors at zero JavaScript cost.
/* 1. Declare the anchor reference */
.trigger-btn {
anchor-name: --avatar-anchor;
}
/* 2. Tether the floating tooltip to the anchor */
.user-tooltip {
position: fixed;
position-anchor: --avatar-anchor;
/* Position below the anchor and horizontally centered */
top: anchor(bottom);
left: anchor(center);
transform: translateX(-50%);
margin-top: 8px;
}
Step 2: Automatic Viewport Edge Flipping with position-try-fallbacks
The hardest part of floating UI has always been collision detection: what happens when a button is near the bottom of the screen and the dropdown gets cut off? CSS solves this natively with 'position-try-fallbacks'. If the popover overflows the viewport, the browser automatically tries alternative position definitions, like flipping above the anchor.
.smart-dropdown {
position: fixed;
position-anchor: --menu-btn;
top: anchor(bottom);
left: anchor(start);
/* If bottom overflows viewport, automatically flip to top */
position-try-fallbacks: flip-block;
}
Step 3: Tethering Across Different DOM Trees and Modals
A revolutionary capability of Anchor Positioning is that the floating element does not need to be a DOM child of the anchor. A tooltip rendered inside the top-layer native '
<!-- Button deep inside a scrollable sidebar -->
<button class="action-btn" style="anchor-name: --share-btn;">Share</button>
<!-- Floating element located anywhere in the DOM -->
<div popover class="share-menu" style="position-anchor: --share-btn; top: anchor(top); left: anchor(right);">
<ul><li>Copy Link</li><li>Embed</li></ul>
</div>
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What browsers currently support CSS Anchor Positioning?
CSS Anchor Positioning is fully supported in Chrome 125+, Edge 125+, and Chromium-based browsers, with Safari and Firefox implementing the standard behind preview flags. A lightweight official polyfill (@oddbird/css-anchor-positioning) is available for older browsers.
Can multiple floating elements tether to the exact same anchor?
Yes! Any number of elements can declare the same 'position-anchor: --my-anchor'. For example, you can anchor an icon badge to the top-right corner of a button and a tooltip underneath it simultaneously.
How does anchor-size() work for matching element widths?
The 'anchor-size()' function allows the floating element to match the anchor's dimensions (e.g. 'width: anchor-size(width);' or 'min-width: anchor-size(width);'). This is ideal for auto-complete dropdowns that must precisely match their input field width.
What happens if an anchor scrolls out of the visible screen?
By default, the tethered element stays pinned to the anchor and scrolls off-screen with it. You can use 'position-visibility: anchors-visible;' to hide the floating element automatically when its anchor is scrolled out of view.
How do you align tooltips to the center of an anchor?
Use 'left: anchor(center); justify-self: anchor-center;' or pair 'left: anchor(50%);' with 'transform: translateX(-50%);'.