You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Document anchored container queries
* Correct descriptor name error on the @container page
* Add list of descriptor links to @container link on module page
* Apply suggestions from code review - fix links and obvious typos.
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
* Fixes for Hamish review comments
* Add note, plus a couple of Claude fixes
---------
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
@@ -63,6 +65,9 @@ In addition, the specification provides CSS-only mechanisms to:
63
65
-[Fallback options and conditional hiding for overflow](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Try_options_hiding)
64
66
- : A guide to the mechanisms CSS anchor positioning provides to prevent anchor-positioned elements from overflowing their containing elements or the viewport, including position try fallback options and conditionally hiding elements.
- : Explains how to use anchored container queries to conditionally apply styles to anchor-positioned elements depending on what position try fallback options are active on them.
70
+
66
71
## Related concepts
67
72
68
73
-[CSS logical properties and values](/en-US/docs/Web/CSS/Guides/Logical_properties_and_values) module:
## Styling anchor-positioned elements based on active fallback
378
+
379
+
One problem the above functionality doesn't solve is updating the styling of an anchor-positionedelement to suit its different fallback options. For example, it is common to include asmall arrow on a tooltip that points to the anchor element it is associated with, improving UX by making the visual association clearer. When the tooltip moves to a different position, you'll need to change the position and orientation of the arrow, otherwise it will look wrong.
380
+
381
+
To solve this problem, you can use anchored container queries. These extend the functionality of [CSS container queries](/en-US/docs/Web/CSS/Guides/Containment/Container_queries) to enable you to detect when a specific fallback option is applied to an anchor-positionedelement, and apply CSS to its descendants as a result. Specifically, anchored container queries rely on two features:
382
+
383
+
- The {{cssxref("container-type")}} property `anchored` value: Apply this to the anchor-positionedelement to start detecting when different fallback options are applied to it.
384
+
- The {{cssxref("@container")}} at-rule `anchored` keyword: This is followed by aset of parentheses inside which the `fallback` descriptor is included. The descriptor's value is a `position-try-fallbacks` value.
385
+
386
+
For example, let's say we have an anchor-positioned tooltip element that is positioned above its anchor by default via a {{cssxref("position-area")}} value of `top`, but has a {{cssxref("position-try-fallbacks")}} value of `flip-block` specified. This will cause the tooltip to flip in the block direction to the bottom of its anchor when it starts to overflow the top of the viewport. If we want to detect when the fallback is applied to the tooltip, we first need to set `container-type: anchored` on it to turn it into an anchored query container.
387
+
388
+
```css
389
+
.tooltip {
390
+
position: absolute;
391
+
position-anchor: --myAnchor;
392
+
position-area: top;
393
+
position-try-fallbacks: flip-block;
394
+
container-type: anchored;
395
+
}
396
+
```
397
+
398
+
With this in place, we can now write a container query like so:
399
+
400
+
```css
401
+
@container anchored(fallback: flip-block) {
402
+
/* Descendant styles here */
403
+
}
404
+
```
405
+
406
+
The query test — `anchored(fallback: flip-block)` — will return true when the `flip-block` fallback option is applied to the tooltip, in which case the styles specified within the `@container` block will be applied. You might for example want to change the position and orientation of the arrow icon so that it continues to point towards the anchor, change the direction of a gradient, etc.
407
+
408
+
For more information on anchored container queries and some examples, see [Using anchored container queries](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Anchored_container_queries).
409
+
377
410
## Using `position-try-order`
378
411
379
412
The {{cssxref("position-try-order")}} property has a slightly different focus to the rest of the position try functionality, in that it makes use of position try fallback options when the positioned element is first displayed, rather than when it is in the process of overflowing.
Copy file name to clipboardExpand all lines: files/en-us/web/css/guides/containment/container_queries/index.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,10 +11,11 @@ Container queries enable you to apply styles to an element based on certain attr
11
11
- The container's size.
12
12
- Styles applied to the container.
13
13
- The container's scroll-state or that of its scrolling ancestor.
14
+
- Whether the container is [anchor-positioned](/en-US/docs/Web/CSS/Guides/Anchor_positioning) and has a [position-try fallback option](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Try_options_hiding) applied to it.
14
15
15
16
Container queries are an alternative to [media queries](/en-US/docs/Web/CSS/Guides/Media_queries), which apply styles to elements based on viewport size or other device characteristics.
16
17
17
-
This article provides an introduction to using container queries, specifically focusing on size container queries. Other guides discuss [style](/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries#container_style_queries) and [scroll-state](/en-US/docs/Web/CSS/Guides/Conditional_rules/Container_scroll-state_queries) container queries in detail.
18
+
This article provides an introduction to using container queries, specifically focusing on size container queries. Other guides discuss [style](/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries#container_style_queries), [scroll-state](/en-US/docs/Web/CSS/Guides/Conditional_rules/Container_scroll-state_queries), and [anchored](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Anchored_container_queries) container queries in detail.
18
19
19
20

20
21
@@ -174,6 +175,7 @@ If you want to use a single-column layout for devices with a smaller viewport, y
174
175
- CSS {{cssxref("content-visibility")}} property
175
176
-[Using container size and style queries](/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries)
The **`@container`**[CSS](/en-US/docs/Web/CSS)[at-rule](/en-US/docs/Web/CSS/Guides/Syntax/At-rules) is a conditional group rule that applies styles to a [containment context](/en-US/docs/Web/CSS/Guides/Containment/Container_queries#naming_containment_contexts).
10
13
Style declarations are filtered by a condition and applied to the container if the condition is true.
11
-
The condition is evaluated when the queried container size, [`<style-feature>`](#container_style_queries), or scroll-state changes.
14
+
The condition is evaluated when the queried container size, [`<style-feature>`](#container_style_queries), scroll-state, or state of the applied [position-try fallback](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Try_options_hiding) (in the case of [anchor-positioned](/en-US/docs/Web/CSS/Guides/Anchor_positioning) containers) changes.
12
15
13
16
The condition must specify one or both of {{cssxref("container-name")}} and `<container-query>`.
14
17
@@ -48,6 +51,15 @@ If no `<container-query>` is specified, named containers are selected.
48
51
}
49
52
}
50
53
54
+
/* With an anchored query */
55
+
@container anchored(fallback: bottom) {
56
+
.infobox::before {
57
+
content: "▲";
58
+
bottom: 100%;
59
+
top: auto;
60
+
}
61
+
}
62
+
51
63
/* With a <container-name> and a <scroll-state> */
@@ -79,7 +91,7 @@ If no `<container-query>` is specified, named containers are selected.
79
91
-`<container-name>` {{optional_inline}}
80
92
- : The name of the container that the styles will be applied to when the query evaluates to `true`, specified as an {{cssxref("ident")}}.
81
93
-`<container-query>` {{optional_inline}}
82
-
- : A set of features that are evaluated against the query container when the size, [`<style-feature>`](#container_style_queries), or scroll-state of the container changes.
94
+
- : A set of features that are evaluated against the query container when the size, [`<style-feature>`](#container_style_queries), scroll-state, or applied position-try fallback of the container changes.
83
95
84
96
### Logical keywords in container queries
85
97
@@ -134,7 +146,7 @@ Details about usage and naming restrictions are described in the {{cssxref("cont
134
146
135
147
### Descriptors
136
148
137
-
The `<container-condition>` queries include [size](#size_container_descriptors) and [scroll-state](#scroll-state_container_descriptors) container descriptors.
149
+
The `<container-condition>` queries include [size](#size_container_descriptors), [scroll-state](#scroll-state_container_descriptors), and [anchored](#anchored_container_descriptors) container descriptors.
138
150
139
151
#### Size container descriptors
140
152
@@ -172,7 +184,7 @@ The `<container-condition>` can include one or more boolean size queries, each w
172
184
173
185
#### Scroll-state container descriptors
174
186
175
-
Scroll-state container descriptors are specified inside the `<container-condition>`within a set of parentheses following the `scroll-state` keyword, for example:
187
+
Scroll-state container descriptors are specified inside the `<container-condition>`as an argument for the `scroll-state()` function, for example:
176
188
177
189
```css
178
190
@container scroll-state(scrollable: top) {
@@ -319,7 +331,7 @@ Supported keywords for scroll-state container descriptors include {{glossary("ph
319
331
320
332
To evaluate a container with anon-`none` `stuck` scroll-state query, it must have `position: sticky` set on it, and be inside a scroll container. If the test passes, the rules inside the `@container` block are applied to descendants of the `position: sticky` container.
321
333
322
-
It is possible for two values from opposite axes to match at the same time:
334
+
It is possible for two values from adjacent axes to match at the same time:
323
335
324
336
```css
325
337
@container scroll-state((stuck: top) and (stuck: left)) {
@@ -343,6 +355,27 @@ Supported keywords for scroll-state container descriptors include {{glossary("ph
343
355
}
344
356
```
345
357
358
+
#### Anchored container descriptors
359
+
360
+
Anchored container descriptors are specified inside the `<container-condition>` as an argument for the `anchored()` function, for example:
- : Queries whether a specific position-try fallback is currently active on an anchor-positioned container, as specified via the {{cssxref("position-try-fallbacks")}} property. Valid `fallback` values include any component value that is valid for inclusion in a `position-try-fallbacks` property value.
376
+
377
+
If the `fallback` value named in the test is currently active on the anchor-positioned container, the test passes, and the rules inside the `@container` block are applied to descendants of the anchor-positioned container.
378
+
346
379
## Formal syntax
347
380
348
381
{{csssyntax}}
@@ -511,7 +544,11 @@ The global `revert` and `revert-layer` are invalid as values in a `<style-featur
511
544
512
545
### Scroll-state queries
513
546
514
-
See [Using container scroll-state queries](/en-US/docs/Web/CSS/Guides/Conditional_rules/Container_scroll-state_queries) for full walkthroughs of scroll-state query examples.
547
+
See [Using container scroll-state queries](/en-US/docs/Web/CSS/Guides/Conditional_rules/Container_scroll-state_queries) for scroll-state query examples.
548
+
549
+
### Anchored queries
550
+
551
+
See [Using anchored container queries](/en-US/docs/Web/CSS/Guides/Anchor_positioning/Anchored_container_queries) for anchored query examples.
515
552
516
553
## Specifications
517
554
@@ -526,6 +563,7 @@ See [Using container scroll-state queries](/en-US/docs/Web/CSS/Guides/Conditiona
0 commit comments