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

瀏覽器相容性

另見