Sub topic 11
Reusing anchor names with anchor-scope
This chapter assumes you know how position: absolute and inset properties work, how anchor-name and position-anchor connect an element to an anchor, and what anchor() and position-area do. You don't need the Popover API here.
The problem: one name, many anchors
An anchor name is a dashed identifier such as --tip. Nothing forces it to be unique. Suppose a page has a list of ten items, and each item has a trigger and a tooltip. The natural CSS is this:
.item .trigger {
anchor-name: --tip;
}
.item .tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
}Ten elements now share the name --tip. When a tooltip looks up --tip, the browser has to pick one of those ten anchors. By default it does not pick the trigger in the same item. The rules are roughly:
- The anchor must be a valid anchor for the positioned element. It has to come before the element in tree order, or be in a different containing block chain, and it can't be a descendant of the positioned element.
- Among the valid candidates, the browser picks the last one in tree order that comes before the positioned element.
For a list where each tooltip comes right after its trigger, this often gives the right answer by accident. It breaks when the markup changes. If all tooltips are placed at the end of the list, or in a container outside it, every tooltip attaches to the last trigger. Reordering, conditional rendering, and components that render in a different place can all cause this.
A second, subtler failure appears when you reuse a component. A card component declares anchor-name: --card-action on its button. Two cards on the same page produce two anchors with the same name. Each card's popup might resolve to the wrong card's button.
Before anchor-scope existed, the workarounds were generating a unique name per item, for example with inline styles such as style="anchor-name: --tip-3", or using JavaScript to assign names. Both put per-instance bookkeeping back into your markup.
What anchor-scope does
anchor-scope limits how far an anchor name is visible. You set it on an ancestor element. Names listed in the property can only be matched by positioned elements inside that ancestor's subtree, and anchors inside the subtree can only be seen from inside it.
.item {
anchor-scope: --tip;
}With this rule, each .item becomes its own small namespace for --tip. A tooltip inside item 3 can only see anchors named --tip inside item 3. Anchors in items 1, 2, and 4 are invisible to it, even though they share the name.
The full example:
<ul class="items">
<li class="item">
<button class="trigger">First</button>
<div class="tooltip">Tip for the first item</div>
</li>
<li class="item">
<button class="trigger">Second</button>
<div class="tooltip">Tip for the second item</div>
</li>
<li class="item">
<button class="trigger">Third</button>
<div class="tooltip">Tip for the third item</div>
</li>
</ul>.item {
anchor-scope: --tip;
position: relative;
}
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
}You write the CSS once. The markup carries no per-item names.
The values
anchor-scope accepts these values:
| Value | Meaning |
|---|---|
| `none` | The default. The element does not scope any names. |
| `--name` | Scope the one named anchor. |
| `--a, --b` | Scope several names at once. |
| `all` | Scope every anchor name declared within the subtree. |
The all keyword is convenient for self-contained components. You don't have to list each name, and new names added later are scoped automatically.
.card {
anchor-scope: all;
}How the scoping works internally
Scoping changes the lookup, not the names themselves. The model is:
- An element with
anchor-scope: --tipcreates a boundary for--tip. - For a positioned element inside the boundary, the browser only considers anchors named
--tipthat are also inside the same boundary. - For a positioned element outside the boundary, anchors inside the boundary are hidden. The lookup continues as if they did not exist.
This has a consequence that surprises people. The scope element itself can act as the boundary, and the scope applies to anchors and positioned elements that are descendants of it. An anchor declared on the scope element itself is treated as inside the scope. That is useful when the container is the anchor:
.badge {
anchor-name: --badge;
anchor-scope: --badge;
}
.badge .label {
position: absolute;
position-anchor: --badge;
position-area: top right;
}The scope also nests. If an inner element scopes the same name, a positioned element resolves to the nearest enclosing scope's anchors.
flowchart TDThe diagram below shows the lookup for two items.
Scope versus the top layer and containing blocks
Anchor lookup is about the DOM tree and the scope boundary. It is separate from where the element is painted. A popover in the top layer is still a DOM descendant of its parent, so a scope on an ancestor of both the button and the popover still applies. Place the popover inside the scoped container in your markup, not at the end of body, if you want to use anchor-scope.
<div class="menu-wrap">
<button class="menu-btn" popovertarget="m1">Menu</button>
<div id="m1" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>
</div>.menu-wrap {
anchor-scope: --menu-btn;
}
.menu-btn {
anchor-name: --menu-btn;
}
[popover] {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
}Any number of .menu-wrap blocks can appear on the page. Each popover attaches to its own button.
Common errors
The scope is on the wrong element
The scope element must be an ancestor (or the same element) of both the anchor and the positioned element. If you put anchor-scope on the trigger only, the tooltip is outside the boundary and cannot see the anchor at all. The tooltip then has no valid anchor, and the anchor() function falls back or the element is placed as if no anchor existed.
/* Wrong: the tooltip is a sibling, not a descendant */
.trigger {
anchor-name: --tip;
anchor-scope: --tip;
}Move the scope to the shared parent.
Scoping a name you never declared
anchor-scope: --tip does nothing useful if no element in the subtree uses --tip. A typo such as --tooltip in one place and --tip in another gives silent failure, since an unknown name is just a name. Check the spelling in all three properties: anchor-name, position-anchor, and anchor-scope.
Expecting scope to create the anchor
anchor-scope does not declare an anchor. It only restricts visibility. You still need anchor-name on the anchor element and position-anchor (or an explicit name inside anchor()) on the positioned element.
Scoping a name used by a global element
If a sticky header declares anchor-name: --header and many components position against it, scoping --header on a component container hides the header from that component. Scope only names that are meant to repeat. Keep shared names unscoped.
Mixing `anchor-scope: all` with shared anchors
all hides every name in the subtree from the outside. If a component deliberately exposes an anchor to the rest of the page, list the private names explicitly instead of using all.
When to use anchor-scope
| Situation | Approach |
|---|---|
| One anchor and one positioned element on the page | No scope needed. |
| Repeated components, each with its own anchor | `anchor-scope` on the component root. |
| A component with private anchors and one shared anchor | `anchor-scope` with a name list. |
| Generated content where you can't control the names | `anchor-scope: all` on the container. |
| Anchor and popup are in unrelated parts of the DOM | Unique names. Scope can't help, since the popup would be outside the boundary. |
Support and fallback
anchor-scope is newer than anchor-name and position-anchor, and a browser may support the first set before it supports the scope property. Test for it directly:
@supports (anchor-scope: --a) {
.item {
anchor-scope: --tip;
}
}If the property is missing, the lookup falls back to the default tree-order rule described earlier. Keep the markup in the "anchor, then tooltip" order inside each item so that the default rule gives the right result. Then scoped and unscoped browsers produce the same layout for this common case.
Summary of the model
anchor-namedeclares an anchor.position-anchorselects one by name.- Without scoping, a repeated name resolves by tree order, and the result is fragile.
anchor-scopeon a shared ancestor makes each instance resolve only within itself.- Put the scope on an element that contains both the anchor and the positioned element.
Exercise
A page has three .card elements. Each contains a button with anchor-name: --action and a div.menu with position-anchor: --action. All three menus attach to the last button. Which single rule fixes this, and where must it be applied?
Show answer
Add anchor-scope: --action (or anchor-scope: all) to .card. The scope must be on an element that contains both the button and the menu, which is the card. Each menu then resolves --action only among anchors inside its own card.
Exercise
You wrote .trigger { anchor-name: --tip; anchor-scope: --tip; } and the tooltip, a sibling of the trigger, no longer attaches to it. Why?
Show answer
The scope boundary is the trigger. The tooltip is a sibling, so it is outside the boundary and cannot see anchors inside it. Move anchor-scope: --tip to the common parent of the trigger and the tooltip.