--- title: CSSRule slug: Web/API/CSSRule tags: - API - CSSOM - 参考 - 接口 translation_of: Web/API/CSSRule ---
CSSRule
接口表示一条 CSS 规则。有几种不同的规则类型,在下面的{{anch("类型常量")}}部分中有悉数列出。
CSSRule
接口指定了所有类型的规则的公共属性,而特定类型的规则的专有属性则在这些规则各自类型的、更专用的接口中被指定。
可以通过 {{domxref("CSSStyleSheet")}} 的 cssRules
列表了解更多关于 CSSRule
的介绍。
"h1,h2 { font-size: 16pt }"
null
。例如:如果此规则是 {{cssxref("@media")}} 块中的样式规则, 则其父规则将是该 {{domxref("CSSMediaRule")}}。CSSRule接口通过一系列整型常量来约束CSSRule的
{{domxref("cssRule/type","type")}}取值范围,同时这些常量也对应规则的具体实现接口。这些常量和接口的对应关系如下:
类型 | 值 | 对应接口 | 备注与示例 |
---|---|---|---|
CSSRule.STYLE_RULE |
1 |
{{domxref("CSSStyleRule")}} | 最常见的一种规则。selector { prop1: val1; prop2: val2; } |
CSSRule.IMPORT_RULE |
3 |
{{domxref("CSSImportRule")}} | 一条 {{cssxref("@import")}} 规则。(Until the documentation is completed, see the interface definition in the Mozilla source code: nsIDOMCSSImportRule.) |
CSSRule.MEDIA_RULE |
4 |
{{domxref("CSSMediaRule")}} | |
CSSRule.FONT_FACE_RULE |
5 |
{{domxref("CSSFontFaceRule")}} | |
CSSRule.PAGE_RULE |
6 |
{{domxref("CSSPageRule")}} | |
CSSRule.KEYFRAMES_RULE |
7 |
{{domxref("CSSKeyframesRule")}} {{experimental_inline}} | |
CSSRule.KEYFRAME_RULE |
8 |
{{domxref("CSSKeyframeRule")}} {{experimental_inline}} | |
Reserved for future use | 9 |
应当会在将来被用于定义颜色配置 | |
CSSRule.NAMESPACE_RULE |
10 |
{{domxref("CSSNamespaceRule")}} {{experimental_inline}} | |
CSSRule.COUNTER_STYLE_RULE |
11 |
{{domxref("CSSCounterStyleRule")}} {{experimental_inline}} | |
CSSRule.SUPPORTS_RULE |
12 |
{{domxref("CSSSupportsRule")}} | |
CSSRule.DOCUMENT_RULE |
13 |
{{domxref("CSSDocumentRule")}} {{experimental_inline}} | |
CSSRule.FONT_FEATURE_VALUES_RULE |
14 |
{{domxref("CSSFontFeatureValuesRule")}} | |
CSSRule.VIEWPORT_RULE |
15 |
{{domxref("CSSViewportRule")}} {{experimental_inline}} | |
CSSRule.REGION_STYLE_RULE |
16 |
{{domxref("CSSRegionStyleRule")}} {{experimental_inline}} | |
CSSRule.UNKNOWN_RULE |
0 |
{{domxref("CSSUnknownRule")}} {{obsolete_inline}} | |
CSSRule.CHARSET_RULE |
2 |
CSSCharsetRule {{obsolete_inline}} |
(已在大多数浏览器中被移除) |
An up-to-date informal list of constants can be found on the CSSWG Wiki.
使用 WebIDL 语法格式进行描述。
interface CSSRule { const unsigned short STYLE_RULE = 1; const unsigned short CHARSET_RULE = 2; const unsigned short IMPORT_RULE = 3; const unsigned short MEDIA_RULE = 4; const unsigned short FONT_FACE_RULE = 5; const unsigned short PAGE_RULE = 6; const unsigned short KEYFRAMES_RULE = 7; const unsigned short KEYFRAME_RULE = 8; const unsigned short NAMESPACE_RULE = 10; const unsigned short COUNTER_STYLE_RULE = 11; const unsigned short SUPPORTS_RULE = 12; const unsigned short DOCUMENT_RULE = 13; const unsigned short FONT_FEATURE_VALUES_RULE = 14; const unsigned short VIEWPORT_RULE = 15; const unsigned short REGION_STYLE_RULE = 16; readonly attribute unsigned short type; attribute DOMString cssText; readonly attribute CSSRule? parentRule; readonly attribute CSSStyleSheet? parentStyleSheet; };
规范 | 状态 | 备注 |
---|---|---|
{{SpecName('CSSOM', '#css-rules', 'CSSRule')}} | {{Spec2('CSSOM')}} | Obsoleted values CHARSET_RULE and UNKNOWN_RULE . Added value NAMESPACE_RULE . |
{{SpecName("CSS3 Animations",'#interface-cssrule', 'CSSRule')}} | {{Spec2('CSS3 Animations')}} | Added values KEYFRAMES_RULE and KEYFRAME_RULE . |
{{SpecName('CSS4 Fonts', '#om-fontfeaturevalues', 'CSSRule')}} | {{Spec2('CSS4 Fonts')}} | Added value FONT_FEATURE_VALUES_RULE . |
{{SpecName("CSS3 Counter Styles", "#extentions-to-cssrule-interface", 'CSSRule')}} | {{Spec2("CSS3 Counter Styles")}} | Added value COUNTER_STYLE_RULE . |
{{SpecName("CSS3 Conditional", '#extentions-to-cssrule-interface', 'CSSRule')}} | {{Spec2('CSS3 Conditional')}} | Added value SUPPORTS_RULE . (DOCUMENT_RULE has been pushed to CSS Conditional Rules Level 4) |
{{SpecName('DOM2 Style', 'css.html#CSS-CSSRule', 'CSSRule')}} | {{Spec2('DOM2 Style')}} | Initial definition. |
{{Compat("api.CSSRule")}}