Run this yourself. The script that produced the bundle figures below:
anchor-positioning-bundle-cost/in the Coding Dunia code-examples repo.
CSS anchor positioning is a set of CSS properties, anchor-name, position-anchor, and the anchor() function, that tie one element's position to another element's rendered location without any JavaScript calculation. Every tooltip and dropdown library exists to solve one problem: keep element B positioned relative to element A, correctly, even as the page scrolls, resizes, or the anchor moves. That's a surprisingly involved calculation once you account for viewport edges, and it's exactly the calculation CSS anchor positioning now does natively.
Linking an anchor to a tooltip
.trigger-button {
anchor-name: --my-tooltip-anchor;
}
.tooltip {
position: fixed;
position-anchor: --my-tooltip-anchor;
inset: auto;
margin: 8px 0 0;
top: anchor(bottom);
left: anchor(center);
translate: -50% 0;
}
<button class="trigger-button" popovertarget="tip">Show tip</button>
<div id="tip" class="tooltip" popover>Helpful tooltip text</div>
anchor-name gives the button a named anchor reference. position-anchor on the tooltip points at that name, and anchor(bottom)/anchor(center) compute the tooltip's top/left relative to the anchor's actual rendered position, bottom edge and horizontal center respectively. The inset: auto; margin: 0 reset isn't optional: the browser's default popover styles set inset: 0 and margin: auto to center the popover in the viewport, and if you only override top and left, the leftover right: 0, bottom: 0 and auto margins drag the tooltip away from its anchor. popovertarget is what opens it on click; a popover element with nothing pointing at it never shows. This recalculates automatically on scroll and resize, the same behavior Floating UI's autoUpdate() exists to provide manually. Per MDN, the anchor name itself is a dashed-ident, the same custom-property-style syntax CSS variables use, which means one anchor element can be referenced by several positioned elements at once, a single button anchoring both a tooltip and a badge, for example, without any extra JavaScript wiring.
Fallback positions with @position-try
.tooltip {
position: fixed;
position-anchor: --my-tooltip-anchor;
inset: auto;
margin: 8px 0 0;
top: anchor(bottom);
left: anchor(center);
translate: -50% 0;
position-try-fallbacks: --above;
}
@position-try --above {
top: auto;
bottom: anchor(top);
margin: 0 0 8px;
}
position-try-fallbacks: --above tells the browser: if the tooltip would overflow the viewport in its preferred position (below the anchor), try the --above fallback instead, which pins the tooltip's bottom edge to the anchor's top edge. Two rules trip people up here. A custom @position-try name has to be a dashed-ident like --above, because flip-block and its siblings are reserved keywords, and a block named @position-try flip-block is dropped as invalid. And only inset, margin, sizing, self-alignment and anchor properties are allowed inside the block, so a translate there is ignored, which is why the 8px gap lives in margin rather than in translate. In this case you don't need the custom block at all: position-try-fallbacks: flip-block; on its own mirrors top to bottom and margin-top to margin-bottom and gives the same result. Several built-in try-tactic keywords exist (flip-block, flip-inline, flip-start) covering the common flip cases without a custom @position-try block at all, worth checking before writing a manual fallback. Per the spec, you can also list multiple fallbacks in priority order, position-try-fallbacks: flip-block, flip-inline;, and the browser evaluates them one at a time until it finds a position that fits, which is the same collision-avoidance logic Floating UI's autoPlacement() middleware implements in roughly two hundred lines of JavaScript.
Anchor positioning plus popover
<button popovertarget="user-menu" class="menu-trigger">Account</button>
<div id="user-menu" popover class="dropdown-menu">
<a href="/profile/">Profile</a>
<a href="/settings/">Settings</a>
<a href="/logout/">Log out</a>
</div>
.menu-trigger {
anchor-name: --user-menu-anchor;
}
.dropdown-menu {
position-anchor: --user-menu-anchor;
inset: auto;
margin: 4px 0 0;
top: anchor(bottom);
right: anchor(right);
}
The popover attribute is an HTML global attribute that gives any element top-layer rendering, meaning it paints above the rest of the page regardless of stacking context, along with built-in light-dismiss and keyboard accessibility, all without JavaScript. popovertarget on the button wires up open/close behavior, focus handling, and light-dismiss (clicking outside closes it) entirely through HTML attributes, no JavaScript addEventListener needed for that part either. anchor-name/position-anchor handle where it appears. Together, this is a complete dropdown menu, positioning, top-layer rendering, dismiss behavior, and keyboard accessibility, without a single line of JavaScript or a dependency, replacing what used to take two or three separate npm packages working in concert.
Sizing from the anchor with anchor-size()
.dropdown-menu {
position-anchor: --user-menu-anchor;
inset: auto;
margin: 4px 0 0;
top: anchor(bottom);
right: anchor(right);
max-height: anchor-size(height); /* size relative to the anchor's own height, if useful */
max-width: 320px;
overflow-y: auto;
}
anchor-size() reads a dimension from the anchor element itself, useful for a dropdown that should match its trigger's width exactly (width: anchor-size(width);), rather than having a fixed or content-based width unrelated to the button that opened it. According to Chrome for Developers, anchor-size() accepts width, height, block, inline, self-block and self-inline, so a dropdown can size either axis from the trigger, not just match its width. It reads the anchor's box, not the free space around it; picking the fallback with the most room is the job of position-try-order: most-height.
Browser support and the JavaScript fallback
Anchor positioning shipped in Chrome and Edge in 2024, with Firefox and Safari following through 2025 and into 2026. For the remaining gap, this is a feature where a JavaScript fallback still matters more than most CSS features covered elsewhere in this series, an unsupported browser doesn't gracefully degrade to "no positioning," it renders the tooltip at its default static position, likely overlapping other content. Feature-detect with @supports (anchor-name: --a) and load Floating UI conditionally only for browsers that fail that check, rather than assuming universal support the way you reasonably could with :has() or scroll-driven animations by 2026.
A quick three-step check before shipping anchor positioning without a fallback:
- Confirm your actual analytics show the browser mix you're targeting, don't assume from caniuse alone.
- Wrap the CSS in
@supports (anchor-name: --a)so unsupported browsers can be routed to a JavaScript fallback instead of silently rendering overlapping content. - Load Floating UI or a similar library conditionally, only inside the unsupported branch, so supported browsers never pay for the extra dependency.
Anchor positioning is the rare 2026 CSS feature where an unsupported browser does not gracefully degrade, it renders the positioned element at its default static location, likely overlapping other content, which is why feature-detecting with @supports before dropping a JavaScript fallback still matters here.
The same guard-first habit applies to newer value-level features, for example CSS if() with an @supports gate.
Before and after, job by job
Four common jobs, tooltip attachment, scroll recalculation, viewport-overflow handling, and dropdown dismiss behavior, used to each need their own piece of JavaScript. The table below lines up what each one cost before against what it costs with anchor positioning and popover combined.
| Task | Before | With anchor positioning |
|---|---|---|
| Tooltip stays attached to its trigger | Floating UI / Popper.js, JS position calc | anchor-name + anchor() |
| Recalculate on scroll/resize | Manual autoUpdate() listener | Automatic, native |
| Flip to avoid viewport overflow | Manual collision detection logic | position-try-fallbacks |
| Dropdown open/close, focus, dismiss | Custom JS or a UI library | HTML popover attribute |
What the dependency weighs
"Removes a dependency" is the kind of claim that sounds like a win until someone asks how big the dependency was. So I weighed it: not computePosition on its own, which would flatter the CSS side unfairly, but the tooltip nobody would ship without, with offset for the gap, flip to move above the trigger when there's no room below, shift to keep it inside the viewport and arrow for the triangle. The build is bundled with esbuild, minified, then compressed.
@floating-ui/dom 1.8.0, tooltip build | Bytes | |
|---|---|---|
| minified | 15,098 B | 14.74 kB |
| minified + gzip | 6,115 B | 5.97 kB |
| minified + brotli | 5,529 B | 5.40 kB |
Measured 2026-08-24 on Node 20.20.0, esbuild 0.28.2. The CSS version ships 0 B of JavaScript.
Six kilobytes gzipped isn't going to sink your page, and I'd rather say that than pretend otherwise. Tree-shaking also means your figure moves with your middleware list, so a tooltip that never flips comes in lighter than this one. The better reason to reach for anchor() is maintenance: a dependency you deleted has no upgrade path, no breaking change in its next major, and no autoUpdate() listener you have to remember to tear down.
When to drop the library
Anchor positioning adds nothing the web had no workaround for; what it removes is the JavaScript dependency that workaround required. For a straightforward tooltip or dropdown, anchor-name/anchor() combined with the popover attribute covers what Floating UI existed to solve, with the caveat that support isn't universal enough yet to drop the JavaScript fallback for every project without checking your actual browser support requirements first.