1. 介绍
当前这是第 5 级中新内容的早期草案。 第 3 级和第 4 级的特性仍然定义在 [css-conditional-3] 和 [css-conditional-4] 中,尚未被复制到此处。
CSS Conditional Level 5 扩展了 @supports 规则和 supports 查询 语法, 允许检测自定义支持条件,以及受支持的 at-规则 和字体技术。
它还增加了 @when 规则, 泛化了条件规则的概念。 任何可以通过现有条件规则表达的内容, 都可以通过在 @when 中包裹合适的函数来声明条件类型来表达。 这允许作者轻松组合多种类型的查询, 例如媒体查询和 supports 查询, 用一个布尔表达式实现。 如果没有这个, 作者必须依赖嵌套不同的条件规则, 这样读写都更困难, 并且预设这些条件仅能用“and”布尔关系联结(不易表达其他关系), 同时在所提议的 条件规则链中也会受限。
它还新增了 @else 规则, 紧跟在其他条件规则之后, 并自动将其条件视为紧邻前一规则条件的反向, 从而在 条件规则链 中仅应用第一个匹配的规则。
它还新增了容器查询(Container Queries)。 它们在概念上类似于媒体查询, 但允许检测文档中元素的属性(如盒子尺寸或计算样式), 而不是针对整个文档。
2. 对 @supports 规则的扩展
本级规范扩展了 <supports-feature> 语法如下:
<supports-feature> = <supports-selector-fn>
| <supports-font-tech-fn> | <supports-font-format-fn>
| <supports-at-rule-fn>
| <supports-decl>
<supports-decl> = ( [ <declaration> | <supports-condition-name> ] )
<supports-font-tech-fn> = font-tech( <font-tech> )
<supports-font-format-fn> = font-format( <font-format> )
<supports-at-rule-fn> = at-rule( <at-keyword-token> )
- <supports-condition-name>
-
如果 UA 支持该命名条件,结果为 true。 如果名称无法识别,结果为 false。
- <supports-font-tech-fn>
-
如果 UA 支持作为参数提供的字体技术,结果为 true。
- <supports-font-format-fn>
-
如果 UA 支持作为参数提供的字体格式,结果为 true。
- <supports-at-rule-fn>
-
如果 UA 支持作为参数提供的 at-规则,结果为 true。
2.1. 支持定义的扩展
2.1.1. 字体技术和格式
当 CSS 处理器能够在布局和渲染中利用指定的 CSS Fonts 4 § 11.1 Font tech 时,被认为支持某字体技术。
当 CSS 处理器能够在布局和渲染中利用指定的 CSS Fonts 4 § 11.2 Font formats,且该格式未被指定为 <string> 时,被认为 支持某字体格式。
2.1.2. At-规则
如果 CSS 处理器会在任何上下文中接受以指定 at-keyword 开头的 at-规则,则被认为 支持该 at-规则。
注意:由于 @charset 并不是一个有效的 at-规则, 因此在此定义下不被认为是受支持的。
2.1.3. 命名条件
当相关的 命名 supports 条件 返回 true 时,CSS 处理器被认为 支持某命名条件。
3. 通用条件规则:@when 规则
@when at-规则是一个 条件分组规则,它将诸如 @media 和 @supports 等各自的 条件分组规则进行了泛化。 其定义如下:
@when <boolean-condition>{ <rule-list>}
其中,<boolean-condition> 是类似 Media Queries 4 § 3 语法 的布尔代数, 但以 media() 和 supports() 函数作为叶子。
在 Conditional 中以通用方式定义“布尔代数,以 X 为叶子”, 这样所有条件规则都可以直接引用它,而无需各自重新定义布尔代数。
media() 和 supports() 函数定义如下:
media () =media ( [ <mf-plain> | <mf-boolean> | <mf-range>] ) supports () =supports ( <declaration>)
media() 或 supports() 函数与其所包含条件关联的布尔结果相对应。
4. 级联条件:@else 规则
通常,条件分组规则是相互独立的; 每个规则都有单独的条件, 在不直接参考任何其他规则的情况下进行评估, 并仅基于其自身条件决定是否应用其包含的规则。
这对简单条件来说没问题, 但当需要编写一组彼此互斥的条件时就会变得困难: 作者必须非常谨慎地构造条件, 以避免在其他规则应生效时被激活, 并确保这组条件不会意外地全部排除某种情况, 使其未被样式化。
@else 规则是一个 条件分组规则,用于形成 条件规则链, 它将多个 条件分组规则 关联起来, 并保证只有第一个匹配的规则 会将其条件评估为 true。 其定义如下:
@else <boolean-condition>?{ <rule-list>}
@else 的解释与 @when 相同。 如果省略其 <boolean-condition>, 则将其视为始终为真的条件。
条件规则链 是 一系列连续的 条件分组规则, 以某个非 @else 的 条件分组规则 开始, 后跟零个或多个 @else 规则。 在连续的 条件分组规则 之间不能有除空白和/或注释之外的任何内容; 其他任何标记都会“中断”该链。
我们是否应要求链中只有最后一个 @else 可以省略条件? 在调试代码时,通过将其中一个条件设为 “true” 来短路 if-else 链并不少见; 我认为在 CSS 中也同样有用? 如果不小心省略了条件,仍然很容易看出哪里出了问题。
在一个 条件规则链 中, 会按顺序评估每个 条件分组规则 的条件。 如果其中某个条件为真, 则链中所有后续 条件分组规则 的条件 都评估为假, 无论其声明的条件为何。
不属于 @else 规则链的 条件规则链 中的 @else 规则无效,必须被忽略。
@when media ( width >=400 px ) andmedia ( pointer: fine) andsupports ( display: flex) { /* A */ } @else supports ( caret-color: pink) andsupports ( background:double-rainbow ()) { /* B */ } @else { /* C */ }
上述规则中将恰好选中一个, 即使第二条规则 并未排除大宽度、指针精细或对 flexbox 的支持, 而最后一条规则 根本未指定任何条件。
如果不使用 条件规则链 要实现相同效果, 需要这样写:
@media ( width >=400 px ) and( pointer: fine) { @supports ( display: flex) { /* A */ } @supports not( display: flex) { @supports ( caret-color: pink) and( background:double-rainbow ()) { /* B */ } @supports not(( caret-color: pink) and( background:double-rainbow ())) { /* C */ } } } @media not(( width >=400 px ) and( pointer: fine)) { @supports ( caret-color: pink) and( background:double-rainbow ()) { /* B */ } @supports not(( caret-color: pink) and( background:double-rainbow ())) { /* C */ } }
这既难以阅读, 又需要大量重复条件和内容, 并且非常难以正确编写。 如果条件更复杂一些 (这在真实内容中并不罕见), 示例会变得显著更糟。
后备没有测试条件, 因此除非先前某个条件满足,否则将总是被选择。
@when font-tech ( color-COLRv1) andfont-tech ( variations) { @font-face { font-family : icons; src : url ( icons-gradient-var.woff2 ); } } @else font-tech ( color-SVG) { @font-face { font-family : icons; src : url ( icons-gradient.woff2 ); } } @else font-tech ( color-COLRv0) { @font-face { font-family : icons; src : url ( icons-flat.woff2 ); } } @else { @font-face { font-family : icons; src : url ( icons-fallback.woff2 ); } }
注意,在此示例中, 仅当同时支持 COLRv1 和字体可变性时 才会下载可变彩色字体。
后备仍可能用于某些字符; 例如,彩色字体仅支持拉丁文, 而后备支持拉丁文和希腊文。
@font-face { font-family : icons; src : url ( icons-fallback.woff2 ); @supports font-tech ( color-COLRv1) { @font-face { font-family : icons; src : url ( icons-gradient-var.woff2 ); } }
5. 容器查询
虽然 媒体查询提供了查询文档显示所处用户代理或设备环境方面的方法 (如视口尺寸或用户偏好), 但 容器查询 允许检测文档内元素的某些方面(如盒子尺寸或计算样式)。
默认情况下,所有元素都是用于 容器样式查询 的 查询容器, 并且可以通过使用 container-type 属性 (或 container 简写)指定附加的查询类型, 将其建立为用于 容器尺寸查询 和 容器滚动状态查询 的 查询容器。 应用于 查询容器的 扁平树 后代的样式规则, 可以通过针对该容器进行查询来进行条件化, 使用 @container 条件分组规则。
main, aside{ container : my-layout / inline-size; } .media-object{ display : grid; grid-template : 'img' auto'content' auto /100 % ; } @container my-layout( inline-size >45 em ) { .media-object{ grid-template : 'img content' auto / auto1 fr ; } }
主区域和侧边栏中的媒体对象 将各自响应其所属容器的上下文。
对于表示 DOM 树中真实元素的 ::part() 和 ::slotted() 伪元素 选择器, 可由这些元素的 扁平树 祖先建立 查询容器。 对于其他 伪元素, 查询容器 可以由其 起始元素 的包含性 扁平树 祖先建立。
-
::before、::after、::marker 和 ::backdrop 查询其起始元素
-
::first-letter 和 ::first-line 查询其起始元素, 即使 虚构标签序列 可能会为了继承和渲染的目的 将
::first-line推到其他元素之后 -
::slotted() 选择器可以查询 shadow 树内部的容器, 包括插槽本身
-
::slotted()::before 选择器可以查询已插槽的 shadow 宿主子元素
-
::part() 选择器可以查询 shadow 树内部的容器
-
::placeholder 和 ::file-selector-button 可以查询输入元素, 但如果输入元素使用 shadow 树实现,则不会暴露任何内部容器
< style > # container { width : 100 px ; container-type : inline - size ; } @ container ( inline-size < 150px ) { # inner :: before { content : "BEFORE" ; } } </ style > < div id = container > < span id = inner ></ span > </ div >
< div id = host style = "width:200px" > < template shadowroot = open > < style > # container { width : 100 px ; container-type : inline - size ; } @ container ( inline-size < 150px ) { :: slotted ( span ) { color : green ; } } </ style > < div id = container > < slot /> </ div > </ template > < span id = slotted > Green</ span > </ div >
5.1. 创建查询容器:container-type 属性
| 名称: | container-type |
|---|---|
| 值: | normal | [ [ size | inline-size ] || scroll-state ] |
| 初始值: | normal |
| 适用于: | 所有元素 |
| 继承: | no |
| 百分比: | n/a |
| 计算值: | 指定的关键字 |
| 规范顺序: | 按语法 |
| 动画类型: | 不可动画 |
container-type 属性将元素建立为某些类型查询的 查询容器。 对于需要特定包含类型的尺寸类 容器查询, 元素通过此属性被显式设为 查询容器。 对于其他类型的 查询容器, 任何元素都可以是 查询容器, 如用于 容器样式查询。
各值含义如下:
- size
- 为 查询容器 建立对 容器尺寸查询 的支持,涵盖 行内轴 和 块轴。 对 主框 应用 样式包含 和 尺寸包含, 并建立一个 独立格式化上下文。
- inline-size
- 为容器自身的 行内轴 上的 容器尺寸查询 建立 查询容器。 对 主框 应用 样式包含 和 行内尺寸包含, 并建立一个 独立格式化上下文。
- scroll-state
- 为 查询容器 建立 容器滚动状态查询
- normal
- 该元素不是任何 容器尺寸查询 或 容器滚动状态查询 的 查询容器, 但仍是用于 容器样式查询 的 查询容器。
aside, main{ container-type : inline-size; } h2{ font-size : 1.2 em ; } @container ( width >40 em ) { h2{ font-size : 1.5 em ; } }
@container style ( --cards: small) { article{ border : thin solid silver; border-radius : 0.5 em ; padding : 1 em ; } }
#sticky{ container-type : scroll-state; position : sticky; } @container scroll-state ( stuck: top) { #sticky-child{ background-color : lime; } }
5.2. 命名查询容器:container-name 属性
| 名称: | container-name |
|---|---|
| 值: | none | <custom-ident>+ |
| 初始值: | none |
| 适用于: | 所有元素 |
| 继承: | no |
| 百分比: | n/a |
| 计算值: | 关键字 none,或 标识符 的有序列表 |
| 规范顺序: | 按语法 |
| 动画类型: | 不可动画 |
container-name 属性 指定一组 查询容器名称。 这些名称可被 @container 规则使用, 以筛选要作用的 查询容器。 容器名称不是 树作用域名称。
- none
- 该 查询容器 没有 查询容器名称。
- <custom-ident>
- 将 查询容器名称 指定为 标识符。 关键字 none、and、 not 和 or 被排除在该 <custom-ident> 之外。
main{ container-type : size; container-name : my-page-layout; } .my-component{ container-type : inline-size; container-name : my-component-library; } @container my-page-layout( block-size >12 em ) { .card{ margin-block : 2 em ; } } @container my-component-library( inline-size >30 em ) { .card{ margin-inline : 2 em ; } }
也可以仅根据容器名称来进行查询。
@container my-page-layout{ .card{ padding : 1 em ; } }
5.3. 创建命名容器:container 简写
| 名称: | container |
|---|---|
| 值: | <'container-name'> [ / <'container-type'> ]? |
| 初始值: | 参见各自属性 |
| 适用于: | 参见各自属性 |
| 继承: | 参见各自属性 |
| 百分比: | 参见各自属性 |
| 计算值: | 参见各自属性 |
| 动画类型: | 参见各自属性 |
| 规范顺序: | 按语法 |
测试
- animation-container-size.html (在线测试) (源码)
- animation-container-type-dynamic.html (在线测试) (源码)
- animation-nested-animation.html (在线测试) (源码)
- animation-nested-transition.html (在线测试) (源码)
- aspect-ratio-feature-evaluation.html (在线测试) (源码)
- at-container-parsing.html (在线测试) (源码)
- at-container-serialization.html (在线测试) (源码)
- at-container-style-parsing.html (在线测试) (源码)
- at-container-style-serialization.html (在线测试) (源码)
- auto-scrollbars.html (在线测试) (源码)
- backdrop-invalidation.html (在线测试) (源码)
- calc-evaluation.html (在线测试) (源码)
- canvas-as-container-001.html (在线测试) (源码)
- canvas-as-container-002.html (在线测试) (源码)
- canvas-as-container-003.html (在线测试) (源码)
- canvas-as-container-004.html (在线测试) (源码)
- canvas-as-container-005.html (在线测试) (源码)
- canvas-as-container-006.html (在线测试) (源码)
- change-display-in-container.html (在线测试) (源码)
- chrome-legacy-skip-recalc.html (在线测试) (源码)
- column-spanner-in-container.html (在线测试) (源码)
- conditional-container-status.html (在线测试) (源码)
- container-computed.html (在线测试) (源码)
- container-for-cue.html (在线测试) (源码)
- container-for-shadow-dom.html (在线测试) (源码)
- container-inheritance.html (在线测试) (源码)
- container-inner-at-rules.html (在线测试) (源码)
- container-inside-multicol-with-table.html (在线测试) (源码)
- container-longhand-animation-type.html (在线测试) (源码)
- container-name-computed.html (在线测试) (源码)
- container-name-invalidation.html (在线测试) (源码)
- container-name-parsing.html (在线测试) (源码)
- container-name-tree-scoped.html (在线测试) (源码)
- container-nested.html (在线测试) (源码)
- container-parsing.html (在线测试) (源码)
- container-selection-unknown-features.html (在线测试) (源码)
- container-selection.html (在线测试) (源码)
- container-size-invalidation-after-load.html (在线测试) (源码)
- container-size-invalidation.html (在线测试) (源码)
- container-size-nested-invalidation.html (在线测试) (源码)
- container-size-shadow-invalidation.html (在线测试) (源码)
- container-type-computed.html (在线测试) (源码)
- container-type-containment.html (在线测试) (源码)
- container-type-invalidation.html (在线测试) (源码)
- container-type-layout-invalidation.html (在线测试) (源码)
- container-type-parsing.html (在线测试) (源码)
- container-units-animation.html (在线测试) (源码)
- container-units-basic.html (在线测试) (源码)
- container-units-computational-independence.html (在线测试) (源码)
- container-units-content-box.html (在线测试) (源码)
- container-units-gradient-invalidation.html (在线测试) (源码)
- container-units-gradient.html (在线测试) (源码)
- container-units-in-at-container-dynamic.html (在线测试) (源码)
- container-units-in-at-container-fallback.html (在线测试) (源码)
- container-units-in-at-container.html (在线测试) (源码)
- container-units-ineligible-container.html (在线测试) (源码)
- container-units-invalidation.html (在线测试) (源码)
- container-units-media-queries.html (在线测试) (源码)
- container-units-rule-cache.html (在线测试) (源码)
- container-units-selection.html (在线测试) (源码)
- container-units-shadow.html (在线测试) (源码)
- container-units-sharing-via-rule-node.html (在线测试) (源码)
- container-units-small-viewport-fallback.html (在线测试) (源码)
- container-units-svglength.html (在线测试) (源码)
- container-units-typed-om.html (在线测试) (源码)
- counters-flex-circular.html (在线测试) (源码)
- counters-in-container-dynamic.html (在线测试) (源码)
- counters-in-container.html (在线测试) (源码)
- br-crash.html (在线测试) (源码)
- canvas-as-container-crash.html (在线测试) (源码)
- chrome-bug-1289718-000-crash.html (在线测试) (源码)
- chrome-bug-1289718-001-crash.html (在线测试) (源码)
- chrome-bug-1346969-crash.html (在线测试) (源码)
- chrome-bug-1362391-crash.html (在线测试) (源码)
- chrome-bug-1429955-crash.html (在线测试) (源码)
- chrome-bug-1505250-crash.html (在线测试) (源码)
- chrome-bug-346264227-crash.html (在线测试) (源码)
- chrome-bug-372358471-crash.html (在线测试) (源码)
- chrome-custom-highlight-crash.html (在线测试) (源码)
- chrome-layout-root-crash.html (在线测试) (源码)
- chrome-quotes-crash.html (在线测试) (源码)
- chrome-remove-insert-evaluator-crash.html (在线测试) (源码)
- columns-in-table-001-crash.html (在线测试) (源码)
- columns-in-table-002-crash.html (在线测试) (源码)
- container-in-canvas-crash.html (在线测试) (源码)
- container-type-change-chrome-legacy-crash.html (在线测试) (源码)
- dialog-backdrop-crash.html (在线测试) (源码)
- dirty-rowgroup-crash.html (在线测试) (源码)
- flex-in-columns-000-crash.html (在线测试) (源码)
- flex-in-columns-001-crash.html (在线测试) (源码)
- flex-in-columns-002-crash.html (在线测试) (源码)
- flex-in-columns-003-crash.html (在线测试) (源码)
- focus-inside-content-visibility-crash.html (在线测试) (源码)
- force-sibling-style-crash.html (在线测试) (源码)
- grid-in-columns-000-crash.html (在线测试) (源码)
- grid-in-columns-001-crash.html (在线测试) (源码)
- grid-in-columns-002-crash.html (在线测试) (源码)
- grid-in-columns-003-crash.html (在线测试) (源码)
- iframe-init-crash.html (在线测试) (源码)
- inline-multicol-inside-container-crash.html (在线测试) (源码)
- inline-with-columns-000-crash.html (在线测试) (源码)
- inline-with-columns-001-crash.html (在线测试) (源码)
- input-column-group-container-crash.html (在线测试) (源码)
- input-placeholder-inline-size-crash.html (在线测试) (源码)
- marker-gcs-after-disconnect-crash.html (在线测试) (源码)
- math-block-container-child-crash.html (在线测试) (源码)
- mathml-container-type-crash.html (在线测试) (源码)
- orthogonal-replaced-crash.html (在线测试) (源码)
- pseudo-container-crash.html (在线测试) (源码)
- remove-dom-child-change-style.html (在线测试) (源码)
- reversed-ol-crash.html (在线测试) (源码)
- size-change-during-transition-crash.html (在线测试) (源码)
- svg-layout-root-crash.html (在线测试) (源码)
- svg-resource-in-container-crash.html (在线测试) (源码)
- svg-text-crash.html (在线测试) (源码)
- table-in-columns-000-crash.html (在线测试) (源码)
- table-in-columns-001-crash.html (在线测试) (源码)
- table-in-columns-002-crash.html (在线测试) (源码)
- table-in-columns-003-crash.html (在线测试) (源码)
- table-in-columns-004-crash.html (在线测试) (源码)
- table-in-columns-005-crash.html (在线测试) (源码)
- top-layer-crash.html (在线测试) (源码)
- top-layer-nested-crash.html (在线测试) (源码)
- custom-layout-container-001.https.html (在线测试) (源码)
- custom-property-style-queries.html (在线测试) (源码)
- custom-property-style-query-change.html (在线测试) (源码)
- deep-nested-inline-size-containers.html (在线测试) (源码)
- dialog-backdrop-create.html (在线测试) (源码)
- dialog-backdrop-remove.html (在线测试) (源码)
- display-contents-dynamic-style-queries.html (在线测试) (源码)
- display-contents.html (在线测试) (源码)
- display-in-container.html (在线测试) (源码)
- display-none.html (在线测试) (源码)
- fieldset-legend-change.html (在线测试) (源码)
- flex-basis-with-container-type.html (在线测试) (源码)
- font-relative-calc-dynamic.html (在线测试) (源码)
- font-relative-units-dynamic.html (在线测试) (源码)
- font-relative-units.html (在线测试) (源码)
- fragmented-container-001.html (在线测试) (源码)
- get-animations.html (在线测试) (源码)
- grid-container.html (在线测试) (源码)
- grid-item-container.html (在线测试) (源码)
- idlharness.html (在线测试) (源码)
- iframe-in-container-invalidation.html (在线测试) (源码)
- iframe-invalidation.html (在线测试) (源码)
- ineligible-containment.html (在线测试) (源码)
- inheritance-from-container.html (在线测试) (源码)
- inline-size-and-min-width.html (在线测试) (源码)
- inline-size-bfc-floats.html (在线测试) (源码)
- inline-size-containment-vertical-rl.html (在线测试) (源码)
- inline-size-containment.html (在线测试) (源码)
- inner-first-line-non-matching.html (在线测试) (源码)
- layout-dependent-focus.html (在线测试) (源码)
- multicol-container-001.html (在线测试) (源码)
- multicol-inside-container.html (在线测试) (源码)
- nested-query-containers.html (在线测试) (源码)
- nested-size-style-container-invalidation.html (在线测试) (源码)
- never-match-container.html (在线测试) (源码)
- no-layout-containment-abspos-dynamic.html (在线测试) (源码)
- no-layout-containment-abspos.html (在线测试) (源码)
- no-layout-containment-baseline.html (在线测试) (源码)
- no-layout-containment-fixedpos-dynamic.html (在线测试) (源码)
- no-layout-containment-fixedpos.html (在线测试) (源码)
- no-layout-containment-scroll.html (在线测试) (源码)
- no-layout-containment-subgrid-crash.html (在线测试) (源码)
- orthogonal-wm-container-query.html (在线测试) (源码)
- percentage-padding-orthogonal.html (在线测试) (源码)
- pseudo-elements-001.html (在线测试) (源码)
- pseudo-elements-002.html (在线测试) (源码)
- pseudo-elements-002b.html (在线测试) (源码)
- pseudo-elements-003.html (在线测试) (源码)
- pseudo-elements-004.html (在线测试) (源码)
- pseudo-elements-005.html (在线测试) (源码)
- pseudo-elements-006.html (在线测试) (源码)
- pseudo-elements-007.html (在线测试) (源码)
- pseudo-elements-008.html (在线测试) (源码)
- pseudo-elements-009.html (在线测试) (源码)
- pseudo-elements-010.html (在线测试) (源码)
- pseudo-elements-011.html (在线测试) (源码)
- pseudo-elements-012.html (在线测试) (源码)
- pseudo-elements-013.html (在线测试) (源码)
- query-content-box.html (在线测试) (源码)
- query-evaluation-style.html (在线测试) (源码)
- query-evaluation.html (在线测试) (源码)
- reattach-container-with-dirty-child.html (在线测试) (源码)
- registered-color-style-queries.html (在线测试) (源码)
- resize-while-content-visibility-hidden.html (在线测试) (源码)
- at-container-scrollable-parsing.html (在线测试) (源码)
- at-container-scrollable-serialization.html (在线测试) (源码)
- at-container-snapped-parsing.html (在线测试) (源码)
- at-container-snapped-serialization.html (在线测试) (源码)
- at-container-stuck-parsing.html (在线测试) (源码)
- at-container-stuck-serialization.html (在线测试) (源码)
- container-type-scroll-state-computed.html (在线测试) (源码)
- container-type-scroll-state-containment.html (在线测试) (源码)
- container-type-scroll-state-parsing.html (在线测试) (源码)
- scroll-state-initially-scrollable.html (在线测试) (源码)
- scroll-state-initially-snapped.html (在线测试) (源码)
- scroll-state-initially-stuck.html (在线测试) (源码)
- scroll-state-scrollable-change.html (在线测试) (源码)
- scroll-state-scrollable-container-type-change.html (在线测试) (源码)
- scroll-state-scrollable-layout-change.html (在线测试) (源码)
- scroll-state-scrollable-wm.html (在线测试) (源码)
- scroll-state-snapped-change.html (在线测试) (源码)
- scroll-state-snapped-container-type-change.html (在线测试) (源码)
- scroll-state-snapped-layout-change.html (在线测试) (源码)
- scroll-state-snapped-none.html (在线测试) (源码)
- scroll-state-snapped-snap-changing.html (在线测试) (源码)
- scroll-state-snapped-wm.html (在线测试) (源码)
- scroll-state-stuck-container-type-change.html (在线测试) (源码)
- scroll-state-stuck-layout-change.html (在线测试) (源码)
- scroll-state-stuck-writing-direction.html (在线测试) (源码)
- scroll-state-target-query-change.html (在线测试) (源码)
- scrollbar-container-units-block.html (在线测试) (源码)
- scrollbar-container-units-inline.html (在线测试) (源码)
- sibling-layout-dependency.html (在线测试) (源码)
- size-container-no-principal-box.html (在线测试) (源码)
- size-container-with-quotes.html (在线测试) (源码)
- size-container-writing-mode-change.html (在线测试) (源码)
- size-feature-evaluation.html (在线测试) (源码)
- style-change-in-container.html (在线测试) (源码)
- style-container-for-shadow-dom.html (在线测试) (源码)
- style-container-invalidation-inheritance.html (在线测试) (源码)
- style-not-sharing-float.html (在线测试) (源码)
- style-query-document-element.html (在线测试) (源码)
- style-query-no-cycle.html (在线测试) (源码)
- style-query-with-unknown-width.html (在线测试) (源码)
- svg-foreignobject-child-container.html (在线测试) (源码)
- svg-foreignobject-no-size-container.html (在线测试) (源码)
- svg-g-no-size-container.html (在线测试) (源码)
- svg-root-size-container.html (在线测试) (源码)
- table-inside-container-changing-display.html (在线测试) (源码)
- top-layer-dialog-backdrop.html (在线测试) (源码)
- top-layer-dialog-container.html (在线测试) (源码)
- top-layer-dialog.html (在线测试) (源码)
- top-layer-nested-dialog.html (在线测试) (源码)
- transition-scrollbars.html (在线测试) (源码)
- transition-style-change-event-002.html (在线测试) (源码)
- transition-style-change-event.html (在线测试) (源码)
- unsupported-axis.html (在线测试) (源码)
- viewport-units-dynamic.html (在线测试) (源码)
- viewport-units.html (在线测试) (源码)
- whitespace-update-after-removal.html (在线测试) (源码)
container 简写属性 可在同一声明中同时设置 container-type 和 container-name。 如果省略 <'container-type'>, 则其值会被重置为初始值。
main{ container : my-layout / size; } .grid-item{ container : my-component / inline-size; }
5.4. 容器查询:@container 规则
@container 规则 是一种 条件分组规则,其条件包含 一个 容器查询, 即 容器尺寸查询 和/或 容器样式查询 的布尔组合。 <block-contents> 块内的样式声明, 只会在其条件为真、且元素的 容器查询 成立时, 通过 过滤 生效。
@container 规则的语法如下:
@container <container-condition>#{ <block-contents>}
其中:
<container-condition> =[ <container-name>? <container-query>?] ! <container-name> = <custom-ident> <container-query> = not <query-in-parens> | <query-in-parens>[ [ and <query-in-parens>] * |[ or <query-in-parens>] *] <query-in-parens> =( <container-query>) |( <size-feature>) |style ( <style-query>) |scroll-state ( <scroll-state-query>) | <general-enclosed> <style-query> = not <style-in-parens> | <style-in-parens>[ [ and <style-in-parens>] * |[ or <style-in-parens>] *] | <style-feature> <style-in-parens> =( <style-query>) |( <style-feature>) | <general-enclosed> <style-feature> = <style-feature-plain> | <style-feature-boolean> | <style-range> <style-feature-plain> = <style-feature-name> : <style-feature-value> <style-feature-boolean> = <style-feature-name> <style-range> = <style-range-value> <mf-comparison> <style-range-value> | <style-range-value> <mf-lt> <style-range-value> <mf-lt> <style-range-value> | <style-range-value> <mf-gt> <style-range-value> <mf-gt> <style-range-value> <style-range-value> = <custom-property-name> | <style-feature-value> <scroll-state-query> = not <scroll-state-in-parens> | <scroll-state-in-parens>[ [ and <scroll-state-in-parens>] * |[ or <scroll-state-in-parens>] *] | <scroll-state-feature> <scroll-state-in-parens> =( <scroll-state-query>) |( <scroll-state-feature>) | <general-enclosed>
测试
- container-ident-function.html (在线测试) (源码)
- container-ident-function.html (在线测试) (源码)
- multiple-size-containers-comma-separated-queries.html (在线测试) (源码)
- multiple-style-containers-comma-separated-queries.html (在线测试) (源码)
- query-container-name.html (在线测试) (源码)
- at-container-scrolled-parsing.html (在线测试) (源码)
- multiple-scroll-state-containers-comma-separated-queries.html (在线测试) (源码)
- scroll-state-query-with-var.html (在线测试) (源码)
- size-query-with-var.html (在线测试) (源码)
关键字 none、and、not 和 or 被排除在上述 <custom-ident> 之外。
对于每个元素, 要查询的 查询容器 从该元素的祖先 查询容器 中选取, 它们需要是所有 容器特性 在 <container-query> 内的有效 查询容器。 如果 <container-query> 包含 未知或不支持的 容器特性, 那么该 <container-condition> 将不会选中任何 查询容器。 <container-name> 会将被考虑的 查询容器 集合限制为名称匹配的那些。
一旦为元素选定了合格的 查询容器, <container-query> 内的每个 容器特性 都会针对该 查询容器 进行评估。 如果没有祖先是合格的 查询容器, 则该元素的 容器查询 为 unknown。 如同媒体查询,<general-enclosed> 的结果也是 unknown。 如果省略 <container-query>, 只要 <container-name> 匹配,该 查询容器 就是合格的。
如果一个 容器查询 包含多个 <container-condition>, 每个条件都会选择自己的 查询容器 并独立评估。 容器查询 若任一组成 <container-condition> 为 true,则整体为 true; 仅当所有组成 <container-condition> 都为 false 时才为 false。
@container card( inline-size >30 em ) andstyle ( --responsive: true) { /* styles */ }
上述样式只会在存在名为 "card" 的祖先容器,并且同时满足 inline-size 和 style 条件时应用。
我们也可以将多个条件合并为一个列表, 每个条件分别针对不同的容器进行评估:
@container card( inline-size >30 em ), style ( --large: true) { /* styles */ }
上述样式会在存在名为 "card" 的祖先容器且满足 inline-size 条件或最近的样式容器满足 style 条件时应用。
定义在多个嵌套 容器查询 内的元素上的样式规则, 仅当所有包裹的 容器查询 对该元素均为真时才会应用。
注意: 嵌套的 容器查询 可针对不同容器进行评估, 因此并不总能将各个 <container-condition> 合并为一个查询。
@container card( inline-size >30 em ), style ( --responsive: true) { /* styles */ }
上述样式会在元素处于 任一 满足 inline-size 条件的名为 "card" 的容器 或 满足 style 条件的容器内时应用。
如果要要求多个条件 全部 满足且需要查询多个容器, 则需要嵌套多个查询:
@container card( inline-size >30 em ) { @container style ( --responsive: true) { /* styles */ } }
上述样式仅在同时存在名为 "card" 的祖先容器且满足 inline-size 条件, 并且 存在满足 style 条件的祖先容器时应用。
全局的、定义名称的 at-规则,如 @keyframes、@font-face 或 @layer, 如果定义在 容器查询 内,并不受 容器查询 条件的限制。
5.5. 动画容器
对 容器查询 的评估发生变化时, 必须作为 样式变更事件 的一部分处理, 即便该变化是由 动画效果 引起的。
main{ display : flex; width : 300 px ; } #container{ container-type : inline-size; flex : 1 ; } /* Resolved width is initially 200px, but changes as the transition on #sibling progresses. */ #inner{ transition : 1 s background-color; background-color : tomato; } /* When this container query starts (or stops) applying, a transition must start on background-color on #inner. */ @container ( width <=150 px ) { #inner{ background-color : skyblue; } }
< main > < div id = container > < div id = inner > Inner</ div > </ div > < div id = sibling > Sibling</ div > </ main >
由 计算值 引起的 容器查询长度 单位的变化, 也必须作为 样式变更事件 的一部分处理。
6. 容器特性
容器特性 用于查询查询容器的某个具体方面。
容器特性在评估时遵循与 媒体特性相同的规则, 当它们处于布尔上下文时。
6.1. 尺寸容器特性
容器尺寸查询允许查询 查询容器的主框的尺寸。 它是由多个单独尺寸特性 (<size-feature>)的布尔组合,每个尺寸特性都查询 查询容器的某一具体维度特性。 <size-feature> 的语法与 媒体特性相同: 即特性名、比较符和数值。[mediaqueries-5] 组合 尺寸特性为 尺寸查询的布尔语法和逻辑 与CSS 特性查询相同。 (参见@supports。[css-conditional-3])
如果查询容器 没有主框, 或主框不是布局包含盒, 或查询容器不支持相关轴上的 容器尺寸查询, 那么对尺寸特性的评估结果为 unknown(未知)。
相对长度单位 (包括容器查询长度单位) 以及自定义属性在容器查询条件中 的计算基于查询容器的计算值。
树计数函数(CSS Values 5 § 9 树计数函数:sibling-count() 和 sibling-index() 表示法)在容器元素上计算。
注意: 这与媒体查询中相对单位的处理方式不同。
注意: 如果自定义属性替换后导致 尺寸特性的值无效,则按其他无效特性值处理, 尺寸特性的结果为 unknown。
aside, main{ container-type : inline-size; } aside{ font-size : 16 px ; } main{ font-size : 24 px ; } @container ( width >40 em ) { h2{ font-size : 1.5 em ; } }
查询条件中用到的 40em 值 相对于相关查询容器的font-size计算:
-
aside 内的 h2,该条件在大于 640px 时为真。
-
main 内的 h2,该条件在大于 960px 时为真。
aside, main{ container-type : inline-size; } aside{ --query : 300 px ; } main{ --query : 500 px ; } @container ( width >var ( --query)) { h2{ font-size : 1.5 em ; } }
查询条件中用到的 var(--query) 的值 会被替换为对应 查询容器上 --query 自定义属性 的计算值:
-
aside 内的 h2,该条件在大于 300px 时为真。
-
main 内的 h2,该条件在大于 500px 时为真。
6.1.1. 宽度:width 特性
| 名称: | width |
|---|---|
| 适用于: | @container |
| 值: | <length> |
| 类型: | range |
6.1.2. 高度:height 特性
| 名称: | height |
|---|---|
| 适用于: | @container |
| 值: | <length> |
| 类型: | range |
6.1.3. 行内尺寸:inline-size 特性
| 名称: | inline-size |
|---|---|
| 适用于: | @container |
| 值: | <length> |
| 类型: | range |
inline-size 容器特性查询查询容器的内容区在 查询容器的行内轴上的尺寸。
6.1.4. 块级尺寸:block-size 特性
| 名称: | block-size |
|---|---|
| 适用于: | @container |
| 值: | <length> |
| 类型: | range |
block-size 容器特性查询查询容器的内容区在 查询容器的块轴上的尺寸。
6.1.5. 宽高比:aspect-ratio 特性
| 名称: | aspect-ratio |
|---|---|
| 适用于: | @container |
| 值: | <ratio> |
| 类型: | range |
aspect-ratio 容器特性被定义为 width 容器特性 的值与 height 容器特性的值之比。
6.1.6. 方向:orientation 特性
| 名称: | orientation |
|---|---|
| 适用于: | @container |
| 值: | portrait | landscape |
| 类型: | discrete |
- portrait
- 当orientation 容器特性的 height 容器特性的值大于等于 width 容器特性的值时,为 portrait。
- landscape
- 否则orientation为 landscape。
6.2. 样式容器特性
容器样式查询 允许查询查询容器的计算值。 它是多个单独样式特性(<style-feature>)的布尔组合, 每个样式特性都查询 查询容器的单一、具体属性。 <style-feature> 的语法可以是 合法的声明、<style-feature-name> 或合法的样式范围 (<style-range>)。 <style-feature-name> 可以是 支持的 CSS 属性或 合法的<custom-property-name>。 <style-feature-value> 产生式匹配任何 合法的<declaration-value>, 只要不包含<mf-lt>、<mf-gt> 和<mf-eq> 记号。
测试
<style-feature-plain> 在 查询容器上给定属性的计算值 等于给定值时为 true (此值也基于 查询容器 计算),否则为 false。
没有值的样式特性 (<style-feature-boolean>)在 计算值 与该属性的初始值不同则为 true。
-
如果<style-range-value>是 <custom-property-name>, 则应像将 <custom-property-name> 包裹在 var() 中一样进行替换。
-
在 任意替换函数 内执行 <style-range-value> 的替换。
-
将<style-range-value>解析为 <number>、<percentage>、 <length>、<angle>、<time>、 <frequency> 或 <resolution>。 如果无法完成,评估为 false。
-
如果范围内的每个<style-range-value>类型相同, 则分别计算并进行比较; 单位为零的零在与<length>比较时视为零长度。
-
否则评估为 false。
将样式特性组合为 样式查询的布尔语法和逻辑 与CSS 特性查询相同。 (参见@supports。[css-conditional-3])
查询样式特性 的简写属性,当所有长属性的 计算值均匹配时为真,否则为假。
依赖级联的关键字,如revert 与 revert-layer, 在样式特性中作为值无效, 会导致容器样式查询为 false。
注意: 其余非级联依赖的 CSS-wide 关键字会相对于查询容器计算, 与其它值一致。
6.3. 滚动状态容器特性
容器滚动状态查询 允许针对依赖滚动位置的状态对容器进行查询。它是由多个 单独的滚动状态特性(<scroll-state-feature>)的布尔组合组成,每个特性 都查询查询容器的单一特性。<scroll-state-feature> 的语法与 媒体特性相同:特性名、比较符和值。
滚动状态特性 既可以匹配滚动器自身的状态,也可以匹配受祖先 滚动容器 的滚动视口滚动位置影响的元素状态。前者的例子是 scrollable 特性,后者是 snapped。
6.3.1. 滚动状态的更新
滚动状态可能会引发布局循环,因为被查询的滚动状态可能导致样式变化, 而样式变化又可能在布局之后引起滚动状态变化。
为避免此类布局循环,scroll-state 查询容器会在 运行快照布局后状态步骤时更新其当前状态,该步骤只会在 HTML 事件循环处理模型的特定时机运行。
当被要求运行快照布局后状态步骤时,需更新每个 scroll-state 查询容器的当前状态。该快照状态将在下次再次运行这些步骤之前, 用于所有样式与布局更新。
6.3.2. 粘性定位:stuck 特性
| 名称: | stuck |
|---|---|
| 适用于: | @container |
| 值: | none | top | right | bottom | left | block-start | inline-start | block-end | inline-end |
| 类型: | discrete |
stuck 容器特性用于查询某个粘性定位的容器是否为保持在指定边缘的 粘性视图矩形内而发生了可见的偏移。逻辑边会基于 查询容器的方向与写字模式映射到物理边。 如果查询容器不是粘性定位,则任何值都不匹配。
来自相互垂直的两个轴的值可能会同时匹配, 但同一轴上的对立边不会同时匹配。
@container scroll-state (( stuck: top) and( stuck: left)) { ...}
永远不会匹配:
@container scroll-state (( stuck: left) and( stuck: right)) { ...}
- none
- 粘性容器未在任何方向发生偏移。
- top
- 粘性容器为保持在顶部边缘而发生了偏移。
- right
- 粘性容器为保持在右侧边缘而发生了偏移。
- bottom
- 粘性容器为保持在底部边缘而发生了偏移。
- left
- 粘性容器为保持在左侧边缘而发生了偏移。
- block-start
- 粘性容器为保持在block-start 边缘而发生了偏移。
- inline-start
- 粘性容器为保持在inline-start 边缘而发生了偏移。
- block-end
- 粘性容器为保持在block-end 边缘而发生了偏移。
- inline-end
- 粘性容器为保持在inline-end 边缘而发生了偏移。
6.3.3. 滚动吸附:snapped 特性
| 名称: | snapped |
|---|---|
| 适用于: | @container |
| 值: | none | x | y | block | inline | both |
| 类型: | discrete |
snapped 容器特性用于查询某个吸附目标
是否已经或将要在给定轴上吸附到其滚动吸附容器。也就是说,
它匹配任何会触发 scrollsnapchanging
事件的吸附目标。
- none
- 查询容器 不是吸附目标。
- x
- 当snapped 容器特性匹配 x 时,表示查询容器是其滚动容器的 水平吸附目标。
- y
- 当snapped 容器特性匹配 y 时,表示查询容器是其滚动容器的 垂直吸附目标。
- block
- 当snapped 容器特性匹配 block 时,表示查询容器是其吸附目标,且位于滚动吸附容器的块方向上。
- inline
- 当snapped 容器特性匹配 inline 时,表示查询容器是其吸附目标,且位于滚动吸附容器的行内方向上。
- both
- 当snapped 容器特性匹配 both 时,表示查询容器是其吸附目标,且位于滚动吸附容器的两个方向上。
6.3.4. 可滚动:scrollable 特性
| 名称: | scrollable |
|---|---|
| 适用于: | @container |
| 值: | none | top | right | bottom | left | block-start | inline-start | block-end | inline-end | x | y | block | inline |
| 类型: | discrete |
测试
- scroll-state-scrollable-axis.html (在线 测试) (源码)
- scroll-state-scrollable-body-001.html (在线 测试) (源码)
- scroll-state-scrollable-body-002.html (在线 测试) (源码)
- scroll-state-scrollable-layout-change-002.html (在线 测试) (源码)
- scroll-state-scrollable-pseudo.html (在线 测试) (源码)
- scroll-state-scrollable-root.html (在线 测试) (源码)
scrollable 容器特性用于查询某个滚动容器在给定方向上是否具有 通过用户发起滚动即可到达的、已裁剪的可滚动溢出矩形内容。 换言之,scrollable 不会匹配 容器,也不会匹配 negative scrollable overflow region。
这些逻辑值会基于查询容器 的方向与写字模式映射为物理方向。若该容器不是滚动容器, 则任何值都不匹配。
- none
- 滚动容器在任何方向上都没有可滚动溢出。
- top
- 滚动容器在顶部边缘之外有可滚动溢出。
- right
- 滚动容器在右侧边缘之外有可滚动溢出。
- bottom
- 滚动容器在底部边缘之外有可滚动溢出。
- left
- 滚动容器在左侧边缘之外有可滚动溢出。
- block-start
- 滚动容器在block-start 边缘之外有可滚动溢出。
- inline-start
- 滚动容器在inline-start 边缘之外有可滚动溢出。
- block-end
- 滚动容器在block-end 边缘之外有可滚动溢出。
- inline-end
- 滚动容器在inline-end 边缘之外有可滚动溢出。
- x
- 滚动容器在水平方向上存在可滚动溢出。
- y
- 滚动容器在垂直方向上存在可滚动溢出。
- block
- 滚动容器在其块方向上存在可滚动溢出。
- inline
- 滚动容器在其行内方向上存在可滚动溢出。
6.3.5. 已滚动:scrolled 特性
| 名称: | scrolled |
|---|---|
| 适用于: | @container |
| 值: | none | top | right | bottom | left | block-start | inline-start | block-end | inline-end | x | y | block | inline |
| 类型: | discrete |
测试
- scroll-state-scrolled-arrow-key-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-home-end-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-hv.html (在线 测试) (源码)
- scroll-state-scrolled-keyboard-scroll-on-body.html (在线 测试) (源码)
- scroll-state-scrolled-keyboard-scroll-on-root.html (在线 测试) (源码)
- scroll-state-scrolled-mouse-drag-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-multiple-scrollers.html (在线 测试) (源码)
- scroll-state-scrolled-programmatic-absolute-scrolls.html (在线 测试) (源码)
- scroll-state-scrolled-programmatic-relative-scrolls.html (在线 测试) (源码)
- scroll-state-scrolled-pu-pd-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-scrollbar-button-clicks.html (在线 测试) (源码)
- scroll-state-scrolled-scrollbar-track-clicks.html (在线 测试) (源码)
- scroll-state-scrolled-spacebar-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-user-touch-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-wheel-scroll.html (在线 测试) (源码)
- scroll-state-scrolled-wm.html (在线 测试) (源码)
- at-container-scrolled-parsing.html (在线 测试) (源码)
- at-container-scrolled-serialization.html (在线 测试) (源码)
对于一个查询容器, 若其是滚动容器, 则scrolled 容器特性用于查询其最近一次相对滚动的方向。 这些逻辑值会基于查询容器的方向与写字模式映射为物理方向。 若该容器不是滚动容器,则任何值都不匹配。
- none
- 查询容器 尚未发生过相对滚动。
- top
- 最近一次相对滚动方向为向上。
- right
- 最近一次相对滚动方向为向右。
- bottom
- 最近一次相对滚动方向为向下。
- left
- 最近一次相对滚动方向为向左。
- block-start
- 最近一次相对滚动方向指向block-start。
- inline-start
- 最近一次相对滚动方向指向inline-start。
- block-end
- 最近一次相对滚动方向指向block-end。
- inline-end
- 最近一次相对滚动方向指向inline-end。
- x
- 最近一次相对滚动发生在水平方向。
- y
- 最近一次相对滚动发生在垂直方向。
- block
- 最近一次相对滚动发生在块方向。
- inline
- 最近一次相对滚动发生在行内方向。
7. 容器相对长度:cqw、cqh、cqi、cqb、cqmin、cqmax 单位
容器查询长度单位 指定一个相对于查询容器尺寸的长度。 使用容器查询长度单位的样式表可以更方便地将组件从一个查询容器迁移到另一个容器。
容器查询长度单位包括:
| 单位 | 相对对象 |
|---|---|
| cqw | 查询容器的宽度的1% |
| cqh | 查询容器的高度的1% |
| cqi | 查询容器的行内尺寸的1% |
| cqb | 查询容器的块级尺寸的1% |
| cqmin | cqi 和 cqb 中较小的值 |
| cqmax | cqi 和 cqb 中较大的值 |
对于每个元素,容器查询长度单位的计算 作为对应轴(或多轴)上的容器尺寸查询进行。 每个轴的查询容器 是最近的祖先中在该轴上支持容器尺寸查询的容器。 如果在某个轴上没有可用的查询容器,则使用该轴的小视口尺寸。
注意: 某些情况下,同一元素上的cqi 和 cqb单位 可能是针对不同的查询容器进行计算。 同样,cqmin 和 cqmax 单位 分别代表 cqi 和 cqb 的较大或较小值, 即便这两个尺寸来自不同的查询容器。
子元素不会继承父元素指定的相对值; 它们继承的是计算值。
/* 回退值不依赖于容器性 */ h2{ font-size : 1.2 em ; } @container ( inline-size >=0 px ) { /* 仅在有 inline-size 容器可用时生效 */ h2{ font-size : calc ( 1.2 em +1 cqi ); } }
8. 定义自定义支持查询:@supports-condition 规则
@supports-condition at-规则 是一种条件分组规则,允许作者定义并命名一个supports 查询以便后续复用, 创建一个命名支持条件。 这样可以用名称引用复杂或常用的特性查询,提高可维护性和可读性。
@supports-condition <supports-condition-name>{ <block-contents>}
测试
其中 <supports-condition-name> 是定义 supports 查询名称的<extension-name>。
块内的所有内容都将被评估以测试用户代理是否支持相关特性。 这些内容不会影响文档的实际渲染。
一旦定义,命名支持条件即可在后续的@supports 或 @when 条件中使用。
如果定义了多个同名的@supports-condition规则, 则以文档顺序最后一个为准,之前的都被忽略。
@supports-condition --thicker-underlines{ text-decoration-thickness : 0.2 em ; text-underline-offset : 0.3 em ; } /* 等价于 (text-decoration-thickness: 0.2em) and (text-underline-offset: 0.3em) */ @supports ( --thicker-underlines) { a{ text-decoration : underline; text-decoration-thickness : 0.2 em ; text-underline-offset : 0.3 em ; } }
@supports-condition 规则允许出现在 @import 和 @namespace 规则之前(若有 @charset 规则,则应在其后)。
@supports-condition --nesting{ &{ } } @import url ( "nested-styles.css" ) supports ( --nesting);
@supports-condition --stuck-container-feature{ @container scroll-state ( stuck: top) { } } @supports ( --stuck-container-feature) { div{ border-color : navy; } }
at-规则名称尚在讨论中。 备选方案包括 @supports-query、@supports-test 和 @custom-supports。 名称应与自定义媒体查询保持一致。
9. API
9.1.
CSSContainerRule 接口
CSSContainerRule
接口表示一个@container 规则。
[Exposed =Window ]interface :CSSContainerRule CSSConditionRule {readonly attribute CSSOMString ;containerName readonly attribute CSSOMString ; };containerQuery
conditionText类型为CSSOMString(CSSContainerRule 对CSSConditionRule上属性的专属定义)-
conditionText属性(定义于父类CSSConditionRule)的取值如下:- 若@container 规则有关联的 <container-name>
- 返回
containerName和containerQuery属性的值,用空格拼接。 - 否则
- 返回
containerQuery属性值。
containerName类型为CSSOMString-
containerName属性取值如下:- 若@container 规则有关联的 <container-name>
- 返回序列化后的 <container-name>。
- 否则
- 返回空字符串。
containerQuery类型为CSSOMStringcontainerQuery属性的返回值为 指定的 <container-query>, 不做任何逻辑简化, 保证返回的查询在任何符合规范的实现中 (包括实现本规范允许的未来扩展的实现,例如 <general-enclosed> 扩展机制)都能得到相同的结果。 换言之, 可以做 token 流简化(如多空格合并为一个空格、在可选空格处省略), 但不得做逻辑简化(如去掉多余括号或基于结果做简化)。
容器查询应有 matchContainer 方法。
该方法将以 matchMedia()
和 MediaQueryList
接口为模型,
但应用于 Element 而不是 Window。
在测量布局尺寸时,其行为类似 resizeObserver,
但还提供额外的容器查询语法和特性。[Issue #6205]
9.2.
CSSSupportsConditionRule 接口
CSSSupportsConditionRule
接口表示一个@supports-condition 规则。
[Exposed =Window ]interface :CSSSupportsConditionRule CSSGroupingRule {readonly attribute CSSOMString ; };name
name类型为CSSOMString- 该属性为命名支持条件的名称。
安全性说明
目前没有针对本文件提出安全性问题。
隐私说明
font-tech() 和 font-format() 函数 可能会泄露用户软件的信息,比如其版本、 以及是否启用了或禁用了某些功能的非默认设置。
这些信息也可以通过其他 API 获得。 但本规范中的特性是信息在 Web 上披露的方式之一。
这些信息也可能被聚合后用于提升对用户指纹识别的准确性。
变更记录
自2024年11月5日工作草案以来的变更
- 明确了文档中最后一个 @supports-condition 规则优先生效 (#12973)
- 扩展supports 查询以通过at-rule()表达at-规则能力 (#2463, #6966, #11116, #11117)
- 新增@supports-condition at-规则及相关
CSSSupportsConditionRule接口。 (#12622) - 明确 container-name 不是树作用域 (#12090)
- 为 scroll-state() 查询定义 direction 特性 (#6400)
- 明确 0 和 0px 在条件中等价 (#12236)
- 为样式容器查询定义了范围语法 (#8376)
- 明确允许树计数函数 (#10982)
- 尺寸查询容器不再应用布局包含 (#10544)
- 为 snapped 查询添加 "both" 值 (#11181)
- 为 overflowing 添加轴关键字 (#11183)
- 将 overflowing 重命名为 scrollable (#11182)
- 使 <container-query> 可选 (#9192)
自2024年7月23日工作草案以来的变更
- 为 scroll-state() 特性添加了 none 关键字 (#10874)
- 添加 container-type:scroll-state 和 stuck、snapped、scrollable 特性的 scroll-state() 查询 (#6402, #10784, #10796)
- 修正示例(不存在 container-type:style)
- 规定容器查询使用 flat tree (#5984)
自2021年12月21日首个公开工作草案以来的变更
- 将容器查询从 CSS Contain 3 移到本规范(#10433)
- 引用 CSS Fonts 4 中 <font-format> 和 <font-tech> 的定义,而不是在本规范中重复定义(#8110)
- 更新为使用新的解析算法名称和块产生式名称
- 修正了 <font-format> 语法中的拼写错误
- 修正了 font-tech 和 font-format 产生式中的多余空格(#7369)
Level 4 以来的新增内容
- 新增 @when 和 @else。
- 扩展supports 查询以通过font-tech() 和 font-format() 表达字体能力
- 扩展supports 查询以通过at-rule()表达 at-规则能力
- 将容器查询从[CSS-CONTAIN-3] 移入本规范。 (详见 CSS Containment 3 § A 变更,了解本特性的演进)
- 新增@supports-condition at-规则及相关
CSSSupportsConditionRule接口。
致谢
@when 和 @else 规则 基于 Tab Atkins 的提案。
感谢 Adam Argyle、 Amelia Bellamy-Royds、 Anders Hartvoll Ruud、 Brian Kardell、 Chris Coyier、 Christopher Kirk-Nielsen、 David Herron、 Eric Portis、 Ethan Marcotte、 Florian Rivoal、 Geoff Graham、 Gregory Wild-Smith、 Ian Kilpatrick、 Jen Simmons、 Kenneth Rohde Christiansen、 Lea Verou、 Martin Auswöger、 Martine Dowden、 Mike Riethmuller、 Morten Stenshorne、 Nicole Sullivan、 Rune Lillesveen、 Scott Jehl、 Scott Kellum、 Stacy Kvernmo、 Theresa O’Connor、 Una Kravets 以及许多其他人为本规范做出的评论和贡献。