Full answer
Before anchor positioning, placing a box next to another box meant a wrapper with position: relative, or JavaScript that measured the button and set top and left. Both break when the button moves, or when the box would run off the screen.
CSS anchor positioning connects two elements in CSS.
- Name the anchor:
anchor-name: --infoon the button. - Link the positioned element:
position-anchor: --info. It must be absolutely or fixed positioned. - Place it with
anchor()in inset properties, for exampletop: anchor(bottom)andleft: anchor(left), or with the shorterposition-area. - Optionally add
position-try-fallbacksso the browser flips the box to another side if it would overflow the screen.
Anchor positioning is a newer feature. Check the support table. Write a fallback first and then override it inside @supports (anchor-name: --a). The fallback can be simple, such as a box placed under a position: relative wrapper with fixed offsets.
It pairs well with the popover attribute. A popover is in the top layer, so it escapes overflow: hidden, and anchoring places it near the invoker.
In a browser with anchor positioning, the tip opens just under the Info button, left edges aligned, and it moves to the top of the button if there is no space below. Elsewhere, the fallback rules apply, which may place the box less precisely. Test the fallback by temporarily removing the @supports block.
Keep the feature as an enhancement. The page should still work if the box is only roughly placed.
<style>
body { font-family: sans-serif; }
.wrap { position: relative; display: inline-block; margin: 40px; }
#tip {
position: absolute; inset: auto; top: 100%; left: 0;
margin: 4px 0 0; padding: 6px 8px; border: 1px solid #999; background: #ffd;
}
@supports (anchor-name: --a) {
#info { anchor-name: --info; }
#tip {
position: fixed;
position-anchor: --info;
top: anchor(bottom);
left: anchor(left);
position-try-fallbacks: flip-block;
margin: 4px 0 0;
}
}
</style>
<div class="wrap">
<button id="info" popovertarget="tip" type="button">Info</button>
<div id="tip" popover>Fees are paid every month.</div>
</div>Checked in Chrome 156 only, where anchor positioning is available. The fallback rules were not run in a browser without it. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- anchor-name defines the anchor; position-anchor links to it.
- anchor() in top, left and similar properties places the box.
- position-try-fallbacks flips the box when space runs out.
- Wrap it in @supports with a plain fallback.
- It pairs well with popover, which uses the top layer.
Common mistakes
- Using anchor positioning with no fallback or support check.
- Forgetting that the positioned element must be absolute or fixed.
- Using one anchor-name for many buttons on the page without thinking about which one the tooltip links to.
Follow-ups
- Why is a popover a good partner for anchor positioning?
- What does position-try-fallbacks do?
Tips
- Describe the three steps: name, link, place.
- Mention position-try-fallbacks as the answer to overflow near the viewport edge.