Sub topic 2
Declaring an Anchor and Linking an Element to It
This chapter covers the two properties that connect a tooltip, menu, or popover to the element it points at. It assumes you know the CSS position property, inset properties such as top and left, and how an absolutely positioned element finds its containing block. It also assumes you are comfortable with basic selectors and the cascade.
The two halves of the link
Anchor positioning needs two elements and two declarations.
- The anchor is the element you measure from. You mark it with
anchor-name. - The anchored element is the one you place. You point it at an anchor with
position-anchor.
An anchor name is a dashed identifier, which means it starts with two hyphens, like a custom property. It is not a class or an ID, and it does not appear in your HTML.
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
}The anchored element must be absolutely or fixed positioned. A position: relative or static element ignores the anchor link. This declaration only creates the association. It does not move anything by itself. Placement comes from anchor() and position-area, which later chapters cover.
A working example
The markup below has a button and a tooltip that sit next to each other.
<button class="trigger">Save</button>
<div class="tooltip">Saves your draft</div>The next rules add a basic placement so you can see the link working. They use anchor() in a simple way. The tooltip's bottom edge goes to the anchor's top edge.
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
bottom: anchor(top);
left: anchor(left);
}If you remove the position-anchor line, the anchor() functions have no default anchor to read from. The browser then treats them as invalid and the tooltip falls back to its normal absolute position.
How the browser resolves the link
The browser does not search the whole page without limits. It looks for the last element in tree order that has a matching anchor-name, and that element must come before the anchored element in the document. There are further visibility rules too. The anchor must not be inside a different containing block that the anchored element cannot see. For example, an anchor in a separate scroll container needs care. The anchor-scope chapter later in this tutorial covers how to control which anchors an element can see.
Common errors
- Missing dashes.
anchor-name: tipis invalid. The name must be a dashed identifier such as--tip. - Wrong positioning. The anchored element needs
position: absoluteorposition: fixed. Without it nothing happens. - Name mismatch.
--tipand--Tipare different names, because dashed identifiers are case-sensitive. - Duplicate names. If several elements share one
anchor-name, the last one in tree order wins. A list of buttons that all use--tipgives every tooltip the same anchor, which is rarely what you want. - Anchor after the element. The anchor must come earlier in the DOM than the element that uses it, or the link will not resolve.
Naming and selecting anchors
You can set anchor-name to a list of names to make one element serve as several anchors. You can also use none, the default, to declare that an element is not an anchor.
.card {
anchor-name: --card, --highlight;
}Anchor names work across the cascade like other properties. A later rule can override an earlier one, and you can set them in media queries.
Exercise
A tooltip uses position-anchor: --tip, but it stays at its normal position and never moves to the button. The button has anchor-name: tip. What is wrong, and what is the fix?
Show answer
The anchor name is not a dashed identifier. tip must be written as --tip in the anchor-name declaration, so that it matches position-anchor: --tip. After that, the tooltip also needs position: absolute or position: fixed and an anchor() or position-area rule to actually be placed.