MDN The anchor-nameCSS property enables defining an element as an anchor element by giving it one or more identifying anchor names. Each name can then be set as the value of a positioned element's position-anchor property to associate it with the anchor.
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Dui nunc mattis enim ut tellus elementum sagittis vitae et.</p><div class="anchor">⚓︎</div><div class="infobox"> <p>This is an information box.</p></div><p> Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. In arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam.</p><p> Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique. Porta lorem mollis aliquam ut porttitor. Turpis cursus in hac habitasse platea dictumst quisque. Dolor sit amet consectetur adipiscing elit. Ornare lectus sit amet est placerat. Nulla aliquet porttitor lacus luctus accumsan.</p>
Syntax
none | <anchor-name>#
Formal grammar from the specification, as webref extracts it: the latest level's definition, sometimes ahead of what browsers implement.
Definition
Initial
none
Applies to
all elements that generate a principal box
Inherited
no
Percentages
n/a
Computed value
as specified
Canonical order
per grammar
Animation type
discrete
Browser support
First browser version supporting anchor-name
Chrome
125
Edge
125
Firefox
147
Safari
26
Chrome Android
125
Safari iOS
26
First version with support, from MDN browser-compat-data.
MDN The anchor-scopeCSS property can be used to limit the scope in which a positioned element can be associated with anchor elements to a particular subtree.
MDN The position-anchorCSS property specifies the default anchor element for a positioned element. This default is used by position-area and position-try, and by anchor functions (anchor() and anchor-size()) when no <anchor-name> argument is provided to those functions.
// Get references to the two select menusconst select1 = document.querySelector("#anchor1-anchor-select");const select2 = document.querySelector("#anchor2-anchor-select");// Store references to all the anchors in a NodeList (array-like)const anchors = document.querySelectorAll("#anchor-container > div");// Set the same change event handler on both select menusselect1.addEventListener("change", updateAnchorNames);select2.addEventListener("change", updateAnchorNames);function updateAnchorNames() { // Remove all anchor names from all anchors for (const anchor of anchors) { anchor.style.anchorName = "none"; } // convert the select menu values to numbers, and remove one to // make them match the selected anchors' index positions in the NodeList const value1 = Number(select1.value) - 1; const value2 = Number(select2.value) - 1; if (value1 === value2) { // If the chosen anchors are both the same, set both anchor // names on the same anchor anchors[value1].style.anchorName = "--my-anchor1, --my-anchor2"; } else { // If they are not the same, set the anchor names separately // on each selected anchor anchors[value1].style.anchorName = "--my-anchor1"; anchors[value2].style.anchorName = "--my-anchor2"; }}
Syntax
normal | none | auto | <anchor-name> | match-parent
Formal grammar from the specification, as webref extracts it: the latest level's definition, sometimes ahead of what browsers implement.
Definition
Initial
normal
Applies to
absolutely positioned boxes
Inherited
no
Percentages
n/a
Computed value
as specified
Canonical order
per grammar
Animation type
discrete
Browser support
First browser version supporting position-anchor
Chrome
151
Edge
151
Firefox
151
Safari
27
Chrome Android
151
Safari iOS
27
First version with support, from MDN browser-compat-data.
MDN The position-areaCSS property enables an anchor-positioned element to be positioned relative to the edges of its associated anchor element by placing the positioned element on one or more tiles of an implicit 3x3 grid, where the anchoring element is the center cell.
<div class="anchor" contenteditable="true">⚓︎</div><p class="positionedElement" contenteditable="true">This can be edited.</p><style contenteditable="true">.positionedElement { position-area: top center; }</style>
Syntax
none | <position-area>
Formal grammar from the specification, as webref extracts it: the latest level's definition, sometimes ahead of what browsers implement.
Definition
Initial
none
Applies to
positioned boxes with a default anchor box
Inherited
no
Percentages
n/a
Computed value
the keyword none or a pair of keywords, see § 3.1.3 Computed Value and Serialization of <position-area>
Canonical order
per grammar
Animation type
discrete
Browser support
First browser version supporting position-area
Chrome
129
Edge
129
Firefox
147
Safari
26
Chrome Android
129
Safari iOS
26
First version with support, from MDN browser-compat-data.
MDN The position-try-fallbacksCSS property enables you to specify a list of one or more alternative position try fallback options for anchor-positioned elements to be placed relative to their associated anchor elements. When the element would otherwise overflow its inset-modified containing block, the browser will try placing the positioned element in these different fallback positions, in the order provided, until it finds a value that stops it from overflowing its container or the viewport.
MDN The position-try-orderCSS property allows you to prioritize the position-try-fallbacks option applied to an anchor-positioned element when it first renders, based on which option creates the most space around the element in the given direction.
MDN The position-visibilityCSS property enables conditionally hiding an anchor-positioned element depending on, for example, whether it is overflowing its containing element or the viewport.
<p> Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique.</p><p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Dui nunc mattis enim ut tellus elementum sagittis vitae et.</p><p> Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique. Porta lorem mollis aliquam ut porttitor. Turpis cursus in hac habitasse platea dictumst quisque.</p><div class="anchor">⚓︎</div><div class="infobox"> <p>This is an information box.</p></div><p> Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. In arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam.</p><p> Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique. Porta lorem mollis aliquam ut porttitor. Turpis cursus in hac habitasse platea dictumst quisque. Dolor sit amet consectetur adipiscing elit. Ornare lectus sit amet est placerat. Nulla aliquet porttitor lacus luctus accumsan.</p><form> <fieldset> <legend>Choose position visibility</legend> <div> <label for="radio-always">always</label> <input type="radio" id="radio-always" name="position-visibility" value="always" checked /> </div> <div> <label for="radio-anchors-visible">anchors-visible</label> <input type="radio" id="radio-anchors-visible" name="position-visibility" value="anchors-visible" /> </div> <div> <label for="radio-no-overflow">no-overflow</label> <input type="radio" id="radio-no-overflow" name="position-visibility" value="no-overflow" /> </div> </fieldset></form>
const infobox = document.querySelector(".infobox");const radios = document.querySelectorAll('[name="position-visibility"]');for (const radio of radios) { radio.addEventListener("change", setPositionVisibility);}function setPositionVisibility(e) { infobox.style.positionVisibility = e.target.value;}
Spec The @position-try rule defines a position option with a given name, specifying one or more sets of positioning properties that can be applied to a box via position-try-fallbacks.
MDN The @position-tryCSSat-rule is used to define a custom position try fallback option, which can be used to define positioning and alignment for anchor-positioned elements. One or more sets of position try fallback options can be applied to the anchored element via the position-try-fallbacks property or position-try shorthand. When the positioned element is moved to a position where it starts to overflow its containing block or the viewport, the browser will select the first position try fallback option it finds that places the positioned element fully back on-screen.
Spec An absolutely positioned box can use the anchor-size() function in its sizing properties to refer to the size of one or more anchor boxes. The anchor-size() function resolves to a <length>. It is only allowed in the accepted @position-try properties (and is otherwise invalid).
MDN The anchor-size()CSSfunction enables setting anchor-positioned element's size, position, and margins relative to the dimensions of anchor elements. It returns the <length> of a specified side of the target anchor element. anchor-size() is only valid when used within the value of anchor-positioned elements' sizing, inset, and margin properties.
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><p> Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. In arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam.</p><div class="anchor">⚓︎</div><div class="infobox" id="infobox1"> <p>This is the first infobox.</p></div><div class="infobox" id="infobox2"> <p>This is the second infobox.</p></div><p> Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique. Porta lorem mollis aliquam ut porttitor. Turpis cursus in hac habitasse platea dictumst quisque. Dolor sit amet consectetur adipiscing elit. Ornare lectus sit amet est placerat. Nulla aliquet porttitor lacus luctus accumsan.</p>
Spec An absolutely positioned box can use the anchor() function as a value in its inset properties to refer to the position of one or more anchor boxes. The anchor() function resolves to a <length>. It is only allowed in the inset properties (and is otherwise invalid).
MDN The anchor()CSSfunction can be used within an anchor-positioned element's inset property values, returning a length value relative to the position of the edges of its associated anchor element.
MDN The anchor-size()CSSfunction enables setting anchor-positioned element's size, position, and margins relative to the dimensions of anchor elements. It returns the <length> of a specified side of the target anchor element. anchor-size() is only valid when used within the value of anchor-positioned elements' sizing, inset, and margin properties.
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p><p> Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. In arcu cursus euismod quis viverra nibh cras pulvinar. Vulputate ut pharetra sit amet aliquam.</p><div class="anchor">⚓︎</div><div class="infobox" id="infobox1"> <p>This is the first infobox.</p></div><div class="infobox" id="infobox2"> <p>This is the second infobox.</p></div><p> Malesuada nunc vel risus commodo viverra maecenas accumsan lacus. Vel elit scelerisque mauris pellentesque pulvinar pellentesque habitant morbi tristique. Porta lorem mollis aliquam ut porttitor. Turpis cursus in hac habitasse platea dictumst quisque. Dolor sit amet consectetur adipiscing elit. Ornare lectus sit amet est placerat. Nulla aliquet porttitor lacus luctus accumsan.</p>