- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Anchor Positioning
CSS Layout
Anchor Positioning
Position tooltips, menus, and callouts relative to an anchor while providing fallback placements for constrained space. The chapter covers anchor association, relative insets, position areas, sizing, fallbacks, and progressive enhancement.
Anchor positioning ties a tooltip or menu to the button that opens it, in pure CSS. The browser keeps them together, and can flip the tooltip to the other side when it would run off the screen. That used to need a JavaScript library.
It works with absolutely or fixed positioned elements, so it builds on CSS Position. Placement uses logical words like block-start and inline-end, explained in Logical Properties.
Connect the tooltip to its button
Example
css
.trigger {
anchor-name: --trigger;
}
.tooltip {
position: absolute;
position-anchor: --trigger;
position-area: block-start center;
}The button gets a name with anchor-name (it must start with two dashes). The tooltip says which anchor it belongs to with position-anchor, and position-area: block-start center places it centered above the button.
Measure from the anchor's edges
Example
css
.menu {
position: fixed;
position-anchor: --trigger;
inset-block-start: calc(anchor(block-end) + 0.5rem);
inset-inline-start: anchor(inline-start);
min-width: anchor-size(width);
}For finer control, anchor() gives you the position of one edge of the anchor. Here the menu starts 0.5rem below the button's bottom edge and lines up with its left edge. anchor-size() does the same for width and height, so a dropdown can be exactly as wide as its button.
When it would not fit
A tooltip above a button at the very top of the screen would be cut off and overflow. Fallbacks fix that:
Example
css
.tooltip {
position-area: block-end center;
position-try-fallbacks:
flip-block,
flip-inline,
flip-block flip-inline;
}
@position-try --above {
position-area: block-start center;
}
.menu {
position-try-fallbacks: --above, flip-inline;
position-visibility: anchors-visible;
}The browser tries the preferred position first, then flips to the other side, then to the other edge, and uses the first one that fits. @position-try lets you define your own alternative positions when flipping is not enough.
Anchor Positioning Reference
| Syntax | Purpose | Example |
|---|---|---|
| anchor-name | Names the reference element. | anchor-name: --trigger |
| position-anchor | Associates a positioned element. | position-anchor: --trigger |
| position-area | Places the element in an anchor-relative region. | position-area: block-end |
| anchor() | Returns an anchor-relative inset. | top: anchor(bottom) |
| position-try-fallbacks | Lists alternative placements when the preferred position overflows. | position-try-fallbacks: flip-block |
| @position-try | Defines a named custom fallback placement. | @position-try --above { ... } |
| position-visibility | Conditionally hides unusable anchored content. | position-visibility: anchors-visible |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Anchor Positioning example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.trigger {
anchor-name: --trigger;
}
.tooltip {
position: absolute;
position-anchor: --trigger;
position-area: block-start center;
}
.menu {
position: fixed;
position-anchor: --trigger;
inset-block-start: calc(anchor(block-end) + 0.5rem);
inset-inline-start: anchor(inline-start);
min-width: anchor-size(width);
}
.tooltip {
position-area: block-end center;
position-try-fallbacks:
flip-block,
flip-inline,
flip-block flip-inline;
}
@position-try --above {
position-area: block-start center;
}
.menu {
position-try-fallbacks: --above, flip-inline;
position-visibility: anchors-visible;
}
</style>
</head>
<body>
<main><button class="trigger">Account</button><aside class="tooltip menu">Account settings</aside></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| anchor-name | 125 | 125 | 147 | 26 |
| position-area | 129 | 129 | 147 | 26 |
| @position-try | 125 | 125 | 147 | 26 |
| position-try-fallbacks | 128 | 128 | 147 | 26 |
| position-visibility | 125 | 125 | 147 | 26.2 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Make it optional
Support is still new in some browsers. Write the tooltip so it is still usable without anchor positioning, for example shown below its button in normal flow, and add the anchored version inside @supports (anchor-name: --a). And keep the tooltip right after its button in the HTML, so keyboard and screen reader users meet them together.
Anchor Positioning exercises
Name the anchor
Give the button the anchor name --help.
The HTML your code works on
<style>
.stage { position: relative; height: 200px; }
</style>
<div class="stage">
<button class="btn">Help</button>
<div class="tip">Click to ask a question.</div>
</div>Result
