← Back to CSS
40

Anchor Positioning: attach overlays to an element without JS geometry

Learn how to use `anchor-name`, `position-anchor` and `anchor()` to place tooltips, popovers and contextual UI around another element with much less manual glue.

📘 Theory

The core idea: declare an anchor and consume it

Think less in absolute pixels and more in relationships between elements.

With `anchor-name` you name the reference element. With `position-anchor` you tell the overlay which anchor to follow. And with functions like `anchor(bottom)` or `anchor(center)` you request coordinates derived from that relationship.

This is especially useful for tooltips, form help, contextual popovers and menus that should keep following a trigger even when the layout changes.

Minimal tooltip pattern

The classic example is enough to internalize the syntax.

Where it helps more than relative and absolute

It does not replace every positioning technique, but it does reduce the fragile ones.

1

Tooltips and help UI

The visual relationship already exists: the content should appear next to the element that invoked it.

2

Small popovers

Filter states, contextual information or quick confirmations get cleaner without manual coordinate math.

3

Reusable components

The positioning logic depends less on the surrounding page and lives more naturally inside the component itself.

Fallback and progressive enhancement

The important lesson here is not “use the new thing”, but “do not tie the UI to perfect support”.

1

If the overlay is secondary, you can leave it without the enhancement or show it in a simpler default position. If it is critical, prepare a fallback with `relative + absolute` or a small JavaScript measurement step.

2

The professional move is not to duplicate the whole interface out of excitement. Solve the robust version first, then let modern browsers simplify the path.

🧪 Learn by doing

Example Interactive demo: native tooltip and contextual popover See how the overlay follows the trigger with native support and how the approach changes when the browser needs a fallback.

🏁 Challenges

Challenge Challenge: attach a tooltip to the right button Declare an anchor name, use it in the tooltip and place the tooltip below the trigger.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com