Anchor positioning
Definition and purpose
CSS anchor positioning links a positioned box to another element’s geometry. Name an anchor with anchor-name, associate the floating box with position-anchor, and use anchor() in insets or position-area to select placement.
Absolute fallback first
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b { position: relative; width: 300px; height: 180px; } #a { position: absolute; top: 40px; left: 60px; width: 80px; height: 30px; } #t { position: absolute; top: 78px; left: 60px; background: navy; color: white; padding: 4px; } </style> </head> <body> <div id="b"> <button id="a" aria-describedby="t">Help</button> <span id="t">Helpful note</span> </div> </body></html>The note is 8px below the button for these fixed dimensions.
Syntax and mental model
The anchor must be acceptable for the positioned element under the anchor positioning rules. A dashed identifier such as --help names it. anchor(bottom) resolves to the bottom edge in the relevant containing-block coordinate system. In this controlled example the button starts at y=40 with height 30; top: calc(anchor(bottom) + 8px) resolves to 78px. A fixed absolute fallback comes first; @supports can enable enhancement, while an anchor() fallback argument addresses unresolved anchors in supporting engines. position-try-fallbacks can request alternate placements (for example flip-block with a compatible placement); test collision behavior rather than assuming a guaranteed fit. This persistent note is not a complete tooltip interaction pattern.
Enhance with an anchor
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b { position: relative; width: 300px; height: 180px; } #a { position: absolute; top: 40px; left: 60px; width: 80px; height: 30px; } #t { position: absolute; top: 78px; left: 60px; background: navy; color: white; padding: 4px; } @supports (top: anchor(bottom)) { #a { anchor-name: --help; top: 55px; } #t { position-anchor: --help; top: calc(anchor(bottom) + 8px); left: anchor(left); } } </style> </head> <body> <div id="b"> <button id="a" aria-describedby="t">Help</button> <span id="t">Helpful note</span> </div> </body></html>In a supporting browser moving the button down 15px moves the note down 15px. The original absolute placement remains for unsupported syntax.
Common mistakes
Do not treat @supports as proof that the intended anchor exists. Provide usable fallback placement; avoid hiding essential instructions on collision. An anchored box is not automatically a popover or a top-layer element.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Overscroll and scroll anchoring. Next lesson: Scroll and view timelines.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Anchor positioning — knowledge check
Five questions on anchor positioning.
Which property names the anchor?
Which property associates the positioned note?
Anchor top 55px, height 30px, gap 8px. Note top?
What does @supports verify?
Which feature can request alternate placement on overflow?