Skip to content
Abhishek Sawant

Frontend8 Min Read

An Anchor Is Not A Parent

CSS anchor positioning links a tooltip to its trigger by name instead of by nesting, so you can predict where it lands and what happens when there's no room.

FIG_01 // FRONTEND
Fig. 01 — An Anchor Is Not A Parent

Every tooltip you’ve written in JavaScript has the same shape. Measure the trigger with getBoundingClientRect, do some arithmetic, set top and left, then listen to scroll and resize so the arithmetic stays true.

It works until the trigger lives inside a scrolling panel, at which point the tooltip can trail the trigger by a frame and wobble. CSS anchor positioning moves that whole job into the browser’s layout pass. By the end of this you’ll be able to predict where an anchored element lands, and what it does when there’s no room.

The Parent Was A Workaround

Before JavaScript got involved, the CSS-only tooltip had one trick: wrap the trigger in a position: relative box and place the tooltip inside it with top: 100%.

tooltip-old.css css
.wrap {
  position: relative;
}
.tip {
  position: absolute;
  top: 100%;
  left: 50%;
  translate: -50% 0;
}

That works, and it has a hidden cost. The tooltip has to be a DOM child of the wrapper, so your markup structure is doing visual work. The relationship “this label belongs to that button” and the relationship “this box is positioned against that box” are the same relationship, because nesting is the only way to express either.

Everything that goes wrong follows from it. An ancestor with overflow: hidden clips the tooltip. A parent that creates a stacking context traps it. And nothing in the stylesheet can say “if this runs off the bottom of the screen, go above instead”, because CSS has no way to ask the question.

That last gap is why Floating UI and Popper exist. They measure, because CSS couldn’t.

Anchor positioning replaces nesting with a name. You give the trigger an anchor-name, and you point the positioned element at that name with position-anchor.

tooltip.css css
.trigger {
  anchor-name: --tip;
}

.tip {
  position: fixed; /* or absolute: required */
  position-anchor: --tip;
  top: anchor(bottom);
  left: anchor(center);
  translate: -50% 0;
}

The anchor() function reads an edge of the anchor’s box, so top: anchor(bottom) means “start where the trigger ends”. Nothing about the markup is implied. The tooltip can be a sibling, or the last child of body.

index.html html
<button class="trigger">Save</button>

<!-- a hundred elements later, somewhere else in the tree -->
<div class="tip">Saved to your drafts</div>

Now the failure mode, which will feel familiar. The positioned element must be absolute or fixed. Leave position at its default and every anchor property is inert. There’s no warning, the declarations look valid, and the element sits wherever normal flow put it.

!

Check support before you ship. Chromium has had anchor positioning since version 125 and Safari since 26. Check Firefox yourself, and check the newer pieces like anchor-scope separately from the basics. Wrap the layout in @supports (anchor-name: —a) and keep your old placement as the fallback.

Position Area Is A Grid

Writing top: anchor(bottom) and a translate for every placement gets old quickly. There’s a shorter way, and it comes with a mental model worth having.

Picture the anchor’s box extended outward in all four directions until it hits the edges of the positioned element’s containing block. That cuts the space into a three-by-three grid, and the anchor itself is the middle cell. position-area names the cells your element is allowed to occupy.

Fig. 02 — The anchor is the pink middle cell. position-area: top fills the yellow one.

The outer cells run out to the containing block, so a cell is as big as the room really is. That’s what makes this more than a shorthand: the element is sized and aligned against the space that exists, not against a point.

ValueWhere the element lands
topAbove the anchor, centred on it
bottomBelow the anchor, centred on it
rightBeside it, vertically centred
top leftThe corner cell, diagonally off the anchor

The tooltip from earlier collapses to this:

tooltip.css css
.tip {
  position: fixed;
  position-anchor: --tip;
  position-area: top;
  margin-block: 0.5rem; /* the gap lives in margin, not in a magic offset */
}

