To position an element relative to an anchor element, the positioned element requires three features: an association, a position, and a location. The anchor-name
and position-anchor
properties provide the association.
The anchor element accepts one or more <dashed-ident>
anchor names set on it via the anchor-name
property. When one of those names is then set as the value of the position-anchor
property of an element that has its position
set to absolute
or fixed
, the two elements are associated. The two elements become tethered by setting a location on the associated element relative to the anchor, making it an "anchor-positioned" element.
If multiple anchor elements have the same anchor name set on them, and that name is referenced by the position-anchor
property value of a positioned element, that positioned element will be associated with the last anchor element with that anchor name in the source order.
Anchor positioning changes the containing block of anchor-positioned elements, making its position
relative to its anchor rather than to the nearest positioned ancestor element.
To tether and place a positioned element in a specific location relative to an anchor element, an anchor positioning feature is needed, such as the anchor()
function (set within an inset property's value) or the position-area
property.
You cannot associate a positioned element with an anchor element if the anchor is hidden, such as with display: none
or visibility: hidden
, or if the anchor is part of the skipped contents of another element due to it having content-visibility: hidden
set on it.
The anchor-name
property is supported on all elements that generate a principal box. This means that pseudo-elements, including generated content created using ::before
and ::after
, and UI features like the range
input thumb (::-webkit-slider-thumb
) can be anchor elements. Pseudo elements are implicitly anchored to the same element as the pseudo-element's originating element, unless otherwise specified.
For more information on anchor features and usage, see the CSS anchor positioning module landing page and the Using CSS anchor positioning guide.