All posts

CSS Anchor Positioning: 2 Practical Use Cases Beyond Tooltips

Pin a badge to an image it isn't nested in and draw chat thread lines with one pseudo-element, using anchor(), anchor-scope, and current browser support.

Two UI cards connected by an indigo anchor line, illustrating CSS anchor positioning

You already know how CSS positioning turns into a pile of magic numbers. You set top: 6px, everything looks great, then a designer tweaks one thing and your layout starts slipping. CSS anchor positioning fixes that by letting you position one element relative to another element directly, even when the DOM structure isn't ideal.

This post walks through two real-world patterns from Kevin Powell's video below: pinning a badge to an image when the badge lives somewhere else in the markup, and drawing thread lines in a chat UI by anchoring a single pseudo-element to two different targets. Both work in every major browser engine as of 2026 (support details are near the end).

Tethering a badge to an image (without changing your markup)

Product card with a NEW badge pinned to the bottom-right corner of the image area, connected by a dashed indigo anchor line
A badge tethered to an image, even when the badge lives elsewhere in the markup.

Picture a common card layout. You have an image at the top, content underneath, and a small badge (a tag or label) sitting in the content area. Then the designer asks for something that sounds simple: move the badge to the bottom-right corner of the image.

If the badge lives inside the content section and not inside an image wrapper, you have a problem. You can't absolutely position it inside the image container because it isn't in that container. Historically, you'd either accept hacky offsets or restructure the HTML.

Anchor positioning gives you a third option: keep your markup as-is and tether the badge to the image anyway.

Why position: absolute turns into magic numbers

The classic setup makes the card the containing block with position: relative, makes the badge position: absolute, and offsets it with top and right. At first it's clean: top: 6px; right: 6px; puts the badge near the top-right.

Moving it to the image's bottom-right corner is where things go sideways. Since the badge is positioned relative to the card, you start guessing the image height:

.badge {
  top: 154px; /* or whatever looks right today */
  right: 6px;
}

That "today" is the trap. The moment the image height changes, your badge is wrong again.

The fallbacks aren't great either. If your images really have a fixed height, you can store it in a custom property and compute offsets, but you're still babysitting numbers. Or you can add an image wrapper and move the badge into it, which sometimes breaks the content structure you actually want.

Anchor positioning lets you keep the HTML that makes sense and still get a layout that responds to real element geometry.

Set up the image as an anchor

First, name the element you want to anchor to. Anchor names are dashed identifiers, so like custom properties they must start with --.

.product-image {
  anchor-name: --product-image;
}

.badge {
  position: absolute;
  position-anchor: --product-image;
}

At this point nothing moves. That's expected: you've only declared the relationship. You still need to tell CSS which edges should line up.

Use anchor() so offsets come from the anchor, not the containing block

This is the part that trips people up. If you write bottom: 6px; right: 6px;, you're still in the old model, and those values are measured from the containing block. To measure from the anchor, use the anchor() function.

To stick the badge to the bottom-right of the image, line the badge's edges up with the image's edges:

.badge {
  bottom: anchor(bottom);
  right: anchor(right);
}

Now the badge snaps to the image, even though it sits elsewhere in the markup. Because position-anchor sets the default anchor, anchor(bottom) doesn't need the name repeated.

You can also anchor to different edges. Setting the badge's bottom to anchor(top) places it above the image. If the image wrapper uses overflow: hidden (common with rounded corners), you might not see the badge there until you disable the overflow while testing.

Fine-tune spacing with calc()

Once the badge is pinned, you usually want a little breathing room so it doesn't sit exactly on the corner. Wrap anchor() in calc() to add a consistent offset:

.badge {
  bottom: calc(anchor(bottom) + 8px); /* 8px up from the image's bottom edge */
  right: calc(anchor(right) + 8px);   /* 8px in from the image's right edge */
}

Inset values push inward from their own side, so adding to bottom moves the badge up and adding to right moves it left. Subtract instead to push the badge past the edge. The optional second argument inside anchor() is a fallback value for when the anchor can't be found, not an offset, so the math belongs in calc().

That's the difference between "place it at pixel 154" and "place it 8px from the image edge." One survives redesigns, the other doesn't.

When everything breaks: repeated anchor names and anchor-scope

There's a gotcha that shows up as soon as you have multiple cards on the page.

If every image uses the same anchor name and nothing limits which image each badge can see, the last one in DOM order wins. Every badge anchors to the final image on the page, and all your badges stack in one spot.

Keeping position: relative on each card usually prevents this, because an anchor generally has to sit inside the positioned element's containing block to count. Remove that position: relative (sometimes you need to) and every image on the page becomes a candidate.

The fix is anchor-scope. Instead of making every anchor name unique, scope the name to a parent element such as the card:

.card {
  anchor-scope: --product-image;
}

Now each card is a boundary. Inside a card, --product-image refers to that card's image only. You can also write anchor-scope: all to scope every anchor name set inside the element.

If anchor positioning starts behaving strangely, it's usually a validity or scoping issue. The best troubleshooting reference is James Stuckey Weber's OddBird article on why anchor positioning isn't working. It walks through the rules (DOM order, containing blocks, pseudo-elements) that can make an anchor look broken when the syntax is fine.

Drawing thread lines by anchoring to multiple elements

