Skip to content
Tutorials Center
My learning

Topics

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.

CSS
.tooltip {
  position: absolute;
  position-anchor: --trigger;
  top: anchor(bottom);
  left: anchor(center);
}

Try it

A tooltip attached below a button

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

CSS
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: absolute or fixed.
  • 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 as left, right, center; vertical insets accept top, 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 with calc(anchor(bottom) + 10px).
  • Prefer position-area for simple cases. If you only need "below and centered", position-area is shorter and handles flipping with position-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.