On this page10 sections
  1. Learning Objectives
  2. 1. Name the Anchor, Attach the Element
  3. 2. position-area: a 3×3 Grid
  4. 3. The anchor() Function
  5. 4. Flipping When There's No Room
  6. 5. With Popovers
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 048: Anchor Positioning

Category7: Advanced Styling
Lecture048 of 057
Estimated video length10 min
TargetCSS3 · Chrome, Edge, Safari 26+
LevelAdvanced

Tooltips and menus usually need JavaScript to sit next to the button that opened them, and to flip to the other side when they'd fall off the screen. Anchor positioning does both in CSS: name an anchor, then position another element relative to it.


Learning Objectives

  • Name an element as an anchor with anchor-name
  • Attach another element with position-anchor
  • Place it with position-area or the anchor() function
  • Flip it automatically with position-try-fallbacks

1. Name the Anchor, Attach the Element

Example

html
<style>
  .btn { anchor-name: --menu-btn; padding: 8px 14px; margin: 40px; }
  .tip {
    position: absolute;
    position-anchor: --menu-btn;
    position-area: top;              /* sit above the anchor */
    margin-bottom: 8px;
    background: #212529; color: white; padding: 6px 10px; border-radius: 6px; font: 13px system-ui;
  }
</style>

<button class="btn">I'm the anchor</button>
<div class="tip">I'm attached above it</div>
  • anchor-name gives the anchor a name, which must start with --.
  • The attached element must be position: absolute (or fixed), with position-anchor set to that name.

If your browser doesn't support anchor positioning yet, the tooltip just appears in the normal flow.


2. position-area: a 3×3 Grid

Think of a 3×3 grid around the anchor, with the anchor in the middle cell.

output
top left     | top        | top right
left         |  [anchor]  | right
bottom left  | bottom     | bottom right

Example: change bottom to right, top left, and so on:

html
<style>
  .a { anchor-name: --a; margin: 60px 140px; padding: 10px; background: #d0ebff; display: inline-block; }
  .b { position: absolute; position-anchor: --a; position-area: bottom; margin-top: 6px;
       background: #ffd43b; padding: 6px 10px; border-radius: 6px; }
</style>

<div class="a">Anchor</div>
<div class="b">position-area: bottom</div>

3. The anchor() Function

For exact control, set the edges yourself:

css
.tip {
  position: absolute;
  position-anchor: --menu-btn;
  top: anchor(bottom);        /* my top = the anchor's bottom */
  left: anchor(left);         /* my left = the anchor's left */
}

4. Flipping When There's No Room

Example: the menu flips above the button if there isn't room below:

html
<style>
  .menu-btn { anchor-name: --m; padding: 8px 14px; }
  .menu {
    position: absolute; position-anchor: --m; position-area: bottom span-right;
    position-try-fallbacks: flip-block;      /* try above if below doesn't fit */
    margin: 6px 0; padding: 6px 0; min-width: 150px;
    background: white; border: 1px solid #dee2e6; border-radius: 8px; box-shadow: 0 8px 20px rgb(0 0 0 / 0.12);
  }
  .menu a { display: block; padding: 6px 12px; color: #343a40; text-decoration: none; }
</style>

<div style="height: 200px; display: flex; align-items: flex-end;">
  <button class="menu-btn">Options</button>
</div>
<div class="menu"><a href="#">Edit</a><a href="#">Share</a><a href="#">Delete</a></div>

flip-block tries the opposite side vertically; flip-inline tries the opposite side horizontally.


5. With Popovers

Anchor positioning pairs naturally with the Popover API: the popover handles opening and closing, and the anchor handles placement.

html
<button popovertarget="info" style="anchor-name: --info;">ℹ Info</button>
<div id="info" popover style="position-anchor: --info; position-area: bottom; margin: 6px 0 0; inset: auto;">
  Anchored popover
</div>

Common Beginner Mistakes

1. Names without --. anchor-name: btn is invalid; write --btn.

2. Forgetting position: absolute. The anchored element must be absolutely or fixed positioned.

3. Relying on it for essential layout. Check browser support, and make sure the page still works when it isn't supported.


Practice Questions

  1. Which property names an element as an anchor?
  2. Which property attaches an element to that anchor?
  3. Place an element directly to the right of its anchor.
  4. Write CSS that sets an element's top edge to the anchor's bottom edge.
  5. Which property flips a menu above the button when there's no room below?

Practice Answers

  1. anchor-name (e.g. anchor-name: --btn;)
  2. position-anchor
  3. position-area: right;
  4. top: anchor(bottom);
  5. position-try-fallbacks: flip-block;

Key Takeaways

  • anchor-name: --x on the anchor; position: absolute; position-anchor: --x on the attached element.
  • position-area places it on a 3×3 grid; anchor() sets exact edges.
  • position-try-fallbacks flips it when space runs out.
  • A great match for popovers, tooltips and menus.