For years, putting a tooltip next to a button meant reaching for JavaScript. You measured the button with getBoundingClientRect(), worked out where the tooltip should go, listened for scroll and resize events, and then handled the case where the tooltip ran off the edge of the screen. Libraries like Floating UI and Popper exist because this is fiddly to get right.

CSS anchor positioning moves that job into the browser. You name one element as an anchor, tell another element to position itself relative to it, and give the browser a list of fallback positions to try when space runs out. The browser keeps everything in place as the page scrolls and resizes.

This article is for front-end developers who build tooltips, dropdown menus, popovers or any other UI that needs to sit next to something else. We will start with the basics and build up to a dropdown menu that flips when it hits the edge of the viewport, with no JavaScript involved.

The two halves: anchors and positioned elements

#

Anchor positioning always involves two elements:

  • The anchor: the element you want to attach something to, such as a button.
  • The anchor-positioned element: the thing that attaches to it, such as a tooltip. It must be absolutely or fixed positioned.

You connect them with a dashed-ident name, the same style of name you use for custom properties.


_10
<button class="trigger">Save</button>
_10
<div class="tooltip">Saves your draft</div>


_10
.trigger {
_10
anchor-name: --save-button;
_10
}
_10
_10
.tooltip {
_10
position: absolute;
_10
position-anchor: --save-button;
_10
}

anchor-name declares the anchor. position-anchor tells the tooltip which anchor it belongs to. On its own this does not move the tooltip anywhere useful yet. For that, you need either the anchor() function or the position-area property.

Placing things with position-area

#

position-area is the quickest way to get started. Imagine a three by three grid drawn around the anchor, with the anchor in the centre cell. position-area places the positioned element into one or more cells of that grid.


_10
.tooltip {
_10
position: absolute;
_10
position-anchor: --save-button;
_10
position-area: top;
_10
}

That puts the tooltip above the button, centred horizontally. A few other useful values:


_11
/* Below the anchor, centred */
_11
position-area: bottom;
_11
_11
/* To the right, vertically centred */
_11
position-area: right;
_11
_11
/* Below the anchor, starting at its left edge and extending right */
_11
position-area: bottom span-right;
_11
_11
/* Top-left corner, outside the anchor */
_11
position-area: top left;

The span- keywords let the element cover two cells, which is handy for dropdown menus that should line up with the left edge of their button and grow to the right.

You can also use logical keywords such as block-start, block-end, inline-start and inline-end if your interface needs to work in right-to-left or vertical writing modes.

When you use position-area, the browser also sets the element's default alignment so it sits snugly against the anchor. Add a margin to create a gap:


_10
.tooltip {
_10
position: absolute;
_10
position-anchor: --save-button;
_10
position-area: top;
_10
margin-bottom: 8px;
_10
}

Precise placement with anchor()

#

position-area covers most needs, but sometimes you want exact control. The anchor() function returns the position of one of the anchor's edges, and you use it inside the inset properties: top, right, bottom and left.


_10
.tooltip {
_10
position: absolute;
_10
position-anchor: --save-button;
_10
_10
/* Tooltip's bottom edge sits on the anchor's top edge */
_10
bottom: anchor(top);
_10
_10
/* Tooltip's left edge lines up with the anchor's centre */
_10
left: anchor(center);
_10
}

Read bottom: anchor(top) as "put my bottom edge at the anchor's top edge". The function accepts top, right, bottom, left, center, the logical keywords start and end, and percentages along the anchor's size.

You can name the anchor directly inside the function, which lets one element reference more than one anchor:


_10
.connector {
_10
position: absolute;
_10
left: anchor(--box-a right);
_10
right: anchor(--box-b left);
_10
top: anchor(--box-a center);
_10
}

Because anchor() returns a length, it works inside calc() too:


_10
.tooltip {
_10
bottom: calc(anchor(top) + 8px);
_10
}

There is a companion function, anchor-size(), that returns the anchor's width or height. It is useful when a dropdown should be at least as wide as the button that opens it:


_10
.menu {
_10
min-width: anchor-size(width);
_10
}

Centring on the anchor

Centring a tooltip horizontally over its anchor used to need a translate trick. With anchor positioning you can use the anchor-center value for justify-self or align-self:


_10
.tooltip {
_10
position: absolute;
_10
position-anchor: --save-button;
_10
bottom: anchor(top);
_10
justify-self: anchor-center;
_10
}

Handling the edge of the screen with position-try

#

This is where anchor positioning saves the most code. A tooltip above a button near the top of the viewport will be cut off. Normally you would detect that in JavaScript and flip it below. In CSS you list fallbacks with position-try-fallbacks, and the browser tries each one in order until it finds one that fits.

The simplest fallbacks are the built-in flip keywords:


