Skip to content
Tutorials Center
My learning

Topics

Lesson 108 of 139 ยท Modern CSS

Anchor Positioning

Tether tooltips, menus and labels to other elements with CSS anchor positioning, using anchor-name, position-area, anchor() and automatic fallbacks.

Tooltips, drop-down menus, "new" badges and help bubbles all have something in common: they must appear next to another element, wherever that element happens to be. For years the only reliable way to do that was JavaScript that measured the page, calculated coordinates and recalculated them on every scroll and resize.

CSS anchor positioning moves that job into CSS. You give one element a name, tell another element to position itself relative to that name, and the browser keeps them tethered. It even flips the element to the other side when it would run off the screen. In this lesson you will learn the core properties, the anchor() function, the simpler position-area, and how to add fallbacks.

Support is new. Anchor positioning works in Chrome and Edge (since 2024), Safari 26 and Firefox 147 (January 2026), so it is Baseline newly available, but older browser versions lack it. Check a current compatibility table before relying on it. Every demo below uses @supports and shows a message when your browser doesn't support the feature, and the lesson ends with a fallback strategy.

The three building blocks

Anchor positioning needs three things:

  1. An anchor. Give any element a name with anchor-name. Names are dashed identifiers, like custom properties: --profile, --menu-button.
  2. A positioned element. It must use position: absolute or position: fixed.
  3. A link between them. Set position-anchor on the positioned element to the anchor's name, then say where it should go.
CSS
.avatar {
  anchor-name: --profile;
}

.badge {
  position: absolute;
  position-anchor: --profile;
  position-area: top right;
}

The badge now sits off the top-right corner of the avatar. Move the avatar anywhere on the page, and the badge follows.

position-area: the easy way

position-area imagines a 3ร—3 grid around the anchor. The anchor is the center cell, and you choose which cell (or cells) the positioned element goes into:

TEXT
 top left    | top center    | top right
-------------+---------------+--------------
 center left |   [ANCHOR]    | center right
-------------+---------------+--------------
 bottom left | bottom center | bottom right

Single keywords work as shortcuts. position-area: bottom means the bottom row, centered under the anchor. top means the top row. You can also span cells: bottom span-right starts under the anchor and stretches toward the right, which suits drop-down menus that line up with the left edge of their button.

position-area around an anchor

Change the position-area values, for example to top left or bottom span-right, and run the example again. Notice that the labels come after the anchor in the HTML. An anchor must be laid out before the element that uses it, so placing the positioned element later in the document (or in the top layer, as you will see with popovers) is the safe habit.

The anchor() function for exact control

position-area covers most layouts. When you need precise control, use the anchor() function inside the inset properties (top, right, bottom, left, and their logical equivalents). It returns the position of one of the anchor's edges:

CSS
.tooltip {
  position: absolute;
  position-anchor: --help;

  /* My top edge sits on the anchor's bottom edge, plus a 6px gap */
  top: calc(anchor(bottom) + 6px);

  /* My left edge lines up with the anchor's left edge */
  left: anchor(left);
}

anchor() accepts the sides top, right, bottom, left, center, the logical start and end, or a percentage such as anchor(25%). Because it returns a length, you can use it inside calc().

To center an element on its anchor, combine anchor(center) with a different alignment trick. The cleanest is justify-self: anchor-center, which centers the positioned element horizontally on the anchor:

CSS
.tooltip {
  position: absolute;
  position-anchor: --help;
  bottom: calc(anchor(top) + 8px);
  justify-self: anchor-center;
}

There is also an anchor-size() function for sizes. For example, width: anchor-size(width) makes a menu exactly as wide as the button that opens it.

A tooltip that follows its button

Here is a small, complete tooltip. It appears when the button is hovered or focused, sits centered above it, and stays attached while the page scrolls:

An anchored tooltip

Hover over the button or reach it with Tab. The tooltip is linked to the button with aria-describedby, so screen readers announce the description too. CSS positioning never changes what assistive technology hears, so that ARIA connection still matters.

Fallbacks when space runs out

A tooltip placed above a button near the top of the screen would be cut off. Anchor positioning can try alternative positions automatically with position-try-fallbacks.

The simplest fallbacks are built-in keywords:

  • flip-block mirrors the position across the block axis (above becomes below).
  • flip-inline mirrors it across the inline axis (left becomes right).
  • flip-block flip-inline does both at once.
CSS
.menu {
  position: absolute;
  position-anchor: --menu-button;
  position-area: bottom span-right;
  position-try-fallbacks: flip-block, flip-inline;
}

The browser tries the main position first. If the element would overflow its containing block, it tries each fallback in order and uses the first that fits.

For full control, define a named fallback with the @position-try at-rule and list it by name:

CSS
@position-try --to-the-left {
  position-area: left;
  margin-right: 8px;
}

.popover {
  position-area: right;
  position-try-fallbacks: --to-the-left, flip-block;
}

Earlier drafts and browser versions used different names: inset-area instead of position-area, and position-try-options instead of position-try-fallbacks. If you read older articles, translate those names to the current ones.

Anchoring popovers

Anchor positioning pairs perfectly with the HTML popover attribute, which you met in The Popover API. A popover lives in the top layer, above everything else, so it can never be clipped by a parent with overflow: hidden. Anchoring then places it next to its button.

An anchored popover menu with fallbacks

The button sits at the right edge, so a menu stretching to the right would not fit. flip-inline kicks in and the menu opens toward the left instead. Move the button to the left by changing text-align and run the example again: the menu now uses its primary position. The popover's default styles center it in the viewport with inset: 0 and margin: auto, which is why the example resets inset and margin.

A fallback strategy for today

Because older browsers lack it, design so that browsers without anchor positioning still work:

  • Wrap the anchoring rules in @supports (anchor-name: --a) as the demos do, and keep a simpler layout outside it.
  • A classic fallback is to wrap the anchor and the positioned element in a container with position: relative and place the element with ordinary top/left. It won't flip near edges, but it stays close to its anchor. See Positioning for how containing blocks work.
  • For popovers, the unanchored fallback is a centered panel, which is perfectly usable.
  • If precise placement is essential, a small positioning library can fill the gap, loaded only when CSS.supports("anchor-name: --a") is false (see Feature Queries).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which property gives an element a name that others can anchor to?

    anchor-name: --something names the anchor. The positioned element points at it with position-anchor.
  2. 2. What does position-area: bottom span-right do?

    position-area uses a 3ร—3 grid around the anchor. bottom picks the bottom row and span-right spans from the center column toward the right.
  3. 3. Which value is a valid way to say "my top edge sits 6px below the anchor's bottom edge"?

    anchor(bottom) returns the position of the anchor's bottom edge, and calc() adds the gap.
  4. 4. What does position-try-fallbacks: flip-block do?

    Fallbacks are tried in order only when the main position would overflow. flip-block mirrors the position vertically in horizontal writing modes.
  5. 5. Which position value can an anchored element use?

    Anchor positioning works on absolutely positioned elements, which includes position: fixed and popovers in the top layer.

Summary

  • anchor-name names an anchor; position-anchor links an absolutely or fixed-positioned element to it.
  • position-area places the element in a 3ร—3 grid around the anchor; it covers most tooltips and menus.
  • anchor() returns an anchor edge for precise insets, anchor-size() returns its size, and justify-self: anchor-center centers on it.
  • position-try-fallbacks with flip-block, flip-inline or named @position-try rules keeps elements on screen.
  • Popovers plus anchor positioning give you menus that are never clipped and always attached.
  • Older browser versions lack it: use @supports and design a working fallback.

Next: @scope, a new way to limit where styles apply and stop components from leaking into each other.