Favorites

anchor-size() CSS Function

anchor-size()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The anchor-size() function returns the width or height of an anchor element, so a positioned element can match its anchor's size.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

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

Anchor
Tip
Demo Code
HTML Code
<div class="demo-area">
	<div class="demo-anchor">Anchor</div>
	<div class="demo-element">Tip</div>
</div>
CSS Code
.demo-area {
	position: relative;
	width: 300px;
	height: 190px;
	background: #f4f4f4;
	outline: 1px dashed #b0b0b0;
}
.demo-anchor {
	position: absolute;
	left: 90px;
	top: 20px;
	width: 120px;
	height: 50px;
	box-sizing: border-box;
	padding: 6px;
	background: #e7ecff;
	border: 2px solid #4361ee;
	font-size: 14px;
	anchor-name: --demo-anchor;
}
.demo-element {
	position: absolute;
	position-anchor: --demo-anchor;
	position-area: bottom;
	margin-top: 6px;
	box-sizing: border-box;
	padding: 4px;
	background: rgb(247 37 133 / 85%);
	color: #fff;
	font-size: 13px;
	width: anchor-size(width);
}

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

<div class="wrap">
<h1>CSS anchor-size() examples</h1>
<div class="card stage">
<button class="select">Choose a fruit &#9662;</button>
<ul class="options">
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
</ul>
<div class="label" style="margin-top: 150px">.options { width: anchor-size(width); }</div>
</div>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* anchor-size() examples */
.stage {
position: relative;
height: 230px;
padding-top: 10px;
}

.select {
anchor-name: --select;
width: 240px;
padding: 10px 14px;
border: 2px solid #2563eb;
border-radius: 8px;
background: #fff;
font: inherit;
text-align: left;
}

.options {
position: absolute;
position-anchor: --select;
top: calc(anchor(bottom) + 6px);
left: anchor(left);
/* exactly as wide as the select button */
width: anchor-size(width);
margin: 0;
padding: 6px 0;
list-style: none;
border-radius: 8px;
background: #fff;
box-shadow: 0 10px 24px rgba(16, 42, 67, 0.18);
}

.options li {
padding: 8px 14px;
}

.options li:hover {
background: #eff6ff;
}
Open in editor

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.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee