Sub topic 11
Reusing anchor names with anchor-scope
This chapter assumes you know how anchor-name and position-anchor link an element to an anchor, and that you are comfortable with position: absolute and containing blocks. Here you will learn why repeated anchor names cause trouble and how anchor-scope fixes it.
The problem: one name, many anchors
Imagine a list of items, each with a trigger and a tooltip. You might write this:
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
}Every .trigger now declares the same name, --tip. By default, an anchored element that refers to a name that several elements share picks the last one in document order that it can see. So every tooltip in the list would attach to the final trigger. The tooltips pile up in one place instead of sitting above their own triggers.
You could give each trigger a unique name such as --tip-1 and --tip-2. That means inline styles or generated CSS. anchor-scope lets you keep one name and still get the right match.
What anchor-scope does
anchor-scope is set on an ancestor element. It limits a named anchor so that only descendants of that element can see it. Each scoped ancestor gets its own private version of the name.
.item {
anchor-scope: --tip;
}
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
}<ul>
<li class="item">
<button class="trigger">First</button>
<span class="tooltip">Tip for first</span>
</li>
<li class="item">
<button class="trigger">Second</button>
<span class="tooltip">Tip for second</span>
</li>
</ul>Each .item creates a scope for --tip. Inside the first item, the tooltip can only see the first trigger. Inside the second, only the second. The name is the same, but the lookup never crosses an item boundary.
Syntax
anchor-scope accepts these values:
| Value | Meaning |
|---|---|
| `none` | No scoping. This is the default. |
| `--name` | Scope the listed name or names. Use a comma-separated list for several. |
| `all` | Scope every anchor name declared inside this element. |
Using all is convenient for a reusable component. Everything inside stays self-contained, and you do not need to list names.
.card {
anchor-scope: all;
}How scoping works
When an element is scoped for a name, two things happen:
- Anchors with that name inside the scope are visible only to elements inside the same scope.
- Anchors with that name outside the scope are hidden from elements inside it.
So scoping works in both directions. A tooltip inside a scoped item will not accidentally attach to a same-named anchor elsewhere on the page. The scoping element itself counts as inside the scope, so it can also be the anchor.
Common errors
- Scope set on the wrong element. If
anchor-scopesits on the trigger itself, the tooltip is a sibling and falls outside the scope. Put it on a shared ancestor. - Scope too narrow. If the trigger and tooltip are not both descendants of the scoped element, the tooltip finds no anchor, and the element is not positioned relative to an anchor.
- Name mismatch. The name in
anchor-scopemust match the name inanchor-nameexactly, including the leading double dash. - Scope too wide. Scoping on
bodymeans the whole page shares one scope and the original problem comes back.
When to use unique names instead
Scoping is the right choice when many copies of the same structure repeat. If a single tooltip must refer to an anchor in a different part of the page, a scope would hide that anchor. In that case, use a unique name and leave anchor-scope off.
Exercise
A page has three .card elements. Each contains a button with anchor-name: --menu and a div with position-anchor: --menu. All three menus appear next to the last button. Which single declaration fixes this, and on which element?
Show answer
Add anchor-scope: --menu to .card (or anchor-scope: all). The scope then sits on a common ancestor of each button and menu, so each menu can only see the button inside its own card.
Exercise
You set anchor-scope: --tip on a .trigger button. The tooltip is a sibling of the button, and it still does not find the anchor. Why?
Show answer
The scope applies to the element and its descendants. The sibling tooltip is outside the button, so it cannot see the scoped name. Move anchor-scope to the common parent of the button and the tooltip.