Reach for anchor() when a cell edge isn’t what you want. anchor-size() is the other half of that toolkit: a dropdown that’s at least as wide as its button is min-width: anchor-size(width).

Flipping Is Declared, Not Computed

Here’s the part JavaScript libraries earn their keep on. The tooltip is above the trigger, the trigger is at the top of the screen, and the tooltip is now off the page.

With anchor positioning you describe the alternatives and the browser does the checking. It lays the element out at its primary position, and if that overflows the containing block, it walks your list.

tooltip.css css
@position-try --beside {
  position-area: right;
}

.tip {
  position: fixed;
  position-anchor: --tip;
  position-area: top;
  position-try-fallbacks: flip-block, --beside;
}

flip-block mirrors the placement across the anchor, so top becomes bottom. --beside is a custom fallback you define in an @position-try block. Because the browser does this during layout, there’s no frame where the tooltip is in the wrong place, and no scroll listener to throttle.

Script

You measure, compute, check the viewport, and re-run it on every scroll and resize. The answer is always one event behind.

Stylesheet

You list the placements you’d accept, in order. The browser picks the first that fits, as part of layout.

A position is a preference, not a promise.

Internal_Style_Doctrine // §4.1

That’s the real shift. You stop writing coordinates and start writing an ordered list of places you’d be happy to sit.

Where It Still Breaks

Three things catch people, and two of them are silent.

Names are not unique. anchor-name isn’t an id. If a list of cards each carries anchor-name: --card and their tooltips live in a separate layer after all of them, every tooltip resolves to the same name, and in practice the last matching anchor wins. You get a page where every tooltip flies to the bottom card.

The fix is anchor-scope, which limits a name to one subtree, so each row resolves its own.

rows.css css
.row {
  anchor-scope: --card;
}
.row .trigger {
  anchor-name: --card;
}
.row .tip {
  position-anchor: --card;
  position-area: right;
}

Anchoring doesn’t change the containing block. An absolutely positioned tooltip is still clipped by an overflow: hidden ancestor, however well it’s anchored. The anchor changes what the element measures against, not where it’s allowed to paint. Fixed positioning escapes most clipping, but an ancestor with a transform takes it back.

The sturdier exit is the popover attribute, which promotes the element to the top layer, above everything and outside any clipping ancestor.

menu.html html
<button class="trigger" popovertarget="menu">Options</button>
<div id="menu" popover class="tip">...</div>
menu.css css
.trigger {
  anchor-name: --menu;
}
.tip {
  position-anchor: --menu;
  position-area: bottom;
  inset: auto; /* the popover default of inset: 0 fights you */
  margin: 0;
}

Names can be dropped quietly. A typo in a dashed identifier, or a position-anchor that points at an element that isn’t rendered, doesn’t error. The anchored declarations stop doing anything. When a placement looks wrong, check the names match before you touch anything else.

Three Decisions, In Order

Every anchored element answers the same three questions, and it’s useful to answer them in order.

  1. What am I attached to? An anchor-name on the trigger, a matching position-anchor on the element, and position: absolute or fixed.
  2. Where do I sit? position-area for a cell, anchor() and anchor-size() when you need an exact edge or width.
  3. What if there’s no room? An ordered position-try-fallbacks list, ending somewhere you’re sure fits.

When it fails, the failures map to those questions as well. Nothing moved: question one, and it’s almost always the missing position. Everything went to the last row: question one again, and it’s a name collision. Off the edge of the screen: question three.

On Monday, take your worst tooltip or dropdown, the one with the scroll listener, and delete the listener. Wrap the CSS version in @supports and leave the old path as the fallback until your support table says otherwise.

The old habit was to put things inside what they point at. The better habit is to name what you point at, and let the document stay in whatever order makes sense.

Tagged
  • CSS
  • Layout
  • Browser_APIs

Related Logs

System Updates

Weekly dispatches,
straight to your terminal.