Tooltips That Never Clip: CSS Anchor Positioning Recipes
October 9, 2026 | 9 min read | CSS
By Syed · Frontend Engineer & Creator of UITweak
The tooltip works in your demo, then dies in production: clipped by an overflow: hidden ancestor, buried inside a dialog, or detached from its button after the first scroll. You reach for a positioning library, add scroll and resize listeners, and now a 12 KB script does layout math the browser could do for free. CSS anchor positioning makes all of that obsolete. It tethers any positioned element to any other element with two declarations, keeps them glued through scroll and resize with zero JavaScript, and flips the tooltip automatically when it runs out of room. It reached Baseline in 2026, so every major browser ships it now. This guide is the problem-first version: four copy-paste recipes for the tooltip bugs that actually bite.
30-second version: anchor-name marks the trigger, position-anchor tethers the tooltip, and the browser keeps them glued through scroll and resize. position-try-fallbacks declares automatic flips. Pair with the popover API when the tooltip must escape overflow: hidden ancestors, and wrap everything in @supports (anchor-name: --x) so old browsers get a plain tooltip instead of a broken one.
Why Tooltips Get Clipped (and Why z-index Cannot Fix It)
Tooltip bugs are never about z-index. They come from three distinct traps:
- The overflow trap. The tooltip lives inside a card, table cell, or dropdown with
overflow: hiddenoroverflow: auto. The tooltip renders, but the ancestor clips it like a window frame. No z-index escapes clipping. Only moving the tooltip out of that subtree (or into the top layer) fixes it. - The containing-block trap. A
position: fixedtooltip inside an ancestor withtransform,filter, orperspectivestops being fixed to the viewport and becomes fixed to that ancestor instead. It slides away with the transformed parent on scroll. - The hand-rolled flip trap. Your JS flip logic measures the viewport, places the tooltip, and listens to scroll and resize. Then a nested scroll container moves, a dialog opens, or the layout shifts, and the tooltip detaches because one of the dozen listeners missed a case.
CSS anchor positioning kills the third trap entirely and gives you clean answers for the first two. The browser owns the tether, so there is no listener to miss and no measurement to go stale.
CSS Anchor Positioning Basics: anchor-name and position-anchor
The whole feature is a pair. You name the anchor, then you point the positioned element at it:
anchor-name: --save-btnon the trigger element turns it into a named anchor. The name is a dashed ident, so it must start with--. Names are document-scoped by default; for repeated components (a tooltip on every row of a table),anchor-scopekeeps each name visible only inside its own subtree.position-anchor: --save-btnon the tooltip sets its default anchor. From there, theanchor()function in any inset property resolves to the anchor's edges:top: anchor(bottom)means "my top edge sits at the anchor's bottom edge".
One rule to memorize: anchor() only works on positioned elements. The tooltip still needs position: absolute (or fixed); the anchor declarations only change wherethe insets resolve, not whether positioning applies.
Recipe 1: A Basic Anchored Tooltip
The classic: a tooltip above a button, centered on it, shown on hover and keyboard focus. Accessible markup included, because a tooltip sighted users can see but screen readers cannot is a bug:
<button class="trigger" aria-describedby="tip-save">Save</button>
<span class="tooltip" role="tooltip" id="tip-save">
Saved to your library
</span>/* Step 1: name the trigger */
.trigger {
anchor-name: --save-btn;
}
/* Step 2: tether the tooltip to it */
.tooltip {
position: absolute;
position-anchor: --save-btn;
/* 8px gap above the trigger, centered on it */
bottom: calc(anchor(top) + 8px);
left: anchor(center);
transform: translateX(-50%);
/* show / hide */
opacity: 0;
pointer-events: none;
transition: opacity 0.15s ease;
}
.trigger:hover + .tooltip,
.trigger:focus-visible + .tooltip {
opacity: 1;
}That is the entire positioning logic. Resize the viewport, scroll any ancestor, change the button's size: the tooltip stays glued to the button because the browser recalculates the tether on every layout pass. No getBoundingClientRect, no listeners, no library.
Recipe 2: Auto-Flip With position-try Fallbacks
A tooltip pinned above its button breaks when the button sits near the top of the viewport. This is the flip logic everyone hand-rolls and gets wrong. position-try-fallbacks declares it once and the browser tries each fallback in order until one fits:
.tooltip {
position: absolute;
position-anchor: --save-btn;
bottom: calc(anchor(top) + 8px);
left: anchor(center);
transform: translateX(-50%);
/* try below, then the sides, when "above" does not fit */
position-try-fallbacks: flip-block, flip-inline;
}flip-block mirrors the position across the block axis (above becomes below). flip-inline mirrors across the inline axis.flip-start combines both. The browser checks each candidate against the actual available space, including scroll position, and picks the first one that fits.
For full control, write a named fallback with @position-try. This one keeps the tooltip below the button but shifts it when the centered position would overflow the side:
@position-try --below-shifted {
top: calc(anchor(bottom) + 8px);
bottom: unset;
left: anchor(right);
transform: translateX(-100%);
}
.tooltip {
position: absolute;
position-anchor: --save-btn;
bottom: calc(anchor(top) + 8px);
left: anchor(center);
transform: translateX(-50%);
position-try-fallbacks: --below-shifted, flip-block;
}Two naming gotchas before you copy code from old tutorials. The property shipped in Chrome as inset-area (Chrome 125 to 130) and was renamed to position-area in 129; the old name was removed in 131, so use position-area. Likewise, position-try-options became position-try-fallbacks in Chrome 128. Old names silently do nothing, which looks exactly like broken code.
Recipe 3: Popover Pairing for Top-Layer Tooltips
Anchor positioning does not escape overflow: hidden. A tooltip tethered to a button inside a clipped card still gets clipped, because anchoring changes position, not painting order. For tooltips inside dialogs, dropdowns, or clipped cards, pair the anchor with the popover API: the popover promotes the tooltip to the top layer, above all clipping and stacking contexts, while the anchor keeps it tethered to its trigger.
<button class="trigger" id="btn-save">Save</button>
<div class="tooltip" id="tip-save" popover="manual" role="tooltip">
Saved to your library
</div>
<script>
const btn = document.getElementById("btn-save");
const tip = document.getElementById("tip-save");
btn.addEventListener("mouseenter", () => tip.showPopover());
btn.addEventListener("mouseleave", () => tip.hidePopover());
btn.addEventListener("focus", () => tip.showPopover());
btn.addEventListener("blur", () => tip.hidePopover());
</script>/* the popover UA stylesheet sets margin: auto and inset: 0,
which fights anchor positioning. Reset both first. */
[popover].tooltip {
margin: unset;
inset: unset;
position: absolute; /* top layer: relative to the viewport */
position-anchor: --save-btn;
bottom: calc(anchor(top) + 8px);
left: anchor(center);
transform: translateX(-50%);
position-try-fallbacks: flip-block, flip-inline;
}
.trigger {
anchor-name: --save-btn;
}The margin: unset; inset: unset; reset is the step everyone misses: the popover UA styles center the element with margin: auto, which overrides your anchor insets and parks the tooltip in the middle of the screen. Reset it, and the top-layer tooltip tethers exactly like the inline one, but nothing can clip it.
Use popover="manual" here, not "auto": auto popovers light-dismiss on any outside click, which fights hover behavior. For the full top-layer story, our modal top-layer guide explains stacking contexts and the top layer in depth, and our custom select guide shows more popover API patterns, including the trigger wiring the popover attribute gives you for free.
Recipe 4: The @supports Fallback for Older Browsers
Write the fallback first, then enhance. Old browsers ignore the @supports block and get a plain tooltip pinned above the trigger's wrapper. Modern browsers get the anchored, auto-flipping version:
/* Fallback: plain tooltip, no tether, no flip */
.tooltip {
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
transform: translateX(-50%);
opacity: 0;
pointer-events: none;
transition: opacity 0.15s ease;
}
/* Enhancement: tethered and auto-flipping */
@supports (anchor-name: --x) {
.trigger {
anchor-name: --save-btn;
}
.tooltip {
position-anchor: --save-btn;
bottom: calc(anchor(top) + 8px);
left: anchor(center);
position-try-fallbacks: flip-block, flip-inline;
}
}@supports (anchor-name: --x) is the standard feature test for the whole anchor positioning family. If you need the enhanced behavior in browsers older than Chrome 125, Firefox 147, or Safari 26, the Oddbird CSS anchor positioning polyfill covers the basics (anchor names and the anchor() function) back to much older releases, though not everyposition-try detail.
Style It Like a Pro: Shadow, Shape, and Motion
Positioning is half the tooltip; the other half is looking deliberate. Tooltips read best as small dark or light surfaces floating just off the trigger, and the shadow does most of that work. A single flat shadow looks pasted on; a layered shadow with a tight contact shadow plus a soft ambient one reads as elevation. Generate the stack visually in our shadow studio and paste the resulting value straight into the recipe:
.tooltip {
background: #1e293b;
color: #f8fafc;
font-size: 13px;
padding: 6px 10px;
border-radius: 8px;
white-space: nowrap;
/* layered shadow, built in the shadow studio */
box-shadow:
0 1px 2px rgb(15 23 42 / 0.35),
0 8px 24px -6px rgb(15 23 42 / 0.30);
}Two finishing touches. First, cap the width and allow wrapping for long text: max-width: 240px; white-space: normal; keeps a paragraph from becoming a banner. Second, remember that a tooltip's shadow is also subject to the overflow trap: if you skip the popover pairing, a clipped ancestor cuts the shadow too. Our CSS box-shadow guide covers layered shadow technique in detail when you want to go further.
Browser Support for CSS Anchor Positioning
Verified as of October 2026. The feature is Baseline 2026, newly available across all three engines:
- Chrome / Edge 125+ (May 2024).
position-areasince 129,position-try-fallbackssince 128 (renamed fromposition-try-options). - Firefox 147+, enabled by default (January 2026). Versions 145 and 146 carried it behind the
layout.css.anchor-positioning.enabledflag. - Safari 26+ (September 2025, macOS Tahoe / iOS 26).
- Popover API (for Recipe 3): Chrome 114+, Edge 114+, Firefox 125+, Safari 17+.
One honest caveat: the web-features project rates anchor positioning as Limited, meaning sub-features still diverge across engines (for example, newer additions like position-anchor: normal land in later versions). The core used in these recipes is stable everywhere, but keep the @supports guard from Recipe 4, and check caniuse for your audience before shipping.
Debugging anchored tooltips: the six-point checklist
When an anchored tooltip misbehaves, walk this list in order. One, the tooltip has position: absolute or fixed. anchor() resolves against the containing block on a statically positioned element, which usually parks the tooltip somewhere useless. Two, the anchor name matches exactly, including the -- prefix, on both the trigger's anchor-name and the tooltip's position-anchor. Three, the anchor element actually exists in the layout when the tooltip renders. Anchoring to an element inside a closed details element or a display: none subtree gives you nothing to tether to. Four, repeated components need anchor-scope. Without it, every tooltip on the page tethers to the first element carrying that anchor name in document order, and all your table-row tooltips pile onto row one. Five, popover tooltips need the margin: unset; inset: unset; reset from Recipe 3, or the UA stylesheet centers them. Six, put the @supports enhancement after the fallback styles. CSS source order still applies, and an enhancement block written before the fallback gets overridden by it.
anchor-size(): match the tooltip to its trigger
anchor() positions edges. anchor-size() sizes things. It resolves to the anchor's width, height, or axis size, which makes it the clean way to build dropdowns and menus that match their trigger exactly.
.menu {
position: absolute;
position-anchor: --menu-btn;
top: anchor(bottom);
left: anchor(left);
width: anchor-size(width);
}No JavaScript measuring the button, no resize listener when the button label changes. The menu tracks the trigger's width through every layout pass. You can also use it inside calc, for example width: calc(anchor-size(width) * 2) for a menu twice as wide as its button. In practice I reach for anchor-size() more often than anchor() itself, because "same width as the trigger" is the single most common dropdown requirement, and it used to need a ResizeObserver.
position-area: the shorthand worth learning
If the anchor() math feels fiddly, position-area is the shorthand. One declaration places the tooltip in a named area around the anchor, no inset arithmetic needed.
.tooltip {
position: absolute;
position-anchor: --save-btn;
position-area: top;
}That single line centers the tooltip above the trigger with the same result as the bottom/left/transform trio from Recipe 1. Other values, bottom, left, right, span-start, follow the same pattern. It pairs with position-try-fallbacks exactly like the longhand version, so your flip logic does not change. The one caveat is version support: position-area arrived in Chrome 129, later than the rest of the feature. Inside an @supports block that is a non-issue; the fallback path just needs the longhand insets. In practice I write the longhand for anything load-bearing and reach for position-area in newer codebases where the browser floor is already past it.
When anchor positioning is the wrong call
Three cases where I keep the JavaScript library. First, virtualized lists. The anchor must be a real element in the DOM at layout time. If your rows are recycled and the trigger is not rendered, there is nothing to tether to, and a library that positions against coordinates is the honest tool. Second, cross-shadow-DOM anchoring. Anchor names do not cross shadow boundaries by default, so a tooltip inside a web component cannot tether to a trigger outside it. Third, an existing Floating UI setup that already works. Rewriting working positioning code to delete a dependency is a refactor with real regression risk and no user-visible payoff. I am enthusiastic about anchor positioning for new code. I do not rewrite shipped code to get it.
Frequently Asked Questions
Do I still need Floating UI or Popper for tooltips?
For the common cases, no. Anchor positioning replaces the measure, place, and flip loop for tooltips, dropdowns, and menus tethered to DOM elements. The remaining JS cases are exotic: positioning relative to something not in the DOM (virtual lists), anchoring across shadow DOM boundaries, and menus that reposition based on sub-menu placement. If none of those apply, delete the dependency.
My anchored tooltip sits in the wrong place. What did I miss?
Almost always one of three things. The tooltip is not positioned: anchor() only resolves on an element with position set to something other than static. The anchor name is misspelled or missing its -- prefix. Or you are on a popover and forgot the margin: unset; inset: unset; reset, so the UA stylesheet is centering it.
Does the tooltip stay attached when the page scrolls?
Yes, and this is the whole point. The tether is recomputed by the layout engine, so scrolling any ancestor, resizing the viewport, or changing the trigger's size all move the tooltip with it. There is no scroll listener to attach and no stale measurement to invalidate.
Which browsers support position-try flip?
The same set as anchor positioning itself: Chrome/Edge 125+ (the -fallbacks name since 128), Firefox 147+, Safari 26+. The named @position-try blocks and the flip-block / flip-inline keywords are part of that same Baseline 2026 surface.
Recap: name the trigger with anchor-name, tether with position-anchor plus anchor(), flip with position-try-fallbacks, escape clipping with a popover in the top layer, and guard it all with @supports. Four recipes, zero listeners, zero libraries.