Skip to content

Commit 8300697

Browse files
Editorial review: Document anchor-scope property (#42227)
* Document anchor-scope property * Update files/en-us/web/css/guides/anchor_positioning/using/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Update files/en-us/web/css/guides/anchor_positioning/using/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Fixes for andruud review comments * Update files/en-us/web/css/guides/anchor_positioning/using/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Fixes for estelle review comments * Update files/en-us/web/css/reference/properties/anchor-scope/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * A few more fixes --------- Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
1 parent 466df2f commit 8300697

5 files changed

Lines changed: 399 additions & 3 deletions

File tree

files/en-us/web/css/guides/anchor_positioning/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ In addition, the specification provides CSS-only mechanisms to:
1818

1919
### Properties
2020

21+
- {{cssxref("anchor-scope")}}
2122
- {{cssxref("anchor-name")}}
2223
- {{cssxref("position-anchor")}}
2324
- {{cssxref("position-area")}}
@@ -26,8 +27,6 @@ In addition, the specification provides CSS-only mechanisms to:
2627
- {{cssxref("position-try")}} shorthand
2728
- {{cssxref("position-visibility")}}
2829

29-
The CSS anchor positioning module also introduces the `anchor-scope` property. Currently, no browsers support this feature.
30-
3130
### At-rules and descriptors
3231

3332
- {{cssxref("@position-try")}}

files/en-us/web/css/guides/anchor_positioning/using/index.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,99 @@ For example, to stop a customizable `<select>` element's picker from being ancho
119119
}
120120
```
121121

122+
## Anchor scoping
123+
124+
When multiple anchor elements are given the same {{cssxref("anchor-name")}} value and a positioned element has that name as its {{cssxref("position-anchor")}} property value, the positioned element will be associated with the _last_ anchor element in the source order with that `anchor-name` value.
125+
126+
For example, if a document contains multiple repeated components, each with a positioned element tethered to an anchor, all the positioned elements will be anchored to the last anchor on the page unless each component uses a different anchor name. This is likely not the desired behavior.
127+
128+
The {{cssxref("anchor-scope")}} property can fix this problem by limiting the visibility, or "scope", of an `anchor-name` value to a specific subtree. The result is that each positioned element can only be anchored to an element within the same subtree of the element that has the scope set on it.
129+
130+
- `anchor-scope: all` sets the scope so that _any_ `anchor-name` values set in the subtree can only be bound to by positioned elements in the same subtree.
131+
- `anchor-scope: --my-anchor, --my-anchor2` sets the scope so that the specified `anchor-name` values, when set in the subtree, can only be bound to by positioned elements in the same subtree.
132+
- `anchor-scope: none` is the default value; it specifies that no anchor scoping is set.
133+
134+
For example, let's say you have an multiple anchor and anchor-positioned {{htmlelement("div")}} elements inside {{htmlelement("section")}} containers:
135+
136+
```html live-sample___anchor-scope
137+
<section class="scoped">
138+
<div class="anchor">⚓︎</div>
139+
<div class="positioned">Positioned 1</div>
140+
</section>
141+
142+
<section class="scoped">
143+
<div class="anchor">⚓︎</div>
144+
<div class="positioned">Positioned 2</div>
145+
</section>
146+
147+
<section class="scoped">
148+
<div class="anchor">⚓︎</div>
149+
<div class="positioned">Positioned 3</div>
150+
</section>
151+
```
152+
153+
We turn each `anchor` `<div>` into an anchor element by giving them an `anchor-name` of `--my-anchor`. We then position each `positioned` `<div>` relative to an element with the `--my-anchor` anchor name by giving them absolute positioning, a `position-anchor` value of `--my-anchor`, and a {{cssxref("position-area")}} value of `right`. Finally, we set the anchor scope of each `<section>` container using `anchor-scope: --my-anchor`:
154+
155+
```css hidden live-sample___anchor-scope
156+
html {
157+
height: 100%;
158+
}
159+
160+
body {
161+
height: inherit;
162+
display: flex;
163+
justify-content: space-evenly;
164+
align-items: center;
165+
}
166+
167+
.scoped {
168+
padding: 20px;
169+
background: #eee;
170+
}
171+
172+
.anchor {
173+
font-size: 1.8rem;
174+
color: white;
175+
text-shadow: 1px 1px 1px black;
176+
background-color: blue;
177+
width: fit-content;
178+
padding: 3px;
179+
}
180+
181+
.positioned {
182+
background: orange;
183+
width: fit-content;
184+
padding: 3px;
185+
}
186+
```
187+
188+
```css live-sample___anchor-scope
189+
.anchor {
190+
anchor-name: --my-anchor;
191+
}
192+
193+
.positioned {
194+
position: absolute;
195+
position-anchor: --my-anchor;
196+
position-area: right;
197+
}
198+
199+
.scoped {
200+
anchor-scope: --my-anchor;
201+
}
202+
```
203+
204+
This results in the following positioning behavior:
205+
206+
{{ EmbedLiveSample("anchor-scope", "100%", "150") }}
207+
208+
Each positioned element is positioned relative to the anchor inside the same `<section>` element. This is because each `<section>` element has an `anchor-scope` of `--my-anchor` set on it; positioned elements inside each scoped container can therefore only be positioned relative to `my-anchor` anchors inside the same container.
209+
210+
If we didn't set `anchor-scope: --my-anchor` on the containers, all of the positioned elements would be positioned relative to the last anchor on the page.
211+
122212
## Positioning elements relative to their anchor
123213

124-
As we saw above, associating a positioned element with an anchor is not really much use on its own. Our goal is to place the positioned element relative to its associated anchor element. This is done either by setting a [CSS `anchor()` function](#using_inset_properties_with_anchor_function_values) value on an [inset property](/en-US/docs/Glossary/Inset_properties), [specifying a `position-area`](#setting_a_position-area), or centering the positioned element with the [`anchor-center` placement value](#centering_on_the_anchor_using_anchor-center).
214+
As we saw earlier, associating a positioned element with an anchor is not really much use on its own. Our goal is to place the positioned element relative to its associated anchor element. This is done either by setting a [CSS `anchor()` function](#using_inset_properties_with_anchor_function_values) value on an [inset property](/en-US/docs/Glossary/Inset_properties), [specifying a `position-area`](#setting_a_position-area), or centering the positioned element with the [`anchor-center` placement value](#centering_on_the_anchor_using_anchor-center).
125215

126216
> [!NOTE]
127217
> CSS anchor positioning also provides mechanisms for specifying fallback positions if the positioned element's default position causes it to overflow the viewport. See the [Fallback options and conditional hiding](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Try_options_hiding) guide for details.

files/en-us/web/css/reference/properties/anchor-name/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ Scroll the page to see how both of the infoboxes are tethered to the anchor.
382382
## See also
383383

384384
- {{cssxref("position-anchor")}}
385+
- {{cssxref("anchor-scope")}}
385386
- HTML [`anchor`](/en-US/docs/Web/HTML/Reference/Global_attributes/anchor) attribute
386387
- [CSS anchor positioning](/en-US/docs/Web/CSS/Guides/Anchor_positioning) module
387388
- [Using CSS anchor positioning](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Using) guide

0 commit comments

Comments
 (0)