Sub topic 3
Placing an element with anchor()
This chapter 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 have declared an anchor with anchor-name and linked an element to it with position-anchor, as covered in the previous chapter.
What anchor() does
The anchor() function returns a length. That length is the position of one edge of the anchor, expressed in the coordinate space of the positioned element's containing block. You use it as the value of an inset property: top, right, bottom, left, or the logical equivalents such as inset-block-start.
.tooltip {
position: absolute;
top: anchor(bottom);
}This reads as "put my top edge at the anchor's bottom edge". The browser does the measuring and converts the result for you. You do not need to know where the containing block starts.
The function only works on an element with position: absolute or position: fixed. On other elements it has no effect.
A first example: a tooltip above its trigger
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
bottom: anchor(top);
left: anchor(center);
translate: -50% 0;
margin-bottom: 8px;
}Each declaration has a job:
anchor-name: --tipmakes.triggeran anchor.position-anchor: --tiptells the tooltip which anchor itsanchor()calls refer to by default.bottom: anchor(top)places the tooltip's bottom edge at the anchor's top edge.left: anchor(center)places the tooltip's left edge at the horizontal center of the anchor.translate: -50% 0shifts the tooltip left by half its own width, so its center, not its left edge, sits over the anchor's center.margin-bottom: 8pxleaves a gap.
Which keywords you can use
The first argument is a side keyword. It names a point on the anchor.
| Keyword | Meaning |
|---|---|
| `top`, `bottom`, `left`, `right` | The physical edges of the anchor |
| `start`, `end` | Logical edges, relative to the inset property's axis |
| `center` | The midpoint along the axis of the inset property |
| a percentage | A point along the axis, where `0%` is the start and `100%` is the end |
The keyword must suit the property. You can use anchor(top) or anchor(bottom) in top and bottom, which are vertical, but not in left. anchor(center) and percentages work in any inset property.
Naming the anchor explicitly
By default, anchor() uses the anchor set by position-anchor. You can name a different one as the first argument:
.label {
position: absolute;
top: anchor(--header bottom);
left: anchor(--sidebar right);
}This is useful when an element must line up with two different anchors, because position-anchor accepts only one.
Fallback values
anchor() accepts an optional second argument, used when the anchor cannot be found or the function is invalid:
.tooltip {
position: absolute;
top: anchor(bottom, 100px);
}If the anchor is missing, the tooltip sits 100px from the top of its containing block. Without a fallback value, an invalid anchor() makes the whole declaration invalid at computed-value time, so the property behaves as if unset.
Common errors
- Missing
position: absolute. The tooltip stays in normal flow and ignoresanchor(). Setabsoluteorfixed. - Wrong axis keyword. Writing
left: anchor(top)is invalid. Match horizontal keywords toleftandright, and vertical keywords totopandbottom. - Forgetting
translatefor centering.left: anchor(center)aligns the tooltip's left edge, not its middle. - Anchor not found. If
anchor-nameandposition-anchordo not match exactly, including the--prefix, nothing is anchored. Check for typos. - Setting both
topandbottomby accident. The element stretches between the two values instead of keeping its natural height.
Exercise
Exercise
Write CSS that places a .badge element so that its top edge aligns with the top edge of an anchor named --card, and its left edge sits at the anchor's right edge. Assume .card already has anchor-name: --card.
Show answer
.badge { position: absolute; position-anchor: --card; top: anchor(top); left: anchor(right); }
Exercise
A tooltip uses left: anchor(center) but appears shifted to the right of the anchor's middle. What is missing, and why?
Show answer
The tooltip's left edge is at the anchor's center, so its body extends to the right. Adding translate: -50% 0 moves it left by half its own width and centers it.