The anchor-size() function enables a positioned element's sizing, position, and margin values to be expressed in terms of an anchor element's dimensions; it returns a <length> value representing the dimension of a specific anchor element the positioned element's property values are set relative to. It is a valid value for sizing, inset, and margin properties set on anchor-positioned elements.
The length returned is the vertical or horizontal size of an anchor element or its containing block. The dimension used is defined by the <anchor-size> parameter. If that parameter is omitted, the dimension used will match the axis of the sizing, position, or margin property is it set on. So for example:
-
width: anchor-size() is equivalent to width: anchor-size(width). -
top: anchor-size() is equivalent to top: anchor-size(height). -
margin-inline-end: anchor-size() is equivalent to margin-inline-end: anchor-size(self-inline). margin-inline-end: anchor-size() is also equivalent to margin-inline-end: anchor-size(width) in horizontal writing modes, or margin-inline-end: anchor-size(height) in vertical writing modes.
The anchor element used as the basis for the dimension length is the element with the anchor-name specified in the <anchor-name> parameter. If more than one element has the same anchor name, the last element with that anchor name in the DOM order is used.
If no <anchor-name> parameter is included in the function call, the element's default anchor, referenced in its position-anchor property, or associated with the element via the anchor HTML attribute, is used.
If an <anchor-name> parameter is included and there are no elements matching that anchor name, the fallback value is used. If no fallback was included, the declaration is ignored. For example, if width: anchor-size(--foo width, 50px); height: anchor-size(--foo width); were specified on the positioned element but no anchor named --foo exists in the DOM, the width would be 50px and the height declaration would have no effect.
If an element has sizing, position, or margin properties with anchor-size() values set on them, but it is not an anchor-positioned element (it does not have its position property set to absolute or fixed or does not have an anchor associated with it via its position-anchor property), the fallback value will be used if one is available. If no fallback is available, the declaration is ignored.
For example, if width: anchor-size(width, 50px); were specified on the positioned element but no anchor was associated with it, the fallback value would be used, so width would get a computed value of 50px.
For detailed information on anchor features and usage, see the CSS anchor positioning module landing page and the Using CSS anchor positioning guide.