Sub topic 2
Declaring an anchor and linking an element to it
This chapter assumes you know the position property (relative, absolute, fixed), inset properties such as top and left, and how containing blocks work. You also need the basics of selectors and the cascade. The previous chapter covered containing blocks, so this one starts from there.
Anchor positioning needs two declarations before any placement can happen. The first marks an element as an anchor and gives it a name. The second tells a positioned element which named anchor to use. Every later feature in this tutorial, including anchor(), position-area and anchor-size(), depends on this link.
The two halves of the link
The link has two properties:
anchor-namegoes on the element you want to attach to. It gives that element a name.position-anchorgoes on the element you want to place. It says which name to use.
The names are dashed identifiers, so they start with two hyphens, as custom properties do. --tip and --menu-btn are valid names. tip is not.
Here is a complete page that sets up the link:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Anchor link</title>
<style>
body { padding: 120px; font: 16px system-ui, sans-serif; }
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
/* Placement comes in the next chapters. */
background: #222;
color: #fff;
padding: 4px 8px;
}
</style>
</head>
<body>
<button class="trigger">Hover target</button>
<div class="tooltip">I am linked to the button</div>
</body>
</html>If you open this page, the tooltip will not appear next to the button. Declaring the link does not move anything. The tooltip still has no inset values, so it sits at its static position. The link only gives the tooltip a reference box. You use that box through anchor() or position-area, which are the subjects of the next two chapters.
What anchor-name accepts
The syntax is anchor-name: none | <dashed-ident>#. The initial value is none, which means the element is not an anchor.
You can give one element several names, separated by commas:
.card-header {
anchor-name: --header, --card-top;
}Different positioned elements can then use different names for the same box. This is useful when two components were written separately and each chose its own name.
Some facts about anchor-name:
- It does not change layout, painting or the containing block. Adding it to an element has no visible effect by itself.
- It is not inherited. Naming a container does not make its children anchors.
- The element must generate a box. An element with
display: noneis not rendered, so nothing can anchor to it. - Transforms on the anchor are accounted for, because the anchored element is placed against the anchor's rendered box.
What position-anchor accepts
The syntax is position-anchor: auto | <anchor-element>, where <anchor-element> is a dashed identifier. The initial value is auto.
An anchored element must be absolutely positioned or fixed (position: absolute or position: fixed). Setting position-anchor on an element in normal flow, or on a position: relative or sticky element, does nothing. This is a common first mistake. If your anchor-based values appear to be ignored, check position first.
position-anchor sets the default anchor for the element. Any anchor(), anchor-size() or position-area declaration that does not name its own anchor uses this default. That is why the examples in this tutorial write bottom: anchor(top) and not bottom: anchor(--tip top). The longer form is available when you need it:
.tooltip {
position: absolute;
position-anchor: --tip;
/* Uses the default anchor, --tip. */
bottom: anchor(top);
/* Names a different anchor explicitly. */
left: anchor(--sidebar right);
}The auto value means the element has no named default anchor. It can still have an implicit one. In Chromium, a popover opened with popovertarget treats its invoking button as an implicit anchor. The popover chapter covers this. Do not rely on implicit anchors in code that must work across browsers. Name the anchor explicitly.
Which elements count as valid anchors
Naming an element is not enough. The browser also checks that the named element is an acceptable anchor for the specific positioned element. If the check fails, the name is treated as if it did not exist for that element, and the declarations that use it fall back to their failure behavior. The main rules are:
- The anchor must be rendered. It cannot be
display: noneor inside skipped content. - The anchor cannot be a descendant of the positioned element. An element cannot be placed against its own content.
- The anchor must be in the positioned element's reach. In practice, the anchor must be inside the positioned element's containing block, or be the containing block's own descendant tree. An anchor elsewhere on the page is only usable if the positioned element's containing block is an ancestor of it too.
- If the anchor is itself absolutely or fixed positioned, it must come before the positioned element in tree order. This rule prevents circular dependencies, where A is placed against B and B against A.
The practical consequence of rule 3 is that you can break the link by moving the positioned element into a different containing block. If a tooltip works in a test page but fails in the real layout, look at which ancestor establishes its containing block and whether the anchor is inside it.
What happens when names collide
Anchor names are not unique IDs. Two elements can share anchor-name: --tip. When a positioned element looks up --tip, the browser picks the last matching element that precedes the positioned element in tree order and is a valid anchor for it.
This matters for repeated components:
<ul class="list">
<li><button class="item">One</button><span class="note">Note for one</span></li>
<li><button class="item">Two</button><span class="note">Note for two</span></li>
<li><button class="item">Three</button><span class="note">Note for three</span></li>
</ul>
<style>
.item { anchor-name: --item; }
.note { position: absolute; position-anchor: --item; }
</style>Every .note here resolves --item to the closest preceding .item in tree order, which happens to be its own sibling button. The example works because of document order. If a .note came before its button, or sat in a separate container, it would pick up the wrong button, or none at all.
Relying on document order is fragile. A later chapter introduces anchor-scope, which limits how far a name is visible, so each repeated component has its own --item. Until then, use unique names per component when the DOM does not guarantee a match.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Element ignores the anchor | `position` is not `absolute` or `fixed` | Set `position: absolute` on the anchored element |
| Element is placed against the wrong anchor | Several elements share the same name | Use unique names, or scope them later with `anchor-scope` |
| Link works in a demo, not in the app | The positioned element's containing block does not contain the anchor | Move the element, or change which ancestor is positioned |
| Name is rejected | Missing `--` prefix | Write `--tip`, not `tip` |
| Nothing resolves | Anchor is `display: none` | Keep the anchor rendered |
| Circular placement | Both elements are positioned and the anchor comes later in the DOM | Reorder the DOM so the anchor comes first |
Debugging the link
When the placement looks wrong, check things in this order:
- Confirm
anchor-nameappears in the computed styles of the anchor element in dev tools. - Confirm the anchored element has
position: absoluteorfixed. - Compare the spelling of the two names, including the leading
--. - Check the containing block of the anchored element.
- Look for duplicate names that could win by tree order.
Writing names that last
Because names are tree-wide by default, pick a convention early. A name that describes the component and role, such as --nav-menu-button, collides less often than --btn. For a component you ship as a library, prefix the names with the component name. Avoid generating names with JavaScript unless the component is rendered many times and anchor-scope is not available to you.
The named link also keeps CSS readable. A rule such as position-anchor: --nav-menu-button says which element the box depends on without a selector path or an ID.
Exercise
A .popup element has anchor-name: --target and position-anchor: --target on the same rule, and position: relative. The author expects it to attach to itself. What is wrong, and what should change?
Show answer
Three things are wrong. An anchored element must be position: absolute or fixed, so position: relative makes position-anchor have no effect. An element also cannot be its own anchor, so the name should go on a different element, such as the trigger button. Finally, no placement property such as anchor() or position-area is declared, so nothing would move anyway. Put anchor-name: --target on the trigger, keep position-anchor: --target on .popup, change .popup to position: absolute, and add a placement declaration.
Exercise
Four cards each contain a button with anchor-name: --action and, after it in the same card, a position: absolute hint with position-anchor: --action. All four cards share one positioned ancestor. Which button does each hint resolve to, and what breaks if you move one hint to the start of its card, before its button?
Show answer
Each hint resolves to the last --action button that comes before it in tree order, which is the button in its own card. If a hint is moved before its own button, the closest preceding --action button is the one in the previous card, so the hint attaches to the wrong card. For the first card, no valid anchor precedes the hint, so it has no anchor. Keep the button first, use unique names per card, or scope the names with anchor-scope once you learn it.