On this page10 sections
Lecture 048: Anchor Positioning
| Category | 7: Advanced Styling |
| Lecture | 048 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · Chrome, Edge, Safari 26+ |
| Level | Advanced |
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-areaor theanchor()function - Flip it automatically with
position-try-fallbacks
1. Name the Anchor, Attach the Element
Example
<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-namegives the anchor a name, which must start with--.- The attached element must be
position: absolute(orfixed), withposition-anchorset 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.
top left | top | top right
left | [anchor] | right
bottom left | bottom | bottom rightExample: change bottom to right, top left, and so on:
<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:
.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:
<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.
<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
- Which property names an element as an anchor?
- Which property attaches an element to that anchor?
- Place an element directly to the right of its anchor.
- Write CSS that sets an element's top edge to the anchor's bottom edge.
- Which property flips a menu above the button when there's no room below?
Practice Answers
anchor-name(e.g.anchor-name: --btn;)position-anchorposition-area: right;top: anchor(bottom);position-try-fallbacks: flip-block;
Key Takeaways
anchor-name: --xon the anchor;position: absolute; position-anchor: --xon the attached element.position-areaplaces it on a 3×3 grid;anchor()sets exact edges.position-try-fallbacksflips it when space runs out.- A great match for popovers, tooltips and menus.