CSS position-anchor Property
The CSS position-anchor property specifies the default anchor element used to position an absolutely positioned element.
It is part of CSS Anchor Positioning, which allows elements such as tooltips, labels, and popovers to be positioned relative to other elements without relying on fixed coordinates. The property connects a positioned element to an anchor name declared using anchor-name.
position-anchor Syntax
The position-anchor property accepts either a named anchor identifier or the keyword auto.
Basic Syntax
selector {
position-anchor: --my-anchor;
}
The named anchor must be defined using anchor-name on an eligible element.
Available Values
.element {
position-anchor: auto;
}
.element {
position-anchor: --button-anchor;
}
.element {
position-anchor: --menu-anchor;
}
CSS-Wide Keywords
.element {
position-anchor: initial;
}
.element {
position-anchor: inherit;
}
.element {
position-anchor: unset;
}
.element {
position-anchor: revert;
}
.element {
position-anchor: revert-layer;
}
CSS-wide keywords follow the normal CSS cascade rules. They cannot be combined with an anchor name.
Property Values
| Value | Description |
|---|---|
auto |
Uses the element's implicit anchor, if one has been established. Otherwise, no default anchor is selected. |
<dashed-ident> |
Specifies the name of an anchor element, such as --button-anchor. |
initial |
Resets the property to auto. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to auto because it is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | Absolutely positioned elements |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
Valid Values
.tooltip {
position-anchor: --help-button;
}
.popover {
position-anchor: --menu-button;
}
.label {
position-anchor: auto;
}
Invalid Values
.tooltip {
position-anchor: button;
}
.tooltip {
position-anchor: #button;
}
.tooltip {
position-anchor: 20px;
}
.tooltip {
position-anchor: --first --second;
}
Named anchors use dashed identifiers beginning with two hyphens. The property does not accept ordinary element selectors, lengths, or multiple anchor names.
How It Works
CSS Anchor Positioning establishes a relationship between an anchor element and an absolutely positioned element.
Step 1: Name the Anchor
.button {
anchor-name: --help-anchor;
}
The anchor-name property assigns an anchor identifier to the button.
Step 2: Associate the Positioned Element
.tooltip {
position: absolute;
position-anchor: --help-anchor;
}
The tooltip now identifies the button as its default anchor, provided the button is an eligible anchor for that tooltip.
Step 3: Define the Placement
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
}
The position-area property places the tooltip in the area below the anchor.
The Default Anchor
Setting position-anchor establishes a default anchor that other anchor-positioning properties and functions can reference.
Anchor Eligibility
Matching names alone do not guarantee a valid anchor relationship. Anchor eligibility also depends on document structure, layout relationships, and the anchor-positioning rules.
Important Distinction
The position-anchor property selects the default anchor. It does not, by itself, specify where the positioned element appears.
Creating Named Anchors
An element becomes a named anchor when the anchor-name property assigns it one or more dashed identifiers.
Naming a Button
.help-button {
anchor-name: --help-anchor;
}
Naming a Card
.card {
anchor-name: --card-anchor;
}
Using Multiple Names
.element {
anchor-name: --primary-anchor, --secondary-anchor;
}
The anchor-name property can assign multiple names to one element.
HTML Structure
<div class="anchor-container">
<button class="help-button" type="button">Help</button>
<div class="tooltip">Helpful information</div>
</div>
Matching the Name
.help-button {
anchor-name: --help-anchor;
}
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: top;
}
The same dashed identifier connects the tooltip's default anchor to the button.
Important Consideration
Anchor names are CSS identifiers, not HTML IDs. An anchor name does not need to match an element's id attribute.
Setting a Default Anchor
The position-anchor property determines which named anchor is used by default for anchor-based positioning.
Explicit Default Anchor
.tooltip {
position: absolute;
position-anchor: --help-anchor;
}
The tooltip uses the eligible element named --help-anchor as its default anchor.
Automatic Default Anchor
.tooltip {
position-anchor: auto;
}
The auto value uses an implicit anchor when one exists. For example, certain popover relationships can establish an implicit anchor.
Overriding an Implicit Anchor
.tooltip {
position-anchor: --custom-anchor;
}
An explicit anchor name selects a named default anchor instead of relying on an implicit relationship.
Using Different Anchors
.first-tooltip {
position-anchor: --first-button;
}
.second-tooltip {
position-anchor: --second-button;
}
Different positioned elements can reference different named anchors.
Important Consideration
Setting a default anchor does not automatically create a visible offset. Use position-area, anchor(), or another supported anchor-positioning mechanism to define placement.
Positioning Relative to an Anchor
After establishing a default anchor, use anchor-positioning properties to place the positioned element relative to it.
Position Below the Anchor
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
}
Position Above the Anchor
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: top;
}
Position to the Right
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: right;
}
Position to the Left
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: left;
}
Adding Spacing
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
margin-top: 8px;
}
The margin adds spacing between the tooltip and its anchor.
Important Consideration
Anchor positioning depends on browser support and the anchor's eligibility. Provide a suitable fallback when the layout must work in browsers without the feature.
Working With Multiple Anchors
A document can contain multiple named anchors, and different positioned elements can reference them independently.
Two Named Anchors
.first-button {
anchor-name: --first-anchor;
}
.second-button {
anchor-name: --second-anchor;
}
Two Positioned Elements
.first-tooltip {
position: absolute;
position-anchor: --first-anchor;
position-area: bottom;
}
.second-tooltip {
position: absolute;
position-anchor: --second-anchor;
position-area: bottom;
}
Each tooltip uses its corresponding named anchor.
Changing the Default Anchor
.tooltip {
position-anchor: --first-anchor;
}
.tooltip.alternate {
position-anchor: --second-anchor;
}
Changing the property can associate the positioned element with a different eligible anchor.
Repeated Anchor Names
More than one element can declare the same anchor name. The browser applies anchor selection rules to determine which eligible element is used.
Anchor Scope
The anchor-scope property can restrict the scope of anchor names, helping prevent unrelated components from interfering with each other.
Important Consideration
Use descriptive anchor names and appropriate scoping when building reusable interface components.
Using Anchor Functions
The anchor() and anchor-size() functions allow positioned elements to use an anchor's location or dimensions in supported CSS properties.
Using anchor()
.tooltip {
position: absolute;
position-anchor: --help-anchor;
top: anchor(bottom);
left: anchor(left);
}
The anchor() function references edges of the default anchor.
Explicit Anchor Reference
.tooltip {
position: absolute;
top: anchor(--help-anchor bottom);
left: anchor(--help-anchor left);
}
An explicit anchor name inside anchor() allows the function to reference that anchor without relying on position-anchor.
Using anchor-size()
.tooltip {
position: absolute;
position-anchor: --help-anchor;
width: anchor-size(width);
}
The anchor-size() function can size the positioned element using a dimension of its default anchor.
Matching Anchor Width
.menu {
position: absolute;
position-anchor: --menu-button;
position-area: bottom;
width: anchor-size(width);
}
This can be useful for a dropdown menu that should match the width of its trigger.
Important Consideration
These functions are separate from position-anchor. The property selects a default anchor, while the functions retrieve anchor geometry.
Using position-area
The position-area property provides a convenient way to place an absolutely positioned element in an area relative to its default anchor.
Below an Anchor
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
}
Above an Anchor
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: top;
}
To the Side
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: right;
}
Positioning at a Corner
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom right;
}
Two values can select an area relative to the anchor along both axes.
Combining With Margins
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
margin: 8px;
}
Margins can add spacing around the positioned element.
Important Consideration
The position-area property requires an appropriate default anchor to establish its anchor-relative placement.
Anchor Visibility and Fallbacks
Anchor positioning can be affected by missing anchors, scrolling, clipping, and available viewport space.
Missing or Ineligible Anchors
If the specified anchor cannot be resolved to an eligible element, anchor-dependent positioning may not produce the intended result.
Viewport Boundaries
A tooltip placed below an anchor near the bottom of the viewport may overflow the visible area.
Position Fallbacks
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try-fallbacks: flip-block;
}
The flip-block fallback allows the browser to try a flipped placement when the preferred position does not fit.
Anchor Visibility
.tooltip {
position-visibility: anchors-visible;
}
The anchors-visible value can hide a positioned element when its associated anchor is not sufficiently visible under the applicable anchor visibility rules.
Browser Fallback
.tooltip {
position: absolute;
top: 100%;
left: 0;
}
@supports (position-anchor: --help-anchor) and (position-area: bottom) {
.tooltip {
position-anchor: --help-anchor;
position-area: bottom;
top: auto;
left: auto;
}
}
This pattern provides conventional absolute positioning as a fallback, then uses anchor positioning in browsers that support the required properties.
Important Consideration
Test anchored interfaces near viewport edges, inside scrolling containers, and when their anchor elements are hidden or removed.
position-anchor Example
The following example associates a tooltip with a named button anchor and positions the tooltip below it.
HTML Example
<div class="anchor-demo">
<button class="demo-button" type="button">Help Button</button>
<div class="demo-tooltip">This tooltip uses a named anchor.</div>
</div>
CSS Example
.anchor-demo {
position: relative;
min-height: 180px;
padding: 25px;
text-align: center;
border: 2px solid #94a3b8;
background-color: #f1f5f9;
}
.anchor-demo .demo-button {
anchor-name: --demo-anchor;
padding: 10px 18px;
border: 0;
background-color: #2563eb;
color: #ffffff;
}
.anchor-demo .demo-tooltip {
position: absolute;
position-anchor: --demo-anchor;
position-area: bottom;
margin-top: 10px;
padding: 10px 14px;
background-color: #1e293b;
color: #ffffff;
}
@supports not ((position-anchor: --demo-anchor) and (position-area: bottom)) {
.anchor-demo .demo-tooltip {
top: 85px;
left: 50%;
transform: translateX(-50%);
}
}
The button declares an anchor name, and the tooltip references that name through position-anchor. The position-area: bottom declaration places the tooltip below the button when CSS Anchor Positioning is supported.
Changing the Placement
.anchor-demo .demo-tooltip {
position-area: top;
}
In supporting browsers, changing the value to top places the tooltip above the anchor.
Using Anchor Edges Instead
.anchor-demo .demo-tooltip {
position-area: none;
top: anchor(bottom);
left: anchor(center);
transform: translateX(-50%);
}
This alternative uses the anchor() function to reference the anchor's bottom edge and horizontal center.
Accessibility Considerations
The position-anchor property changes visual positioning but does not automatically establish semantic relationships between elements.
Maintain Meaningful HTML Relationships
Keep related controls and content logically organized in the document.
Use Appropriate ARIA Relationships
When a tooltip describes a control, use suitable accessibility attributes such as aria-describedby when appropriate.
Do Not Rely on Visual Proximity Alone
Screen reader users may not perceive that two elements are visually positioned near each other.
Ensure Keyboard Access
Interactive anchored content must remain accessible through keyboard navigation.
Avoid Clipping
Test anchored elements near viewport boundaries and within scrolling containers to ensure content remains visible.
Maintain Focus Visibility
Anchored overlays should not obscure focused controls or interfere with expected keyboard navigation.
Important Consideration
CSS Anchor Positioning controls geometry. HTML semantics, focus management, and accessible interaction behavior must be implemented separately.
Common Mistakes
Forgetting anchor-name
.tooltip {
position: absolute;
position-anchor: --missing-anchor;
position-area: bottom;
}
The tooltip cannot use a named default anchor unless an eligible element declares the matching anchor name.
Using an HTML ID Instead of an Anchor Name
.tooltip {
position-anchor: #help-button;
}
The property requires a dashed identifier, not an ID selector.
Omitting Absolute Positioning
.tooltip {
position-anchor: --help-anchor;
position-area: bottom;
}
Anchor positioning requires an appropriate positioning context. Use position: absolute for a typical anchored tooltip.
Expecting position-anchor to Set Placement
The property identifies the default anchor. It does not specify a top, bottom, left, or right placement by itself.
Using Multiple Anchor Names
.tooltip {
position-anchor: --first-anchor --second-anchor;
}
The property accepts only one default anchor name.
Confusing anchor-name With position-anchor
anchor-name names an anchor element. position-anchor associates a positioned element with a default anchor.
Assuming Every Matching Name Is Eligible
Anchor eligibility depends on more than matching identifiers. Document and layout relationships can prevent an anchor from being used.
Ignoring Browser Support
CSS Anchor Positioning is newer than conventional absolute positioning. Provide a fallback when broad browser compatibility is required.
Ignoring Available Space
An anchored tooltip can overflow the viewport. Consider position fallback properties when appropriate.
Confusing Anchor Positioning With Fixed Positioning
Anchor positioning associates an element with another element's geometry. Fixed positioning normally uses a viewport-related containing block.
Important Considerations
Keep the following points in mind when using position-anchor:
- It specifies the default anchor for an absolutely positioned element.
- Its initial value is
auto. - The property is not inherited.
- It accepts
autoor a single dashed identifier. - Named anchors are declared using
anchor-name. - Anchor names begin with two hyphens.
- Anchor names are not HTML ID selectors.
- The named anchor must be eligible for the positioned element.
- Matching anchor names do not automatically guarantee eligibility.
- The property does not define placement by itself.
- Use
position-areafor convenient anchor-relative placement. - Use
anchor()to reference anchor positions in supported inset properties. - Use
anchor-size()to reference anchor dimensions in supported sizing properties. - An implicit anchor can be used when
position-anchorisauto. - Different positioned elements can use different named anchors.
- One element can declare multiple anchor names.
- Repeated names are resolved according to anchor selection rules.
anchor-scopecan help isolate names in reusable components.- Anchor positioning can be affected by scrolling and clipping.
- Position fallback properties can help handle limited available space.
position-visibilitycan control visibility under supported anchor-related conditions.- Provide conventional positioning fallbacks when needed.
- Test anchored content near viewport edges.
- Test layouts when anchor elements are hidden or removed.
- Visual anchor relationships do not establish accessibility semantics.
- Use appropriate HTML and ARIA relationships for interactive content.
- Ensure anchored interfaces remain usable with keyboard navigation.
Browser Support
The CSS position-anchor property has Limited availability and is not supported consistently across all widely used browsers.
It is part of the newer CSS Anchor Positioning feature set. Support for related properties such as position-area, position-try-fallbacks, and position-visibility may vary by browser and version.
For current compatibility information, visit Can I Use? and search for position-anchor.
Use feature queries and appropriate fallback positioning when supporting browsers that do not implement CSS Anchor Positioning.
Specifications
The position-anchor property is defined in CSS Anchor Positioning Module Level 1.
CSS Anchor Positioning Module Level 1
The specification defines default anchor selection and how named anchors participate in anchor positioning.
For additional documentation and examples, consult MDN Web Docs.