_10
.tooltip {
_10
position: absolute;
_10
position-anchor: --save-button;
_10
position-area: top;
_10
position-try-fallbacks: flip-block;
_10
}

flip-block mirrors the position across the block axis, so top becomes bottom. flip-inline mirrors left and right. You can combine them, and list several options separated by commas:


_10
.menu {
_10
position-area: bottom span-right;
_10
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
_10
}

You can also pass position-area values directly as fallbacks:


_10
.tooltip {
_10
position-area: top;
_10
position-try-fallbacks: bottom, right, left;
_10
}

Custom fallbacks with @position-try

When a flip is not enough, define your own named fallback with the @position-try at-rule. Inside it you can set inset properties, margins, sizes and position-area.


_17
@position-try --tooltip-right {
_17
position-area: right;
_17
margin: 0 0 0 8px;
_17
}
_17
_17
@position-try --tooltip-below {
_17
position-area: bottom;
_17
margin: 8px 0 0 0;
_17
}
_17
_17
.tooltip {
_17
position: absolute;
_17
position-anchor: --save-button;
_17
position-area: top;
_17
margin-bottom: 8px;
_17
position-try-fallbacks: --tooltip-below, --tooltip-right;
_17
}

Notice that each fallback resets the margin. A fallback replaces the matching properties, so if the base style has margin-bottom: 8px and the fallback flips the tooltip below, you want the gap on the other side.

Choosing the best fallback, not just the first

By default the browser uses the first option that fits. position-try-order changes that so it picks the option with the most space:


_10
.menu {
_10
position-try-fallbacks: flip-block;
_10
position-try-order: most-height;
_10
}

This is useful for long menus, where you would rather open in whichever direction shows the most items.

Hiding when the anchor scrolls away

If the anchor scrolls out of view, a tooltip stuck to it can look odd. position-visibility lets the browser hide it:


_10
.tooltip {
_10
position-visibility: anchors-visible;
_10
}

Pairing with the popover attribute

#

Anchor positioning handles where something goes. The popover attribute handles whether it is shown. Together they give you an accessible, dismissible menu with no JavaScript.


_10
<button popovertarget="account-menu" class="menu-button">
_10
Account
_10
</button>
_10
_10
<div id="account-menu" popover class="menu">
_10
<a href="/profile">Profile</a>
_10
<a href="/settings">Settings</a>
_10
<a href="/sign-out">Sign out</a>
_10
</div>

The popovertarget attribute wires the button to the popover. Clicking the button toggles it, pressing Escape closes it, and clicking outside closes it too (that is "light dismiss", the default for popover="auto"). The popover is rendered in the top layer, so it sits above everything else without z-index battles and is not clipped by a parent with overflow: hidden.

Now position it:


_16
.menu-button {
_16
anchor-name: --account-menu;
_16
}
_16
_16
.menu {
_16
position-anchor: --account-menu;
_16
position-area: bottom span-right;
_16
position-try-fallbacks: flip-block, flip-inline;
_16
_16
/* Reset the default popover styles */
_16
margin: 0;
_16
inset: auto;
_16
_16
min-width: anchor-size(width);
_16
margin-top: 4px;
_16
}

The reset matters. Browsers give popovers default styles that centre them in the viewport, including inset: 0 and margin: auto. If you leave those in place, they fight your anchor styles and the popover ends up in the middle of the screen.

The HTML spec also says that a popover opened by a popovertarget button gets that button as an implicit anchor, so in supporting browsers you may not need anchor-name at all. Setting the names explicitly is clearer, works the same everywhere, and costs one line.

A hover tooltip with interest in mind

The example above opens on click. For a simple hover or focus tooltip, you can use a sibling selector and avoid the popover attribute entirely:


_19
.has-tooltip {
_19
anchor-name: --tip;
_19
}
_19
_19
.tip {
_19
position: absolute;
_19
position-anchor: --tip;
_19
position-area: top;
_19
position-try-fallbacks: flip-block;
_19
margin-bottom: 6px;
_19
opacity: 0;
_19
pointer-events: none;
_19
transition: opacity 150ms;
_19
}
_19
_19
.has-tooltip:hover + .tip,
_19
.has-tooltip:focus-visible + .tip {
_19
opacity: 1;
_19
}

Remember to connect the tooltip to its trigger for assistive technology, for example with aria-describedby on the button pointing at the tooltip's id.

Many anchors on one page

#

Anchor names are global by default, so if you render a list of cards, each with its own tooltip, every card claiming --tip causes a clash. The browser uses the last matching anchor in the document, which is almost never what you want.

There are two tidy ways round this.

Give each pair a unique name with inline styles. This works well when components are rendered from data:


_10
<button style="anchor-name: --tip-42">Info</button>
_10
<div class="tip" style="position-anchor: --tip-42">Details for item 42</div>