Three chat message bubbles connected by a vertical indigo thread line with anchor dots at each bubble
A single pseudo-element anchored to multiple chat message targets.

Tooltips and menus get most of the attention, but anchor positioning gets more interesting when one element needs to relate to two different things at once.

A chat thread is a clean example. A main comment sits on the left, replies are listed underneath and indented to the right, and you want a thread line that ties the comment to each reply. You could build that with extra wrappers or SVG. Or you can draw it with one pseudo-element per reply, anchored to both the comment and the reply.

Start with a pseudo-element you can actually see

Each reply gets an ::after pseudo-element that becomes the thread line.

A pseudo-element doesn't render without a content value, and an empty one has no size until you give it dimensions or position it. While debugging, give it a loud background color and a small height, then switch to the real styling once you know it shows up.

Once it exists, take it out of the flow with position: absolute. If you forget that step, you can stare at correct anchor CSS for a long time wondering why nothing moves.

Name your anchors: one for the comment, one for the reply

You need two anchors: the main comment and each reply.

.comment {
  anchor-name: --comment;
}

.reply {
  anchor-name: --reply;
}

Now the pseudo-element can reference both by name.

Anchor the pseudo-element's top and left to the comment

Anchor the line's top edge to the bottom of the comment, so it starts right beneath the comment bubble, and its left edge to the comment's left edge. When you reference a specific anchor, the name goes inside anchor() next to the side:

.reply::after {
  content: "";
  position: absolute;
  top: anchor(--comment bottom);
  left: anchor(--comment left);
}

You didn't calculate anything or care about heights. You described the relationship.

Notice what you didn't do: you didn't add position: relative to the reply. You want the pseudo-element to reach over to the comment, so you don't trap it in a containing block that excludes the comment.

Anchor the bottom to the reply's center (and drop the height)

The line should end halfway down the reply bubble. anchor() accepts center as a side, which saves you from computing offsets:

.reply::after {
  bottom: anchor(--reply center);
}

With the top and bottom both anchored, remove any fixed height. The element stretches exactly as far as it needs to.

At this stage, every reply's line may connect to the wrong --reply, often the last one, so the lines overlap. It's the same class of problem as the card badges: a reused anchor name with nothing scoping it.

Use anchor-scope so each reply only sees itself

Each reply's pseudo-element should see the shared comment anchor, but only its own reply anchor. Scope the reply name on the reply element itself:

.reply {
  anchor-name: --reply;
  anchor-scope: --reply;
}

Keeping anchor-name and anchor-scope together like this is a handy pattern whenever a name should stay local. Now each line connects to the center of its own reply.

The current draft of the spec also says that when several anchors share a name, the browser should pick the nearest ancestor before falling back to the last one in DOM order. That rule would cover this case on its own, but anchor-scope makes your intent explicit and doesn't depend on which version of the rule a browser implements.

Turn the block into a thread line with borders and radius

Once the positioning is right, swap the filled rectangle for borders so it reads as a line. In the demo, the line starts as a thick border to confirm each reply has its own element, then gets refined:

  1. Slim the border down to the final thickness.
  2. Hide the top and right borders, leaving an L-shape.
  3. Add border-radius: 0 0 0 12px; to soften the bend.

You can also drop the fixed width by anchoring the line's right edge to the reply's left edge:

.reply::after {
  right: anchor(--reply left);
}

Now the line always reaches the reply, even if your layout spacing changes.

Bonus: anchored elements can animate

Because the pseudo-element sits between two real layout points, changes in those points can animate. Kevin Powell builds on this idea in his CSS-only navigation blob video, which moves an anchored highlight between nav links. Test that kind of effect in Firefox: Can I use notes that Firefox doesn't yet animate anchor() values, so the element jumps instead of gliding.

Browser support in 2026

Anchor positioning is Baseline 2026: it works in the current versions of all major browsers, but older versions are still in use, so treat it as progressive enhancement for anything critical.

Browser

anchor() and anchor-name

anchor-scope

Chrome and Edge

125+

131+

Safari (macOS and iOS)

26+

26+

Firefox

147+

147+

Versions come from MDN's compatibility data for anchor() and anchor-scope. Can I use still flags some gaps: Firefox doesn't animate anchor() values, and Safari 26.0 to 26.6 lacks position-anchor: normal and parts of position-visibility. None of that affects the two patterns above, apart from the animation bonus.

Quick reminders that keep anchor positioning sane

If you're going to use anchor positioning in real components, a few details keep you out of trouble:

  • Anchor names start with --, just like custom properties.
  • Use anchor() in inset properties when you want offsets measured from the anchor's edges instead of the containing block's.
  • Name a specific anchor as anchor(--name side), with a space and no extra words.
  • Put spacing in calc(). The second argument of anchor() is a fallback, not an offset.
  • Expect "last one wins" bugs whenever you reuse an anchor name across components, and reach for anchor-scope.

Start with one magic number you can delete

Once you stop thinking of positioning as "offset from the parent" and start thinking in relationships, anchor positioning fills a gap CSS has had for years. You can pin a badge to an image without rewriting your markup, and you can draw thread lines that stay correct as content shifts. Find one hard-coded offset in a component you already have, rebuild it with an anchor, and see what else you can delete.

If you're early in your front-end career, the habits in our tips for new web designers pair well with techniques like this one.