CSS Reference ยท Functions
The anchor() function
Reference for the CSS anchor() function: position an element against the edges of an anchor element using inset properties, with syntax, fallbacks and examples.
The anchor() function returns a length that matches one side of an anchor element. You use it inside the inset properties (top, right, bottom, left and their logical versions) of an absolutely or fixed positioned element, so that element lines up with another element instead of with its containing block. Typical uses are tooltips, popovers, dropdown menus and labels that must stay attached to a button.
The function is part of CSS anchor positioning. It needs an anchor, which is an element that has an anchor-name, and a positioned element that knows which anchor to use, either through position-anchor or by naming the anchor inside the function.
.tooltip { position: absolute; position-anchor: --trigger; top: anchor(bottom); left: anchor(center); }
Try it
The tooltip is not inside the button, yet it sits 10px under it and is centered on it. Resize or move the button and the tooltip follows.
Syntax
anchor( <anchor-name>? && <anchor-side> , <length-percentage>? )
<anchor-side> = inside | outside | top | right | bottom | left
| start | end | self-start | self-end
| center | <percentage>
- Initial value: not applicable (it is a function, not a property).
- Where it works: in inset properties of elements with
position: absoluteorfixed. - Computed value: a resolved length.
- Animatable: the inset property that uses it is animatable as a length.
Values
| Syntax and arguments | Description |
|---|---|
<anchor-name> |
A dashed identifier such as --trigger. Optional: if omitted, the element's position-anchor is used. |
top, right, bottom, left |
A physical edge of the anchor. |
start, end |
The start or end edge of the anchor, in the writing mode of the positioned element's containing block. |
self-start, self-end |
The same, but using the writing mode of the positioned element itself. |
inside |
The edge on the same side as the property: with left, the anchor's left edge. |
outside |
The edge on the opposite side: with left, the anchor's right edge. Handy for placing something beside the anchor. |
center |
The midpoint between the two edges on that axis. |
<percentage> |
A position along the axis: 0% is the start edge and 100% is the end edge. |
<length-percentage> (second argument) |
Fallback used when the anchor cannot be found. |
Usage notes
- Match the side to the axis. Horizontal insets (
left,right) accept horizontal sides such asleft,right,center; vertical insets accepttop,bottom,center. A mismatch makes the function invalid at computed-value time and the fallback is used. - Opposite edges are common.
top: anchor(bottom)puts the top edge of your element at the bottom edge of the anchor, which means "directly below". - Combine with
calc(). Add gaps withcalc(anchor(bottom) + 10px). - Prefer
position-areafor simple cases. If you only need "below and centered",position-areais shorter and handles flipping withposition-try-fallbacks. - Sizes have their own function. To size an element to its anchor, use
anchor-size(), a separate function that works in sizing and inset properties. - Accessibility. Positioning does not create a relationship for assistive technology. Connect a tooltip to its trigger with markup such as
aria-describedby, or use the Popover API with an invoker button. - Always provide a fallback layout. In a browser without support the declaration is ignored and the element falls back to normal absolute positioning.
Browser support
Anchor positioning is a recent feature: check a compatibility table before relying on it, and keep a fallback layout for browsers that do not support it yet.
Related
anchor-nameโ makes an element an anchorposition-anchorโ chooses the default anchorposition-areaโ grid-based placement around the anchorpositionโ absolute and fixed positioning- Lesson: Anchor Positioning