Source order and anchor positioning
CSS anchor positioning removes one of the constraints that ordinary layout techniques impose: where an element sits in the markup no longer determines whether it can be positioned relative to another element. With conventional approaches, an element that needs to visually overlay a sibling must be close to that sibling in source order, or the relationship has to be restructured into a parent-child pairing.
The classic approach: rearranging the markup
Consider ten div elements in a row. Supposing the tenth needs to cover the second, the visual result is easy enough to imagine, but the markup has to cooperate. The two elements must sit next to each other in source order, and the order matters.
<div>1</div>
<div>2</div>
<div>10</div><!-- Sitting next to Div #2-->
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
To make the tenth div sit over the second one rather than beside it, the relationship has to be redefined as a parent-child arrangement.
<div>1</div>
<div class="parent">
2
<div class="child">10</div><!-- Sitting in Div #2's lap-->
</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
From there the CSS can contain the child inside the parent: inset the child’s position so it is pinned to the parent’s top-left edge, then set its width to 100% of the parent’s size so it completely covers and obscures the parent.
.parent {
position: relative; /* Contains Div #10 */
}
.child {
position: absolute;
}
.child {
position: absolute;
inset-block-start: 0;
inset-inline-start: 0;
}
.child {
position: absolute;
inset-block-start: 0;
inset-inline-start: 0;
width: 100%;
}
Anchor positioning with the original markup
Anchor positioning simplifies all of this because it does not care where the tenth div falls in the HTML. The original markup containing the ten elements can be used exactly as written. Note that anchor positioning is only supported by default in the latest version of Chrome at the time of writing, so follow along there.
<div>1</div>
<div class="parent">2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div class="child">10</div>
The second div becomes the anchor element via the anchor-name property. The .parent and .child classes are kept for clarity.
.parent {
anchor-name: --anchor; /* this can be any name formatted as a dashed ident */
}
The child is then connected to the parent with the position-anchor property.
.child {
position-anchor: --anchor; /* has to match the `anchor-name` */
}
Positioning the child over the parent is handled by position-area, which centers the element over the anchor.
.child {
position-anchor: --anchor;
position-area: center;
}
To cover the parent completely, the child’s size is matched to the parent using the anchor-size() function.
.child {
position-anchor: --anchor;
position-area: center;
width: anchor-size(width);
}
Caveat: when source order still matters
There is in fact a case where the anchor’s source order does matter. The rule of thumb: the anchor is fully laid out before the target. If a position value other than static is set on the anchor, then the target element must also have a set position in a direct parent-child relationship, because otherwise the anchor is no longer a containing block. Temani has an explanation of this alongside demos that illustrate what happens.
Skipping HTML source order is a separation of concerns between content and presentation.



