CSS Anchor Positioning: Tooltips and Menus Without JavaScript
Tether tooltips, dropdowns and popovers to any element, and keep them on screen, with a few lines of CSS
Positioning Things Next to Other Things, Finally in CSS
Tooltips, dropdown menus, date pickers and "new feature" callouts all share one problem: an element has to sit next to another element and stay there, even when the page scrolls or the trigger is close to the edge of the screen. For years the answer was a JavaScript library such as Popper or Floating UI that measured both elements and recalculated coordinates on every scroll and resize.
CSS anchor positioning lets you name an element as an anchor and position another element relative to it, entirely in CSS. The browser keeps them attached as the layout changes and can automatically flip the positioned element to another side when it would overflow. It is defined in the W3C CSS Anchor Positioning specification and has shipped in Chrome and Edge 125, Safari 26 and Firefox 147, so every major engine supports the core feature as of early 2026.
This guide focuses on the parts you will use every week: connecting an anchor, placing the element with position-area, fine-tuning with anchor() and anchor-size(), keeping it on screen with fallbacks, and pairing it with the Popover API. It also covers the details that cause most bugs: reused anchor names in lists, absolute versus fixed positioning, and accessibility.
You should already be comfortable with CSS positioning and z-index and stacking.
The Mental Model: One Anchor, One Positioned Element
Anchor positioning always involves two elements:
- The anchor is the element you attach to, such as a button. You give it a name with
anchor-name. Names are dashed identifiers, like custom properties:--menu-button. - The anchor-positioned element is the tooltip or menu. It must be absolutely or fixed positioned, and it points at the anchor with
position-anchor.
.menu-button {
anchor-name: --menu-button;
}
.menu {
position: absolute; /* or fixed */
position-anchor: --menu-button;
position-area: bottom; /* sit below the button */
}That is a complete, working example. Unlike classic absolute positioning, the menu does not need to be inside the button or share a positioned parent with it. The browser finds the anchor by name.
Implicit anchors with the Popover API
If the positioned element is a popover opened by a button with popovertarget, the browser makes that button its implicit anchor. You can skip anchor-name entirely:
<button popovertarget="account-menu">Account</button>
<div id="account-menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>#account-menu {
margin: 0;
position-area: bottom span-right;
}This is the best combination available today. The popover provides top-layer rendering (no more z-index fights), light dismiss with Escape or an outside click, and focus handling. Anchor positioning provides the placement. MDN documents the same implicit link for the newer commandfor attribute and for showPopover({ source }) in JavaScript.
position-area: Placement Without Math
position-area is the property you will use most. Picture a 3×3 grid drawn around the anchor, with the anchor in the middle cell. You choose the cell, or cells, the positioned element should occupy.
.tooltip { position-area: top; } /* above, centered */
.tooltip { position-area: bottom center; } /* same idea, explicit */
.tooltip { position-area: top left; } /* top-left corner cell */
.tooltip { position-area: bottom span-right; } /* below, aligned to the anchor's left edge, grows right */
.tooltip { position-area: right; } /* to the right, vertically centered */A few rules are worth memorising:
- One keyword means a side.
topis the same astop span-all: the top row, centered across all three columns. span-*keywords stretch across two cells.bottom span-rightcovers the bottom-center and bottom-right cells, which is how most dropdowns line up with the left edge of their button.- Logical keywords such as
block-endandinline-startfollow the writing direction. Use them for right-to-left support. Mixing logical and physical keywords in one value, such asblock-end left, makes the declaration invalid.
Adding a gap
position-area places the element flush against the anchor. Add space with margin on the positioned element, not padding on the anchor:
.tooltip {
position-area: top;
margin-bottom: 8px; /* gap between tooltip and anchor */
}anchor() and anchor-size() for Precise Control
When a grid cell is not precise enough, use the anchor() function inside inset properties (top, right, bottom, left and their logical versions). It returns the position of one of the anchor's edges.
.badge {
position: absolute;
position-anchor: --avatar;
/* center the badge on the avatar's top-right corner */
top: anchor(top);
left: anchor(right);
translate: -50% -50%;
}anchor() only works in inset properties. Writing margin-top: anchor(bottom) is invalid. It also accepts a fallback for when the anchor cannot be found: top: anchor(--avatar bottom, 2rem).
Centering on the anchor
To center a positioned element on its anchor along one axis, use the anchor-center value with the self-alignment properties:
.tooltip {
position-anchor: --info;
bottom: anchor(top);
justify-self: anchor-center; /* horizontally centered on the anchor */
}Sizing from the anchor with anchor-size()
anchor-size() reads the anchor's dimensions. The classic use is a select-style dropdown that is at least as wide as its button:
.dropdown {
position-anchor: --country-select;
position-area: bottom span-right;
min-width: anchor-size(width);
max-height: calc(anchor-size(height) * 8);
overflow-y: auto;
}position-area or anchor()?
Use position-area by default. It is shorter, it handles centering for you, and it works well with the flip fallbacks in the next section. Reach for anchor() when you need corner overlaps, offsets measured from a specific edge, or an element that stretches between two anchors.
Keeping It on Screen: position-try-fallbacks
A tooltip placed top on a button near the top of the viewport will be cut off. JavaScript libraries solved this with "flip" and "shift" logic. CSS now has it built in with position-try-fallbacks, a comma-separated list of alternatives the browser tries, in order, when the element overflows.
.tooltip {
position-area: top;
position-try-fallbacks: flip-block; /* try the bottom if the top overflows */
}
.submenu {
position-area: right span-bottom;
position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;
}flip-blockmirrors the position across the block axis (top and bottom, in horizontal writing).flip-inlinemirrors across the inline axis (left and right).flip-block flip-inlinetogether flip both, landing on the opposite corner.- You can also list plain
position-areavalues:position-try-fallbacks: bottom, right, left.
Custom fallbacks with @position-try
For full control, define a named fallback that can change several properties at once:
@position-try --below-and-wider {
position-area: bottom;
margin: 8px 0 0;
width: 20rem;
}
.tooltip {
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: --below-and-wider, flip-inline;
}Choosing the roomiest option
By default the browser uses the first fallback that fits. position-try-order tells it to prefer whichever option leaves the most space instead: position-try-order: most-height is useful for menus that may contain many items.
The absolute vs fixed gotcha
Fallbacks are triggered when the element overflows its containing block. With position: absolute, that is the nearest positioned ancestor, which may be far larger than the visible viewport. Flips then seem not to happen. If a tooltip should flip at the edge of the screen, use position: fixed (popovers in the top layer already behave this way), or make sure the containing block is what you expect.
Scrolling Containers and Reused Anchor Names
Hide the element when its anchor scrolls away
If an anchor sits inside a scrolling panel, its tooltip can end up floating over unrelated content after the anchor scrolls out of view. position-visibility handles this:
position-visibility: anchors-visiblehides the positioned element once its anchor is completely hidden.position-visibility: no-overflowhides the positioned element if it overflows its containing block, even after trying every fallback.alwaysis the default.
A hidden element behaves as if it had visibility: hidden, so it keeps no hit area.
anchor-scope: reusing one name in a list
Anchor names are document-wide. In a list of cards that each have their own "more options" button and menu, every button would get the same anchor-name, and every menu would attach to the same, last-laid-out button. anchor-scope limits a name to a subtree:
.card {
anchor-scope: --card-actions; /* each card gets its own --card-actions */
}
.card .actions-button {
anchor-name: --card-actions;
}
.card .actions-menu {
position: absolute;
position-anchor: --card-actions;
position-area: bottom span-left;
}Now each menu only looks for the anchor inside its own card. anchor-scope: all scopes every anchor name declared in the subtree. This is the property most tutorials leave out, and it is what makes anchor positioning practical in component libraries.
Three Production Recipes
1. A CSS-only tooltip for any trigger
<span class="has-tip">
<button class="tip-trigger" aria-describedby="tip-save">Save</button>
<span class="tip" id="tip-save" role="tooltip">Saves a draft (Ctrl+S)</span>
</span>.has-tip { anchor-scope: --tip; }
.tip-trigger { anchor-name: --tip; }
.tip {
position: fixed;
position-anchor: --tip;
position-area: top;
margin-bottom: 6px;
position-try-fallbacks: flip-block;
opacity: 0;
pointer-events: none;
transition: opacity 0.15s;
}
.tip-trigger:hover + .tip,
.tip-trigger:focus-visible + .tip {
opacity: 1;
}aria-describedby makes screen readers announce the tooltip text when the button receives focus, and the :focus-visible rule makes the tooltip appear for keyboard users, not just mouse users.
2. A dropdown that matches its button
#sort-menu {
margin: 4px 0 0;
position-area: bottom span-right;
position-try-fallbacks: flip-block;
min-width: anchor-size(width);
}Opened with <button popovertarget="sort-menu">, this menu needs no anchor-name, no z-index and no JavaScript.
3. A notification badge on an avatar
.avatar { anchor-name: --avatar; }
.avatar-badge {
position: absolute;
position-anchor: --avatar;
top: anchor(top);
left: anchor(right);
translate: -60% -40%;
}Because the badge is attached by name, it can live anywhere in the DOM, which is handy when a design system renders avatars and badges in separate components.
Browser Support, Feature Detection and Fallbacks
The core of anchor positioning (anchor-name, position-anchor, position-area, anchor(), anchor-size() and position-try fallbacks) is available in:
- Chrome and Edge 125 (2024)
- Safari 26 (2025)
- Firefox 147 (January 2026)
Newer additions from the next level of the spec, such as anchored container queries that style a tooltip differently depending on which fallback is active, were still Chromium-only in 2026. Check Can I use before relying on them.
Feature detection
/* Simple fallback: below the trigger, inside a positioned wrapper */
.has-tip { position: relative; }
.tip { position: absolute; top: 100%; left: 0; }
@supports (anchor-name: --a) {
.has-tip { position: static; }
.tip {
position: fixed;
top: auto;
left: auto;
position-anchor: --tip;
position-area: top;
position-try-fallbacks: flip-block;
}
}If you must support older browsers with full behaviour, the Oddbird CSS anchor positioning polyfill covers the core properties. Treat it as a bridge, not a permanent dependency.
Accessibility: Position Is Not Relationship
Anchor positioning only changes where something is drawn. Screen readers and keyboard navigation still follow the DOM and the accessibility tree, so:
- Connect the elements semantically. Use
aria-describedbyfor tooltips,aria-controlsandaria-expanded(orpopovertarget, which handles this for you) for menus. A tooltip that is only visually next to its button is invisible to assistive technology. - Keep DOM order sensible. If a popover's content sits far away in the DOM, keyboard users tab into it at an unexpected moment. The Popover API helps here because focus moves into the popover when it opens and returns to the invoker when it closes.
- Never make hover the only trigger. Every hover tooltip needs a
:focus-visibleequivalent, and touch users need a tap target. - Respect reduced motion for any entry animation. Wrap transitions in
@media (prefers-reduced-motion: no-preference).
For more on accessible interactive components, see the CSS accessibility tutorial and the HTML ARIA roles lesson.
Key Takeaways
- Name the anchor with
anchor-name, point to it withposition-anchor, and make the positioned elementabsoluteorfixed. - Popovers opened with
popovertargetget an implicit anchor, so you often need no names at all. - Use
position-areafor placement. Useanchor()andanchor-size()only when you need precise edges or sizes. - Add
position-try-fallbacks: flip-blockto anything near a screen edge, and preferposition: fixedwhen flips should react to the viewport. - Use
anchor-scopewhenever the same component repeats on a page. - Positioning is visual only. Link elements with ARIA or the Popover API.
Your next step: pick one tooltip or dropdown in your project that depends on a positioning library and rebuild it with a popover and position-area. Then compare bundle size and code.
Frequently Asked Questions
What is CSS anchor positioning?
It is a CSS feature that lets you position one element relative to another, the anchor, anywhere in the document. You name the anchor with anchor-name, link to it with position-anchor, and place the element with position-area or the anchor() function. The browser keeps them attached as the page scrolls or resizes.
Is CSS anchor positioning supported in all browsers?
The core feature is supported in Chrome and Edge 125, Safari 26 and Firefox 147, so all major engines support it as of early 2026. Some newer additions, such as anchored container queries, were still Chromium-only. Use @supports (anchor-name: --a) to provide a fallback.
Why does my position-try fallback never flip?
Fallbacks trigger when the element overflows its containing block. With position: absolute, that block may be much larger than the viewport, so nothing overflows. Use position: fixed, or a popover in the top layer, when the element should flip at the screen edge.
Can I use the same anchor-name more than once?
Yes, but without scoping every positioned element attaches to a single matching anchor. Add anchor-scope with the anchor name on each repeating component, such as a card or list item, so each positioned element only finds the anchor inside its own subtree.
Do I still need Popper or Floating UI?
For most tooltips, menus and popovers in browsers released since 2024–2026, no. Anchor positioning with position-try fallbacks covers placement and flipping. Keep a library if you must support older browsers without a polyfill or need behaviour such as virtual anchors tied to a text selection.
What is the difference between position-area and anchor()?
position-area places the element in one or more cells of a 3×3 grid around the anchor and handles alignment for you. anchor() returns the coordinate of a specific anchor edge for use in top, left and other inset properties, which gives precise control for overlaps and offsets.
References and Further Reading
- Using CSS anchor positioning · MDN Web Docs
- Fallback options and conditional hiding for overflow · MDN Web Docs
- CSS Anchor Positioning Module Level 1 · W3C
- The CSS anchor positioning API · Chrome for Developers
- CSS anchor positioning polyfill · OddBird
Related Articles
Continue your learning journey with these related topics