What CSS anchor positioning actually replaces
CSS anchor positioning lets an anchored element position itself relative to another element without measuring the DOM. The anchored element still needs positioning, and the anchor needs an anchor name. Most production interfaces will keep a small amount of JavaScript for focus, ARIA, and open/close state; the part you can remove is getBoundingClientRect(), scroll listeners, collision math, and manual repositioning.

| Approach | Best for | Tradeoff |
|---|---|---|
| Pure hover tooltip | Static desktop help text | Fragile on touch, weak keyboard support, easy to clip |
getBoundingClientRect() | Full control everywhere | Requires scroll/resize updates and manual transforms |
| Floating UI | Broad browser support and advanced middleware | Adds JavaScript and runtime measurement |
| CSS anchors | Modern overlays that should follow layout natively | Needs feature detection and a fallback path |

Core syntax and containing-block limits
Give the anchor a name, then place the overlay with anchor() or anchor-side properties.
.trigger { anchor-name: --menu-button; }
.popup {
position: fixed;
position-anchor: --menu-button;
top: anchor(bottom);
left: anchor(center);
translate: -50% 8px;
}
Use position: fixed for overlays. It avoids clipping by ancestor overflow: hidden and avoids most containing-block surprises. If an ancestor has transform, filter, perspective, contain: paint, or similar properties, it can become the containing block for fixed descendants. Keeping popovers as siblings near the end of <body> is still the safest structure.
The adjacent sibling combinator only works when the trigger immediately precedes the popup: .trigger:hover + .popup. If there is even one wrapper element between them, use :has(), a state class, or the Popover API instead.
Browser support and feature detection
| Feature | Modern baseline | Notes |
|---|---|---|
| Anchor positioning | Chromium and Safari 18+; Firefox support is newer/flag-dependent depending on release | Check current support before enabling for all users |
anchor() with fixed positioning | Main supported pattern | Most reliable production starting point |
position-try / @position-try | Newer Chromium and Safari support; Firefox support varies | Treat as progressive enhancement |
| Popover API | Broad modern support | Still provide button-state and visibility fallbacks |
Do not assume anchor support from a browser name. Detect the CSS feature:
@supports (anchor-name: --a) {
.tooltip[data-native-anchor] {
position: fixed;
position-anchor: --tip;
}
}
For JavaScript-driven fallback selection, test CSS.supports('anchor-name', '--a'). If false, render a static accessible pattern or load Floating UI. Avoid loading positioning JavaScript for browsers that already support the CSS path.
Demo 1: accessible tooltip
This tooltip follows the button on scroll, flips near the block edge, and remains keyboard accessible. JavaScript only synchronizes hover/focus state and aria-describedby; it does no measuring.
<button type="button" class="tip-trigger" aria-describedby="save-tip">Save</button>
<span id="save-tip" role="tooltip" class="tip" data-native-anchor>Saves the draft</span>
.tip-trigger { anchor-name: --save-button; }
.tip {
display: none;
position: absolute;
top: calc(100% + 8px);
left: 0;
background: #111827;
color: white;
padding: 6px 8px;
border-radius: 6px;
white-space: nowrap;
}
.tip-trigger:hover + .tip,
.tip-trigger:focus-visible + .tip,
.tip:popover-open {
display: block;
}
@supports (anchor-name: --a) {
.tip[data-native-anchor] {
position: fixed;
position-anchor: --save-button;
top: anchor(bottom);
left: anchor(center);
translate: -50% 8px;
}
}
@supports (position-try: flip-block) {
.tip[data-native-anchor] {
position-try: flip-block;
}
}
const trigger = document.querySelector('.tip-trigger');
const tip = document.getElementById('save-tip');
function showTip() { tip.style.setProperty('display', 'block'); }
function hideTip() { tip.style.removeProperty('display'); }
trigger.addEventListener('mouseenter', showTip);
trigger.addEventListener('focus', showTip);
trigger.addEventListener('mouseleave', hideTip);
trigger.addEventListener('blur', hideTip);
In the unsupported fallback, this is a simple absolutely positioned tooltip. For clipping layouts, switch to Floating UI only inside the failing branch. The removed JavaScript is scroll/resize tracking; the required JavaScript remains keyboard/focus synchronization and accessible description wiring.
Demo 2: dropdown menu with Popover
For menus, use popover="auto" unless you specifically need manual control. Auto popovers support light dismiss: clicking outside, focusing outside, or opening another popover closes them. With popover="manual", togglePopover() does not add those behaviors; you must close every related popover yourself.
<button type="button" id="menu-button" class="menu-trigger" aria-haspopup="menu" aria-expanded="false" aria-controls="actions-menu">Actions</button>
<div id="actions-menu" class="menu" popover="auto" role="menu" aria-labelledby="menu-button">
<button role="menuitem" type="button">Rename</button>
<button role="menuitem" type="button">Duplicate</button>
<button role="menuitem" type="button">Archive</button>
</div>
.menu-trigger { anchor-name: --actions-trigger; }
.menu {
position: absolute;
top: calc(100% + 8px);
left: 0;
margin: 0;
padding: 6px;
border: 1px solid #d1d5db;
border-radius: 8px;
background: white;
box-shadow: 0 12px 30px rgb(0 0 0 / 14%);
}
.menu button {
display: block;
width: 100%;
padding: 8px 10px;
text-align: left;
background: none;
border: 0;
border-radius: 6px;
}
.menu button:hover,
.menu button:focus-visible {
background: #eef2ff;
}
@supports (anchor-name: --a) {
.menu[data-native-anchor] {
position: fixed;
position-anchor: --actions-trigger;
top: anchor(bottom);
left: anchor(start);
margin-top: 8px;
}
}
@supports (top: anchor(bottom)) and (position-try: flip-block) {
.menu[data-native-anchor] {
position-try: flip-block, flip-inline;
}
}
const button = document.getElementById('menu-button');
const menu = document.getElementById('actions-menu');
const supportsAnchors = CSS.supports('anchor-name', '--a');
menu.classList.toggle('native-anchor', supportsAnchors);
button.addEventListener('click', () => {
if (!menu.matches(':popover-open')) {
menu.showPopover();
return;
}
menu.hidePopover();
});
menu.addEventListener('toggle', () => {
const open = menu.matches(':popover-open');
button.setAttribute('aria-expanded', String(open));
if (open) menu.querySelector('[role="menuitem"]')?.focus();
});
document.addEventListener('keydown', event => {
if (event.key === 'Escape' && menu.matches(':popover-open')) {
menu.hidePopover();
button.focus();
}
});
The toggle event is the right place to update aria-expanded, because light dismiss can close the menu without another click on the trigger. If you only toggle ARIA in the button click handler, the button can incorrectly remain expanded after an outside click.
Automatic flipping and edge avoidance
The short modern rule is position-try: flip-block or flip-inline. Custom fallback positions use the newer at-rule:
@position-try --above {
top: anchor(top);
bottom: auto;
margin-top: 0;
margin-bottom: 8px;
translate: 0 -100%;
}
.menu[data-native-anchor] {
position-try-options: --above, flip-inline;
}
Browser syntax has moved during incubation: older examples use position-try-options; current implementations generally use position-try plus @position-try. Wrap both in @supports. Browsers that do not understand try rules simply keep the initial anchored placement, so define the most common position first.
Scrolling, clipping, and fallback strategy
Native anchors track normal document scrolling without JavaScript. They do not magically escape every clipping ancestor, which is why top-level fixed positioning is preferred for production overlays. Test inside the real containers: modal dialogs, transformed card grids, virtualized tables, and containers with overflow: auto.
Use three layers:
- Baseline: semantic button, accessible names, visible focus, and a simple positioned element.
- CSS enhancement: anchors, Popover styling, and position-try rules behind
@supports. - JavaScript fallback: when anchors are unsupported, either accept the baseline or dynamically initialize Floating UI.
For Popover fallback, keep the menu hidden with [hidden] or a .open class, toggle it with JavaScript, and manage focus and outside clicks manually. Do not treat Popover support and anchor support as the same feature; they must be detected separately.
This is the practical adoption path: ship the accessible baseline today, let modern browsers get native anchored behavior, and reserve Floating UI for the browsers and edge cases that actually need measurement.