What CSS Anchor Positioning actually solves
If you have ever built a tooltip, dropdown, or combobox, you know the job is rarely “center a div.” You calculate coordinates, track scroll and resize, watch the trigger move, avoid viewport edges, match widths, and avoid clipping ancestors. CSS anchor positioning lets a positioned element attach itself to another element in CSS, with the browser recalculating placement when layout changes.

The terminology matters:
anchor-namenames an anchor, for example--menu-button.position-anchorassigns an anchored element to that name.anchor()positions relative to an anchor side or center.anchor-size()reads anchor width or height, useful for matching a trigger width.position-trydefines fallback placements when the preferred position does not fit.inset-areais the newer name for aligning into grid areas around an anchor; older material may call itposition-area.
Support is evolving. Chrome and Edge shipped the core API first; Safari and Firefox support varies by property and preview status. Check current browser support before making it the only positioning mechanism. The examples below are written with progressive enhancement.

Tooltip: the smallest useful example
This tooltip opens on hover and keyboard focus. It is anchored above the button, with a bottom fallback.
<button class="tip-trigger" aria-describedby="tip">Save</button>
<span id="tip" class="tip" role="tooltip">Saves the current draft</span>
.tip-trigger {
anchor-name: --save-button;
}
.tip {
position: fixed;
position-anchor: --save-button;
inset: auto;
margin-bottom: 8px;
justify-self: anchor-center;
bottom: anchor(top);
background: #111827;
color: white;
padding: 6px 9px;
border-radius: 6px;
font-size: 13px;
width: max-content;
max-width: 240px;
}
@supports (position-anchor: --a) {
.tip {
opacity: 0;
pointer-events: none;
transition: opacity .15s ease;
}
.tip-trigger:hover + .tip,
.tip-trigger:focus-visible + .tip {
opacity: 1;
}
.tip {
position-try: flip-block;
}
}
Use a tooltip only for optional supplemental text. Do not put important actions, errors, or form requirements inside one; keyboard and touch users may never see it.
Dropdown menu: anchored, width-matched, and keyboard friendly
For a command menu, use aria-haspopup="menu" and role="menu" only if the items behave like application menu commands. For ordinary navigation links, use a plain listbox-style disclosure or navigation region instead.
<div class="dropdown">
<button id="menuBtn" class="menu-trigger"
aria-expanded="false" aria-controls="menu" aria-haspopup="menu">
Actions
</button>
<div id="menu" class="menu" role="menu" aria-labelledby="menuBtn" hidden>
<button role="menuitem">Rename</button>
<button role="menuitem">Duplicate</button>
<button role="menuitem" class="danger">Delete</button>
</div>
</div>
.menu-trigger { anchor-name: --actions-button; }
.menu {
position: fixed;
position-anchor: --actions-button;
inset: auto;
top: anchor(bottom);
justify-self: anchor-start;\n margin-top: 6px;
min-width: anchor-size(width);
border: 1px solid #d1d5db;
border-radius: 8px;
background: white;
padding: 4px;
box-shadow: 0 12px 30px rgb(0 0 0 / 14%);
}
.menu button {
display: block;
width: 100%;
text-align: left;
border: 0;
background: none;
padding: 8px 10px;
border-radius: 6px;
}
.menu button:hover,
.menu button:focus-visible {
background: #f3f4f6;
outline: none;
}
@supports (position-anchor: --a) {
.menu {
position-try: flip-block;
}
}
const btn = document.querySelector('#menuBtn');
const menu = document.querySelector('#menu');
const items = menu.querySelectorAll('[role="menuitem"]');
function openMenu(open) {
menu.hidden = !open;
btn.setAttribute('aria-expanded', String(open));
if (open) items[0].focus();
}
btn.addEventListener('click', () =>
openMenu(menu.hidden)
);
document.addEventListener('keydown', event => {
if (event.key === 'Escape' && !menu.hidden) {
openMenu(false);
btn.focus();
}
});
items.forEach((item, index) => {
item.addEventListener('keydown', event => {
if (event.key === 'ArrowDown') items[(index + 1) % items.length].focus();
if (event.key === 'ArrowUp') items[(index - 1 + items.length) % items.length].focus();
});
});
Arrow-key roving focus is enough for this short example. A production menu should also handle Home, End, typeahead, outside-pointer dismissal, and restoring focus after a command closes it.
Popover with smart fallbacks
The Popover API gives you top-layer rendering, light-dismiss behavior, and Escape handling without a manual overlay stack. Anchor positioning handles placement.
<button class="share-trigger" popovertarget="share">Share</button>
<div id="share" class="share-popover" popover="manual">
<h2>Share project</h2>
<label>Email
<input type="email">
</label>
<button id="closeShare">Done</button>
</div>
.share-trigger { anchor-name: --share-button; }
.share-popover {
position: fixed;
position-anchor: --share-button;
inset: auto;
top: anchor(bottom);
justify-self: anchor-end;\n margin-top: 8px;
width: 280px;
border: 1px solid #d1d5db;
border-radius: 10px;
padding: 14px;
background: #fff;
}
@supports (position-anchor: --a) {
.share-popover {
position-try: flip-block;
}
}
Use popover="auto" for simple lightweight dismissible UI. Use popover="manual" when you need explicit close logic, as above. If the popover contains a multi-step task, manage focus return and consider a short focus trap while it is open.
How this differs from traditional floating layers
A classic position: fixed solution usually calls getBoundingClientRect(), then writes top, left, width, or transforms. JavaScript must react to:
- page and container scrolling;
- window and container resizing;
- layout shifts that move the trigger;
- collisions with viewport edges;
- matching the trigger’s width;
- transforms, filters, and containing blocks;
- nested scrolling boundaries.
Anchor positioning handles layout recalculation and basic fallback placement declaratively. It does not replace every JavaScript concern: opening and closing, focus management, ARIA state, asynchronous content, complex collision middleware, and server-driven behavior still belong in script.
Also be precise about clipping. position: fixed is normally positioned relative to the viewport, but ancestors with transform, perspective, filter, backdrop-filter, contain, or certain will-change values can become the containing block. An ancestor with overflow: hidden can clip descendants even when they are fixed. A top-layer popover escapes ordinary overflow and most stacking problems because it is rendered in the top layer, but it does not erase the need for accessible focus behavior.
Progressive enhancement
Feature-test the property you need. For these examples, position-anchor is a useful gate:
@supports not (position-anchor: --a) {
.menu,
.share-popover {
/* fallback-safe base styles */
position: fixed;
}
}
A small JavaScript fallback can measure both elements and the viewport:
function placeFloater(trigger, floater, placement = 'bottom') {
const b = trigger.getBoundingClientRect();
const f = floater.getBoundingClientRect();
const gap = 8;
let left = b.left;
let top = b.bottom + gap;
if (left + f.width > window.innerWidth - 8) {
left = window.innerWidth - f.width - 8;
}
if (top + f.height > window.innerHeight - 8) {
top = b.top - f.height - gap;
}
Object.assign(floater.style, {
left: `${Math.max(8, left)}px`,
top: `${Math.max(8, top)}px`,
minWidth: `${b.width}px`
});
}
window.addEventListener('scroll', () => placeFloater(btn, menu), true);
window.addEventListener('resize', () => placeFloater(btn, menu));
For production-grade flip, shift, arrow alignment, nested scroll containers, and cross-platform edge cases, Floating UI is usually the better fallback. Use CSS anchoring when supported and call computePosition() only inside the negative feature check.
Portals remain useful when your component lives deep inside a clipping or stacking-context-heavy React or Vue tree. The Popover API can remove many portal requirements, but use a portal if you must support older browsers, need control over DOM placement for framework reasons, or are building a modal dialog that requires a robust focus trap.
When not to use CSS anchoring
Do not anchor every decorative layer. Avoid it for simple text that belongs in normal flow, for responsive regions that can naturally stack, or when older browser support is mandatory without a tested fallback. Do not use role="menu" for a generic navigation dropdown; that role changes screen-reader expectations and keyboard patterns.
The practical sweet spot is compact, trigger-owned UI: tooltips, command menus, color pickers, combobox listboxes, and share popovers. Start with accessible HTML and Popover semantics, add CSS anchoring, then keep a measured JavaScript fallback for browsers that have not caught up.