CSSLayerStatementRule

Baseline 已广泛支持

此特性已经十分成熟,可在许多设备和浏览器版本上使用。自 2022 年 3 月起,它已在各浏览器中可用。

CSSLayerStatementRule 表示一个 @layer 语句规则。与 CSSLayerBlockRule 不同,它不包含其他规则,仅仅通过提供名称来定义一个或多个层。

此规则允许显式声明 CSS 文件开头处以明显方式出现的层顺序:层顺序由每个层名称首次出现的顺序定义。通过语句声明它们允许读者理解层顺序。它还允许内联层和导入的层交错,这在使用 CSSLayerBlockRule 语法时是不可能的。

CSSRule CSSLayerStatementRule

实例属性

还继承了其父接口 CSSRule 的属性。

示例

HTML

html
<p></p>

CSS

css
@layer layerName, layerName2;

JavaScript

js
const item = document.getElementsByTagName("p")[0];
const rules = document.styleSheets[1].cssRules;
// Note that stylesheet #1 is the stylesheet associated with this embedded example,
// while stylesheet #0 is the stylesheet associated with the whole MDN page

const layer = rules[0]; // A CSSLayerStatementRule

item.textContent = `The CSS @layer statement declares the following layers: ${layer.nameList.join(
  ", ",
)}.`;

结果

规范

规范
CSS 级联与继承第五级
# csslayerstatementrule

浏览器兼容性

另见