---
title: "CSS Anchor Positioning: tooltips and popovers without JavaScript"
description: "Learn how CSS anchor positioning tethers tooltips, menus and popovers to other elements, flips them when space runs out, and pairs with the popover attribute."
url: https://www.danny.engineering/article/css-anchor-positioning-tooltips-and-popovers-without-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["CSS"]
---

# CSS Anchor Positioning: tooltips and popovers without JavaScript

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.


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


```css
.trigger {
  anchor-name: --save-button;
}

.tooltip {
  position: absolute;
  position-anchor: --save-button;
}
```


`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.


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


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


```css
/* Below the anchor, centred */
position-area: bottom;

/* To the right, vertically centred */
position-area: right;

/* Below the anchor, starting at its left edge and extending right */
position-area: bottom span-right;

/* Top-left corner, outside the anchor */
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:


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


## 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`.


```css
.tooltip {
  position: absolute;
  position-anchor: --save-button;

  /* Tooltip's bottom edge sits on the anchor's top edge */
  bottom: anchor(top);

  /* Tooltip's left edge lines up with the anchor's centre */
  left: anchor(center);
}
```


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:


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


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


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


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:


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


### 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`:


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


## 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:


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


`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:


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


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


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


### 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`.


```css
@position-try --tooltip-right {
  position-area: right;
  margin: 0 0 0 8px;
}

@position-try --tooltip-below {
  position-area: bottom;
  margin: 8px 0 0 0;
}

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


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:


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


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:


```css
.tooltip {
  position-visibility: anchors-visible;
}
```


## 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.


```html
<button popovertarget="account-menu" class="menu-button">
  Account
</button>

<div id="account-menu" popover class="menu">
  <a href="/profile">Profile</a>
  <a href="/settings">Settings</a>
  <a href="/sign-out">Sign out</a>
</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:


```css
.menu-button {
  anchor-name: --account-menu;
}

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

  /* Reset the default popover styles */
  margin: 0;
  inset: auto;

  min-width: anchor-size(width);
  margin-top: 4px;
}
```


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:


```css
.has-tooltip {
  anchor-name: --tip;
}

.tip {
  position: absolute;
  position-anchor: --tip;
  position-area: top;
  position-try-fallbacks: flip-block;
  margin-bottom: 6px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms;
}

.has-tooltip:hover + .tip,
.has-tooltip:focus-visible + .tip {
  opacity: 1;
}
```


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:


```html
<button style="anchor-name: --tip-42">Info</button>
<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`:


```css
.card {
  anchor-scope: --tip;
}

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

.card .tip {
  position-anchor: --tip;
}
```


`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.


```html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Anchor positioning demo</title>
  <style>
    body {
      font-family: system-ui, sans-serif;
      display: flex;
      justify-content: flex-end;
      padding: 16px;
    }

    .menu-button {
      anchor-name: --menu;
      padding: 8px 16px;
    }

    .menu {
      position-anchor: --menu;
      position-area: bottom span-right;
      position-try-fallbacks: flip-inline, flip-block;
      margin: 0;
      inset: auto;
      margin-top: 4px;
      min-width: anchor-size(width);
      padding: 8px;
      border: 1px solid #ccc;
      border-radius: 8px;
    }

    .menu a {
      display: block;
      padding: 6px 8px;
    }
  </style>
</head>
<body>
  <button class="menu-button" popovertarget="menu">Menu</button>
  <nav id="menu" class="menu" popover>
    <a href="#">Profile</a>
    <a href="#">Settings</a>
    <a href="#">Sign out</a>
  </nav>
</body>
</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`:


```css
.menu {
  /* Fallback: fixed near the top of the screen */
  position: fixed;
  top: 64px;
  right: 16px;
}

@supports (anchor-name: --a) {
  .menu {
    top: auto;
    right: auto;
    position-anchor: --menu;
    position-area: bottom span-right;
    position-try-fallbacks: flip-block, flip-inline;
  }
}
```


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.

## Related reading

- [MDN: Using CSS anchor positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_anchor_positioning/Using)
- [MDN: The popover attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/popover)
