Anchor positioning

Topic 4 of 4 in Positioning7 min readBeginnerFree course

Overview

Tooltips, dropdown menus, popovers and “select” lists all need to appear next to another element — and stay next to it as the page scrolls, without falling off the edge of the screen. For years this needed JavaScript libraries that measured positions on every scroll. CSS anchor positioning does it natively: you tell one element which other element it’s anchored to, and where.

Support: Chrome and Edge 125+, Firefox 147+ (January 2026) and Safari 26+ (September 2025). It’s now usable in production, with a fallback for older browsers (below).

position-area places an element in a 3 × 3 grid around the anchor: top left, top, top right, left, center, right, bottom left, bottom, bottom right. Here a tooltip uses position-area: top. Code: .button { anchor-name: –help; } .tooltip { position: absolute; position-anchor: –help; position-area: top; position-try-fallbacks: flip-block; }. The tooltip is tethered to the button wherever it is on the page — no JavaScript positioning. If there isn’t room above, flip-block moves it below.

Naming an anchor

Give the anchor element a name — a dashed identifier, like a custom property:

CSS
.help-button {
  anchor-name: --help;
}

Positioning against it

The positioned element must be position: absolute or fixed, and refers to the anchor with position-anchor:

CSS
.tooltip {
  position: fixed;
  position-anchor: --help;
  position-area: top;
}

The tooltip can live anywhere in the HTML — it doesn’t need to be inside or next to the button. It’s positioned relative to the button’s on-screen position.

position-area: the easy way

position-area imagines a 3×3 grid around the anchor, with the anchor in the middle cell, and places the element in one or more cells:

CSS
position-area: top;            /* above, centred horizontally on the anchor */
position-area: bottom;         /* below, centred */
position-area: right;          /* to the right, centred vertically */
position-area: top left;       /* above and to the left (the corner cell) */
position-area: bottom span-right;  /* below, starting at the anchor's left edge, extending right */

If centring would push the element past the edge of its containing block, the browser shifts it inward to keep it inside. The element’s alignment within its area is chosen sensibly for you (an element above the anchor sits at the bottom of the “top” area, next to the anchor). Logical keywords like block-start and inline-end also work. Add margins for a gap:

CSS
.tooltip { position-area: top; margin-block-end: 0.5rem; }

anchor(): exact edges

For more control, use the anchor() function in inset properties. It returns the position of one of the anchor’s edges:

CSS
.menu {
  position: absolute;
  position-anchor: --menu-button;
  top: anchor(bottom);          /* menu's top = button's bottom */
  left: anchor(left);           /* left edges aligned */
  min-width: anchor-size(width); /* at least as wide as the button */
}

anchor-size() reads the anchor’s dimensions — perfect for dropdowns that should match their trigger’s width.

Staying on screen: fallbacks

A tooltip positioned above a button at the very top of the screen would be cut off. position-try-fallbacks lists alternatives the browser tries, in order, when the element would overflow its containing block:

CSS
.tooltip {
  position-area: top;
  position-try-fallbacks: flip-block;            /* try below instead */
}

.menu {
  position-area: bottom span-right;
  position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;
}
  • flip-block mirrors the position across the block axis (top ↔ bottom),
  • flip-inline mirrors across the inline axis (left ↔ right),
  • you can also list other position-area values, or custom options defined with @position-try.

The browser re-evaluates as the user scrolls and resizes, so the element always picks the first option that fits.

Use position: fixed for tooltips and menus. Fallbacks are tried when the element would overflow its containing block. For an absolutely positioned element, that’s usually the whole document — so near the top of the screen there’s still “room” above (in the part of the page you’ve scrolled past), and the tooltip never flips. With position: fixed, the containing block is the viewport, and fallbacks react to what the user can actually see. The element stays tethered to its anchor either way.

Showing and hiding

Anchor positioning only handles where; you decide when. With plain CSS:

CSS
.tooltip { visibility: hidden; }
.help-button:is(:hover, :focus-visible) + .tooltip { visibility: visible; }

For menus and richer popups, pair it with the Popover API, which handles showing, hiding, closing on Escape or an outside click, and puts the element in the top layer above everything else:

HTML
<button popovertarget="menu" class="menu-button">Options</button>
<div id="menu" popover class="menu">…</div>
CSS
.menu-button { anchor-name: --menu-button; }
.menu {
  position-anchor: --menu-button;
  position-area: bottom span-right;
  margin: 0;                       /* popovers are centred by default margins */
  position-try-fallbacks: flip-block;
}

Accessibility

Positioning doesn’t change semantics. Make sure:

  • tooltips are linked with aria-describedby and appear on focus as well as hover,
  • they can be dismissed with Escape, and stay open while hovered (WCAG 1.4.13),
  • menus use real buttons and links, and focus moves sensibly.

Fallback for older browsers

Wrap anchor positioning in @supports, and give older browsers something simple that still works:

CSS
.tooltip { position: absolute; }        /* fallback: positioned relative to a wrapper */
.has-tooltip { position: relative; }
.tooltip { bottom: calc(100% + 0.5rem); left: 50%; translate: -50%; }

@supports (anchor-name: --a) {
  .tooltip { bottom: auto; left: auto; translate: none; position-anchor: --help; position-area: top; }
}

Where it shines: anything that must stay attached to something else — tooltips, dropdowns, “select” menus, onboarding hints, labels on charts. Each one used to need a positioning library; now it’s a few lines of CSS.

Exercise

A tooltip that stays on screen

Make the .tip appear centred above the .help button, with a 0.5rem gap, using CSS anchor positioning (no JavaScript positioning). If there isn’t enough room above (try scrolling the button to the top of the screen), it should flip below the button automatically. Show the tooltip only while the button is hovered or focused.

HTML
<p style="margin-top: 40vh">
  Need a hand?
  <button class="help" aria-describedby="tip">?</button>
</p>
<div class="tip" id="tip" role="tooltip">Lessons unlock as you finish exercises.</div>

<style>
  .tip { background: #1b1d21; color: white; padding: 0.5rem 0.75rem; border-radius: 6px; max-width: 16rem; }
  /* your code here */
</style>

Summary

  • Anchor positioning tethers an absolutely or fixed-positioned element to another element; use fixed so fallbacks respond to the viewport.
  • Name the anchor with anchor-name: --x, then point at it with position-anchor: --x.
  • position-area places the element in a 3×3 grid around the anchor; anchor() gives exact edge positions.
  • position-try-fallbacks (for example flip-block) moves the element when it would overflow the screen.
  • Supported in Chrome and Edge 125+, Firefox 147+ and Safari 26+; provide a simple fallback for older browsers.