Favorites

anchor() CSS Function

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

The anchor() function returns the position of an edge of an anchor element, used in inset properties to place an element next to its anchor.

☕
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() CSS function is part of CSS anchor positioning. It returns the position of one edge of an anchor element, so that an absolutely or fixed positioned element can be placed relative to it. This makes it possible to attach tooltips, popovers, dropdown menus and labels to another element using only CSS, without JavaScript measuring positions.

First, give the anchor a name with anchor-name, for example anchor-name: --menu-btn. Then, on the positioned element, refer to that name either with position-anchor or directly inside the function. anchor() is used in the inset properties (top, right, bottom, left and their logical equivalents). Its side argument says which edge of the anchor to use: physical keywords such as top, bottom, left and right, logical keywords like start and end, center, or a percentage. An optional fallback value is used if the anchor cannot be found.

.button {
  anchor-name: --menu-btn;
}

/* Place the tooltip just below the button, centred */
.tooltip {
  position: absolute;
  position-anchor: --menu-btn;
  top: calc(anchor(bottom) + 8px);
  left: anchor(center);
  translate: -50% 0;
}

/* Name the anchor inside the function instead */
.badge {
  position: absolute;
  bottom: anchor(--avatar top);
  left: anchor(--avatar right, 0px);
}

For simpler cases, the position-area property places an element around its anchor using a grid of keywords, and position-try-fallbacks moves it automatically if it would overflow the screen.

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: 65px;
	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;
	margin: 4px;
	padding: 4px 10px;
	background: #f72585;
	color: #fff;
	font-size: 13px;
	top: anchor(bottom);
	left: anchor(left);
}

Syntax

anchor() = anchor( <anchor-name>? && <anchor-side>, <length-percentage>? )

<anchor-side> = inside | outside | top | left | right | bottom | start | end
| self-start | self-end | <percentage> | center

Values

  • <anchor-name>Optional. The <dashed-ident> set by anchor-name on the anchor element. If omitted, the element's position-anchor (default anchor) is used.
  • <anchor-side>The edge of the anchor to position against: top, right, bottom, left, start, end, self-start, self-end, inside, outside, center, or a <percentage> of the way across the anchor.
  • <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() examples</h1>
<div class="card stage">
<button class="button">Anchor button</button>
<div class="tip">Tooltip: top: anchor(bottom)</div>
<div class="side-label">&larr; left: anchor(right)</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() examples */
.stage {
position: relative;
height: 200px;
display: grid;
place-items: center;
}

.button {
anchor-name: --demo-btn;
padding: 10px 18px;
border: 0;
border-radius: 8px;
background: #2563eb;
color: #fff;
font: inherit;
font-weight: 600;
}

.tip {
position: absolute;
position-anchor: --demo-btn;
top: calc(anchor(bottom) + 10px);
left: anchor(center);
translate: -50% 0;
padding: 6px 10px;
border-radius: 6px;
background: #0f172a;
color: #fff;
font-size: 13px;
white-space: nowrap;
}

.side-label {
position: absolute;
position-anchor: --demo-btn;
top: anchor(center);
left: calc(anchor(right) + 12px);
translate: 0 -50%;
font-size: 13px;
color: #dc2626;
font-weight: 600;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS anchor() 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