大网数据广告活动
Web 标准与前端开发

CSSNestedDeclarations

· 52CE 我爱测

The **`CSSNestedDeclarations`** interface of the CSS Rule API is used to group nested CSSRules.

CSSOM

The CSSNestedDeclarations interface of the CSS Rule API is used to group nested CSSRules.

The interface allows the CSS Object Model (CSSOM) to mirror the structure of CSS documents with nested CSS rules, and ensure that rules are parsed and evaluated in the order that they are declared.

[!NOTE] Implementations that do not support this interface may parse nested rules in the wrong order. See Browser compatibility for more information.

Instance properties

Inherits properties from its ancestor CSSRule.

  • CSSNestedDeclarations.style
    • : Returns the values of the nested rules.

Instance methods

No specific methods; inherits methods from its ancestor CSSRule.

Examples

CSS

The CSS below includes a selector .foo that contains two declarations and a media query.

.foo {
  background-color: silver;
  @media screen {
    color: tomato;
  }
  color: black;
}

This is represented by a number of JavaScript objects in the CSS Object Model:

  • A CSSStyleRule object that represents the background-color: silver rule. This can be returned via document.styleSheets[0].cssRules[0].
  • A CSSMediaRule object that represents the @media screen rule, and which can be returned via document.styleSheets[0].cssRules[0].cssRules[0].
    • The CSSMediaRule object contains a CSSNestedDeclaration object which represents the color: tomato rule nested by the @media screen rule. This can be returned via document.styleSheets[0].cssRules[0].cssRules[0].cssRules[0].
  • The final rule is a CSSNestedDeclaration object that represents the color: black rule in the stylesheet, and which can be returned via document.styleSheets[0].cssRules[0].cssRules[1].

[!NOTE] All top-level styles after the first CSSNestedDeclaration must also be represented as CSSNestedDeclaration objects in order to follow the CSS nested declarations rule

CSSOM (CSS Object Model)

↳ CSSStyleRule
  .style
    - background-color: silver
  ↳ CSSMediaRule
    ↳ CSSNestedDeclarations
      .style (CSSStyleDeclaration, 1) =
      - color: tomato
  ↳ CSSNestedDeclarations
    .style (CSSStyleDeclaration, 1) =
      - color: black

Specifications

Browser compatibility

See Also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。