Or scope the name with anchor-scope, so each card has its own private --tip:


_11
.card {
_11
anchor-scope: --tip;
_11
}
_11
_11
.card .info-button {
_11
anchor-name: --tip;
_11
}
_11
_11
.card .tip {
_11
position-anchor: --tip;
_11
}

anchor-scope limits the name's visibility to that element's subtree, which keeps reusable components self-contained.

A complete example

#

Here is a full page you can paste into a file and open in a browser. It has a button near the top-right corner, so you can see the fallbacks kick in.


_46
<!doctype html>
_46
<html lang="en">
_46
<head>
_46
<meta charset="utf-8">
_46
<title>Anchor positioning demo</title>
_46
<style>
_46
body {
_46
font-family: system-ui, sans-serif;
_46
display: flex;
_46
justify-content: flex-end;
_46
padding: 16px;
_46
}
_46
_46
.menu-button {
_46
anchor-name: --menu;
_46
padding: 8px 16px;
_46
}
_46
_46
.menu {
_46
position-anchor: --menu;
_46
position-area: bottom span-right;
_46
position-try-fallbacks: flip-inline, flip-block;
_46
margin: 0;
_46
inset: auto;
_46
margin-top: 4px;
_46
min-width: anchor-size(width);
_46
padding: 8px;
_46
border: 1px solid #ccc;
_46
border-radius: 8px;
_46
}
_46
_46
.menu a {
_46
display: block;
_46
padding: 6px 8px;
_46
}
_46
</style>
_46
</head>
_46
<body>
_46
<button class="menu-button" popovertarget="menu">Menu</button>
_46
<nav id="menu" class="menu" popover>
_46
<a href="#">Profile</a>
_46
<a href="#">Settings</a>
_46
<a href="#">Sign out</a>
_46
</nav>
_46
</body>
_46
</html>

Because the button is on the right edge, spanning right would overflow, so the browser applies flip-inline and the menu grows to the left instead.

Browser support

#

At the time of writing (late 2026), anchor positioning works in all three major engines:

  • Chrome and Edge have supported it since version 125, released in 2024.
  • Safari added support in Safari 26, in September 2025.
  • Firefox enabled it by default in Firefox 147, in January 2026.

A few details to watch:

  • Some property names changed during development. position-area was once called inset-area, and position-try-fallbacks was once position-try-options. Older blog posts and demos may use the old names, which current browsers ignore.
  • Newer pieces such as anchor-scope and position-visibility arrived later than the core features, so check support for those individually.
  • Users on older browsers, or on devices that update slowly, will still be around for a while.

For older browsers, the safest approach is progressive enhancement. Give the element a sensible default position, then layer anchor positioning on top with @supports:


_16
.menu {
_16
/* Fallback: fixed near the top of the screen */
_16
position: fixed;
_16
top: 64px;
_16
right: 16px;
_16
}
_16
_16
@supports (anchor-name: --a) {
_16
.menu {
_16
top: auto;
_16
right: auto;
_16
position-anchor: --menu;
_16
position-area: bottom span-right;
_16
position-try-fallbacks: flip-block, flip-inline;
_16
}
_16
}

There is also a polyfill maintained by OddBird, @oddbird/css-anchor-positioning, if you need closer parity in older browsers. Check the current caniuse and MDN tables before you decide whether you need it.

When not to use it

#

Anchor positioning is the right default for most tethered UI, but there are cases where it is not the best fit:

  • Complex collision logic. If you need behaviour such as shifting along the edge by a few pixels while keeping an arrow pointed at the anchor, or adjusting based on the content of other elements, a JavaScript library like Floating UI still gives you more control.
  • Layouts that are not really "attached". If something just needs to sit in a corner of a card, ordinary absolute positioning inside a position: relative parent is simpler.
  • When you must support old browsers exactly. If a layout must look identical in browsers without support, and a fallback position is not acceptable, you will need a polyfill or a script.
  • Accessibility does not come for free. Anchor positioning is purely visual. It does not add roles, focus management or keyboard handling. The popover attribute helps with some of that, but you still need sensible markup and ARIA where appropriate.

Wrapping up

#
  • Name an anchor with anchor-name and connect a positioned element to it with position-anchor.
  • Use position-area for quick placement on a three by three grid, and anchor() inside inset properties when you need exact edges.
  • Add position-try-fallbacks with flip-block, flip-inline or your own @position-try rules so elements stay on screen.
  • Pair anchor positioning with the popover attribute for accessible, light-dismiss menus, and remember to reset the default popover inset and margin.
  • Support is now in Chrome, Safari and Firefox, so treat it as a progressive enhancement with a simple fallback for older browsers.
#
file under:
CSS

Was this article helpful?

About the author

Danny Engineering

A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.