anchor-size() CSS Function
The anchor-size() function returns the width or height of an anchor element, so a positioned element can match its anchor's size.
Description
The anchor-size() CSS function is part of CSS anchor positioning. It returns the width or height of an anchor element, so that a positioned element can be sized to match it. A typical use is making a dropdown list exactly as wide as the button or input that opens it, or sizing a highlight to cover an element, without any JavaScript.
The anchor is named with anchor-name and referenced either with position-anchor or by naming it inside the function. The size keyword chooses which dimension to read: width or height, the logical block and inline, or self-block and self-inline, which use the positioned element's own writing mode. If the keyword is left out, the dimension matching the property's axis is used. anchor-size() is valid in sizing properties such as width, height, min-width and max-height, as well as in margins and insets, and can be used inside calc(). An optional fallback value is used if the anchor is missing.
.select-button {
anchor-name: --select;
}
/* Dropdown that matches the button's width */
.options {
position: absolute;
position-anchor: --select;
top: anchor(bottom);
left: anchor(left);
width: anchor-size(width);
}
/* Highlight slightly larger than the anchor */
.ring {
position: absolute;
width: calc(anchor-size(--target width) + 12px);
height: calc(anchor-size(--target height) + 12px);
}
Interactive Demo
Syntax
anchor-size() = anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
<anchor-size> = width | height | block | inline | self-block | self-inline
Values
- <anchor-name>Optional. The <dashed-ident> set by anchor-name on the anchor element. If omitted, the element's position-anchor is used.
- <anchor-size>Optional. Which dimension to read: width, height, block, inline, self-block or self-inline. Defaults to the dimension matching the property's axis.
- <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-size() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





