anchor() CSS Function
The anchor() function returns the position of an edge of an anchor element, used in inset properties to place an element next to its anchor.
Description
The anchor() CSS function is part of CSS anchor positioning. It returns the position of one edge of an anchor element, so that an absolutely or fixed positioned element can be placed relative to it. This makes it possible to attach tooltips, popovers, dropdown menus and labels to another element using only CSS, without JavaScript measuring positions.
First, give the anchor a name with anchor-name, for example anchor-name: --menu-btn. Then, on the positioned element, refer to that name either with position-anchor or directly inside the function. anchor() is used in the inset properties (top, right, bottom, left and their logical equivalents). Its side argument says which edge of the anchor to use: physical keywords such as top, bottom, left and right, logical keywords like start and end, center, or a percentage. An optional fallback value is used if the anchor cannot be found.
.button {
anchor-name: --menu-btn;
}
/* Place the tooltip just below the button, centred */
.tooltip {
position: absolute;
position-anchor: --menu-btn;
top: calc(anchor(bottom) + 8px);
left: anchor(center);
translate: -50% 0;
}
/* Name the anchor inside the function instead */
.badge {
position: absolute;
bottom: anchor(--avatar top);
left: anchor(--avatar right, 0px);
}
For simpler cases, the position-area property places an element around its anchor using a grid of keywords, and position-try-fallbacks moves it automatically if it would overflow the screen.
Interactive Demo
Syntax
anchor() = anchor( <anchor-name>? && <anchor-side>, <length-percentage>? )
<anchor-side> = inside | outside | top | left | right | bottom | start | end
| self-start | self-end | <percentage> | center
Values
- <anchor-name>Optional. The <dashed-ident> set by anchor-name on the anchor element. If omitted, the element's position-anchor (default anchor) is used.
- <anchor-side>The edge of the anchor to position against: top, right, bottom, left, start, end, self-start, self-end, inside, outside, center, or a <percentage> of the way across the anchor.
- <length-percentage>Optional. A fallback value used if the anchor does not exist or the function is not valid on that property.
Example
Browser Support
The following information will show you the current browser support for the CSS anchor() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





