Anchor Positioning
Positioning a tooltip next to a button sounds trivial. In practice it requires measuring the button's position with getBoundingClientRect(), calculating offsets, clamping against viewport edges, re-running the calculation on scroll and resize, and somehow handling the fact that the tooltip lives in a different stacking context from the button. Every major UI library ships hundreds of lines of JavaScript to solve this one problem. CSS anchor positioning solves it in a handful of declarations.
Anchor positioning reached Baseline in January 2026 and now works in Chrome 125+, Edge 125+, Firefox 147+, and Safari 26. There are some behavioral differences between browsers, particularly around overflow handling and position-try-fallbacks. Always test in multiple browsers and see Can I Use: CSS Anchor Positioning for current support data.
Connecting an Anchor to a Target
Anchor positioning is a relationship between two elements. The anchor is any element you want to position relative to. The target is the element you want to move. You connect them with two properties: anchor-name on the anchor and position-anchor on the target.
/* The anchor: give it a name */
.btn {
anchor-name: --my-btn;
}
/* The target: reference the anchor by name */
.tooltip {
position: absolute;
position-anchor: --my-btn;
}
The name must be a dashed-ident: a custom identifier that starts with two hyphens, exactly like a CSS custom property. This naming requirement is intentional. It prevents collisions with future CSS keywords and makes anchor names visually distinct in your stylesheet. Each name must be unique per document; if two elements share the same anchor-name, the last one in DOM order wins.
The target must have position: absolute or position: fixed. Anchor positioning works within the CSS positioning model; it does not replace it. The anchor element itself needs no special positioning at all.
Why Not Just Use a Parent?
The traditional approach to tooltip positioning is wrapping the button and tooltip in a common parent with position: relative and absolutely positioning the tooltip inside it. This works when the tooltip stays within the parent's overflow bounds, but breaks the moment you need the tooltip to escape a clipping ancestor, appear above a sticky header, or be rendered in the top layer alongside other overlapping UI. Anchor positioning has no parent dependency. The anchor and target can be anywhere in the DOM and still maintain their spatial relationship.
Placing the Target: anchor() and position-area
Once the anchor and target are connected, you have two ways to describe where the target should appear.
The anchor() Function
The anchor() function reads a specific edge of the anchor element and returns it as a length value you can use in inset properties (top, right, bottom, left, or their logical equivalents). You use it like any other length value, which means you can combine it with calc(), add offsets, and compose it with other values.
.tooltip {
position: absolute;
position-anchor: --my-btn;
/* Align tooltip's bottom edge to anchor's top edge */
bottom: anchor(top);
/* Center the tooltip horizontally over the anchor */
left: anchor(center);
translate: -50% 0;
/* Add a small gap */
margin-bottom: 8px;
}
The available edge keywords are top, right, bottom, left, start, end, self-start, self-end, and center. The logical keywords respect writing mode, which matters when building components for internationalized layouts.
The position-area Property
position-area is a shorthand approach that imagines a 3x3 grid centered on the anchor. You describe which cell or cells of that grid the target should occupy using two-keyword values.
.tooltip {
position: absolute;
position-anchor: --my-btn;
/* Place above and centered */
position-area: top center;
/* Or just: */
position-area: top; /* center is implied on the cross axis */
}
The first keyword describes the block axis position (top, center, bottom, or their logical equivalents) and the second describes the inline axis (left, center, right). Spanning keywords like span-all let the target occupy multiple cells. For simple tooltip placement, position-area is more readable than anchor(). For precise sub-pixel control or asymmetric offsets, anchor() with calc() gives you more power.
Automatic Repositioning with position-try-fallbacks
The most powerful feature of anchor positioning is overflow-aware fallback. When the preferred position would cause the target to overflow the viewport or a clipping ancestor, the browser can automatically try a list of alternative positions and use the first one that fits.
.tooltip {
position: absolute;
position-anchor: --my-btn;
position-area: top;
margin-bottom: 8px;
/* If "top" overflows, try these in order */
position-try-fallbacks:
position-area(bottom),
position-area(left),
position-area(right);
}
This replaces the JavaScript pattern of measuring viewport edges, checking whether the tooltip clips, and swapping CSS classes to flip the position. The browser does all of that calculation natively. Move the anchor element to a corner of the screen and the tooltip repositions automatically. Resize the viewport and it adapts. No event listeners, no requestAnimationFrame, no layout thrash.
You can also define named @position-try blocks for more complex fallback states that change multiple properties at once, not just the position area:
@position-try --flip-below {
position-area: bottom;
margin-bottom: 0;
margin-top: 8px;
}
.tooltip {
position: absolute;
position-anchor: --my-btn;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: --flip-below;
}
position-try-fallbacks behavior differs between Chrome and Safari in some edge cases, particularly around how overflow is calculated when the anchor is inside a scroll container. Test both browsers when using fallbacks in production.
Sizing with anchor-size()
The anchor-size() function lets the target match the dimensions of its anchor. This is useful for dropdown menus that should be exactly as wide as the button that opens them, or popovers that should scale proportionally to their trigger.
.dropdown {
position: absolute;
position-anchor: --my-btn;
position-area: bottom;
/* Match the anchor's width exactly */
width: anchor-size(width);
/* Or proportionally */
min-width: anchor-size(width);
}
Available size keywords include width, height, block, inline, self-block, and self-inline. Like anchor(), these can be used inside calc() expressions.
Comparing to Popover and JavaScript Solutions
Anchor positioning works naturally alongside the HTML popover attribute, which handles the top-layer rendering and light-dismiss behavior that anchor positioning alone does not provide. The popover attribute ensures the tooltip or menu appears above all other content including fixed headers and modals. Anchor positioning then controls where in the viewport it appears relative to its trigger.
<button popovertarget="my-tip" anchor-name="--trigger">
Hover me
</button>
<div id="my-tip" popover>
Tooltip content
</div>
#my-tip {
position: fixed;
position-anchor: --trigger;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: position-area(bottom);
}
Before anchor positioning, placing a tooltip required either a DOM relationship (the tooltip inside a positioned parent), a JavaScript library like Floating UI or Popper.js, or a manual JavaScript solution using getBoundingClientRect(). The JavaScript approach is fragile: it runs after paint, causes layout thrash if not debounced carefully, and breaks when the page scrolls or resizes unless you re-run it on every relevant event. Anchor positioning eliminates all of that at the cost of slightly newer browser support.
Each option uses position-area with center on the cross axis so the bubble stays aligned with the anchor. Fallbacks are disabled here so you can compare sides cleanly; the reading above covers position-try-fallbacks for production.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
- What problem anchor positioning solves: why JavaScript tooltip libraries exist and what CSS is replacing, demonstrated by showing what the code looked like before and what it looks like now
-
The
anchor-nameandposition-anchorpairing, the dashed-ident naming requirement, and why the target needsposition: absoluteorposition: fixed -
The difference between
anchor()for precise edge-based placement andposition-areafor grid-based shorthand placement, with a live example of each -
How
position-try-fallbacksautomatically repositions the target when it would overflow the viewport, demonstrated by placing the anchor near a screen edge -
How anchor positioning pairs with the HTML
popoverattribute for a complete tooltip or menu solution - Honest browser support: Baseline 2026, works in Chrome, Edge, Firefox 147+, and Safari 26, with some behavioral quirks between browsers in fallback handling
-
Your own demo on your portfolio: a button with a tooltip or a trigger with a dropdown that uses
anchor-name,position-anchor, and at least one fallback
Start by showing the tooltip breaking when you remove position-try-fallbacks and drag the anchor to a corner. Then restore the fallbacks and show the automatic repositioning live.
Resources
- Chrome Developers: The CSS Anchor Positioning API — Comprehensive guide from the Chrome team covering every property with interactive examples
- MDN: Using CSS Anchor Positioning — Full walkthrough of the specification including edge cases and browser compatibility notes
- Can I Use: CSS Anchor Positioning — Current browser support table
- OddBird: Anchor Positioning Updates — An honest look at current cross-browser behavioral differences and known issues from one of the specification authors
- Kevin Powell: CSS Anchor Positioning is Awesome — Video walkthrough building a tooltip and dropdown from scratch with anchor positioning