Start with the positioning problem, not the syntax
If your tooltip is clipped by a card, your dropdown closes when the modal scrolls, or your menu overflows the viewport, the issue is usually not one missing CSS line. It is the combination of DOM placement, containing block, overflow, scrolling, collision handling, and browser support.

Traditional wrapper-relative absolute positioning is still the right baseline for simple, local overlays. CSS Anchor Positioning removes a lot of JavaScript for anchored elements, but support is still uneven across the full feature set. Floating UI remains useful when collision detection, shifting, flipping, and older-browser support are non-negotiable.

Decision table: absolute, CSS anchor, or Floating UI
| Concern | Absolute positioning | CSS Anchor Positioning | Floating UI |
|---|---|---|---|
| DOM relationship | Usually needs the positioned ancestor and overlay near each other. | Can connect separate elements with CSS anchoring or the HTML anchor attribute. | Any DOM relationship; the reference and floating element are passed to JS. |
| Clipping | Easy to clip by overflow: hidden, cards, modals, and tabs. | Top-layer popovers avoid many ancestor clipping problems; non-top-layer anchors can still be affected. | Top-layer or portal patterns plus JS placement give the most control. |
| Collision | Manual only. | Position fallback and safe-area options exist, but syntax and support need checking. | Built-in flip, shift, hide, auto-placement, and middleware. |
| Scroll behavior | Moves with the containing block; often breaks inside scroll containers. | Can track the anchor, including scroll, with supported behavior and top-layer usage. | Explicit scroll and resize listeners; behavior is predictable across browsers. |
| Browser support | Universal. | Baseline support landed in Chrome/Edge 125, Safari 26, and Firefox 133 in 2025; subfeatures differ. | Broad support through JavaScript and regular CSS output. |
| Accessibility responsibility | You manage roles, focus, dismissal, and naming. | CSS positions content; it does not make an accessible widget by itself. | Same: the library positions, but your code owns ARIA, focus, and keyboard behavior. |
| Bundle cost | None. | None. | Small dependency, but still dependency, testing, and update cost. |
| Best fit | Local menus, badges, simple tooltips inside stable containers. | Modern apps using progressive enhancement for top-layer popovers and anchored UI. | Complex date pickers, command menus, rich select components, and broad browser support. |
Check syntax status before copying snippets
The anchor API has shipped in stages, so do not treat every example you find online as stable.
anchor-namegives an element an anchor identity, such as--menu-button.anchor()positions an element relative to that anchor, for exampletop: anchor(bottom).position-anchorassigns an anchor name to the positioned element.anchor-centeris used for centered alignment, commonly withjustify-itemsoralign-items.position-areaand the olderposition-area-top,position-area-bottom, and related longhands are the newer alignment model. Treat area syntax carefully; older implementations usedinset-area-*.- The HTML
anchorattribute creates an element relationship and can pair with the popover API, but it is not a complete replacement for CSS positioning rules.
Also, one @supports check is not enough. A browser may support anchor() but not every position-area longhand, fallback behavior, or HTML anchor feature combination. Feature-test the exact CSS path you use and keep a non-anchor layout underneath.
Complete migration: wrapper menu to top-layer anchored popover
The brittle absolute version
Symptom: the menu works in a table row until the row scrolls, then it is clipped by overflow: auto or hidden behind an adjacent stacking context.
<div class="menu-wrapper">
<button type="button" aria-expanded="false" aria-controls="actions-menu">
Actions
</button>
<div id="actions-menu" class="menu" role="menu">
<button role="menuitem">Edit</button>
<button role="menuitem">Archive</button>
</div>
</div>
<style>
.menu-wrapper { position: relative; }
.menu {
position: absolute;
top: calc(100% + 6px);
left: 0;
min-width: 180px;
}
</style>
Why it fails: the wrapper is both the positioning context and a descendant of clipping or transformed layout. Moving the menu elsewhere breaks top: 100%.
The anchored top-layer version
<button
id="actions-trigger"
class="actions-trigger"
type="button"
popovertarget="actions-menu"
aria-haspopup="menu"
aria-expanded="false"
aria-controls="actions-menu"
>
Actions
</button>
<div
id="actions-menu"
class="actions-menu"
popover="manual"
role="menu"
tabindex="-1"
aria-labelledby="actions-trigger"
>
<button type="button" role="menuitem">Edit</button>
<button type="button" role="menuitem">Archive</button>
</div>
.actions-trigger {
anchor-name: --actions-trigger;
}
.actions-menu {
position: absolute;
position-anchor: --actions-trigger;
top: calc(anchor(bottom) + 6px);
left: anchor(right);
justify-self: anchor-end;
margin: 0;
min-width: 180px;
}
@supports (top: anchor(bottom)) {
.actions-menu {
/* Keep only the exact declarations your migration requires. */
}
}
If you use the HTML relationship instead, add anchor="actions-trigger" to the menu. Keep CSS explicit during migration; mixing implicit HTML assumptions with copied area shorthands can make debugging harder.
Focus and Escape handling
CSS does not manage focus. When the popover opens, move focus to the menu or the first menu item depending on the widget pattern. Return focus to the trigger on close. Trap focus only while the menu is open, and support Arrow keys for a real menu pattern.
const trigger = document.querySelector('#actions-trigger');
const menu = document.querySelector('#actions-menu');
menu.addEventListener('toggle', () => {
const open = menu.matches(':popover-open');
trigger.setAttribute('aria-expanded', String(open));
if (open) menu.focus();
else trigger.focus();
});
menu.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
menu.hidePopover();
}
});
Boundary: if your design needs flip-to-top near the viewport edge, cross-browser safe alignment, or support for browsers without anchor positioning, use the fallback layer.
Use a layered fallback strategy
- Base layer: ship simple absolute positioning or a static accessible disclosure. This must remain usable, not merely visually hidden.
- Anchor layer: enable tested anchor CSS with a narrow
@supportscondition for the exact function or property used. - Floating UI layer: load it only when CSS anchoring is absent or when the component requires collision middleware. Avoid loading it for every tooltip if CSS covers your supported browsers.
Use stable CSS variables so all three layers speak the same language:
:root {
--ui-floating-offset: 6px;
--ui-floating-z: 1000;
--ui-floating-max-width: min(24rem, calc(100vw - 16px));
--ui-floating-placement: bottom-end;
}
Do not name variables after a library, such as --floating-ui-x, if your CSS and fallback implementations both need them. Consumers should override spacing and max width without editing component internals.
Common layout traps
Containing block surprises
Symptom: right: 0 aligns to a modal, table cell, or transformed wrapper instead of the viewport. Wrong fix: adding larger z-index. Positioning does not fix containing-block problems. Verify position, transform, perspective, filter, backdrop-filter, will-change, and containing properties.
Overflow clipping
Symptom: half the tooltip disappears inside a card with overflow: hidden. A top-layer popover escapes ancestor clipping; an ordinary anchored box does not automatically do so.
Transforms and filters
Symptom: your portal appeared to work until a parent animation added transform. These properties can create containing blocks and may affect fixed descendants. Test open menus while page or modal transitions are running.
Scroll containers
Symptom: the menu detaches, lags, or stays open after the trigger scrolls out of view. Decide whether the overlay should hide, track, or flip. CSS anchoring can track in supported cases; Floating UI gives explicit middleware and listeners.
Size shrinkage
Symptom: a menu shrinks to the widest word. Anchored sizing can become ambiguous when both axes depend on anchor edges. Set a sensible min-width or explicit anchored width, and cap it with max-width.
Logical properties and RTL
Symptom: left: 0 looks correct in English but overlaps content in Arabic or Hebrew. Prefer inset-inline-start, inset-inline-end, and placement names such as start and end. Test both LTR and RTL pages.
Collision and safe areas
Symptom: the popover renders under a browser bar or beyond the edge on mobile. bottom-end is a request, not a guarantee. Use safe alignment or collision logic when near viewport edges, notches, and fixed headers.
Top-layer assumptions
Symptom: z-index stops mattering, but focus and dismissal still fail. The top layer helps paint order and clipping; it does not assign role, focus movement, keyboard navigation, or accessible names.
Choose the maintainable path
Use absolute positioning when the overlay truly belongs to a stable local container. Adopt CSS Anchor Positioning behind feature checks for modern top-layer tooltips and menus, while keeping focus logic in your component. Reach for Floating UI when collision handling and broad browser support are part of the component contract. The upgrade is not about replacing every position: absolute; it is about moving complex overlays out of fragile containing blocks without silently dropping accessibility or fallback support.