The anchor-size()
function enables a positioned element's sizing 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 is being sized relative to. It is a valid value for sizing 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 property is it set on.
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 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.