CSSRule

This translation is incomplete. Please help translate this article from English

CSSRule 对象代表一个单一的CSS规则。There are several types of rules, listed in the Type constants section below.

The CSSRule interface specifies the properties common to all rules, while properties unique to specific rule types are specified in the more specialized interfaces for those rules' respective types.

可以通过 CSSStyleSheetcssRules 列表了解更多关于 CSSRule 的介绍。

所有 CSSRule 实例共有的属性

CSSRule.cssText
返回规则的文本表示. 例如 "h1,h2 { font-size: 16pt }"
CSSRule.parentRule 只读
返回包含规则,否则返回 null。例如:如果此规则是 @media 块中的样式规则, 则其父规则将是该 CSSMediaRule
CSSRule.parentStyleSheet 只读
返回包含此规则的样式表的 CSSStyleSheet 对象。
CSSRule.type 只读
规则类型,表示 CSS 规则类型 Type constants 中的一种类型。

常量

类型常量

The CSSRule interface specifies integer constants that can be used in conjunction with a CSSRule's type property to discern the rule type (and therefore, which specialized interface it implements). The relationships between these constants and the interfaces are:

类型 对应接口 备注与示例
CSSRule.STYLE_RULE 1 CSSStyleRule The most common kind of rule:
selector { prop1: val1; prop2: val2; }
CSSRule.IMPORT_RULE 3 CSSImportRule An @import rule. (Until the documentation is completed, see the interface definition in the Mozilla source code: nsIDOMCSSImportRule.)
CSSRule.MEDIA_RULE 4 CSSMediaRule
CSSRule.FONT_FACE_RULE 5 CSSFontFaceRule
CSSRule.PAGE_RULE 6 CSSPageRule
CSSRule.KEYFRAMES_RULE 7 CSSKeyframesRule
CSSRule.KEYFRAME_RULE 8 CSSKeyframeRule
Reserved for future use 9 Should be used to define color profiles in the future
CSSRule.NAMESPACE_RULE 10 CSSNamespaceRule
CSSRule.COUNTER_STYLE_RULE 11 CSSCounterStyleRule
CSSRule.SUPPORTS_RULE 12 CSSSupportsRule
CSSRule.DOCUMENT_RULE 13 CSSDocumentRule
CSSRule.FONT_FEATURE_VALUES_RULE 14 CSSFontFeatureValuesRule
CSSRule.VIEWPORT_RULE 15 CSSViewportRule
CSSRule.REGION_STYLE_RULE 16 CSSRegionStyleRule
CSSRule.UNKNOWN_RULE 0 CSSUnknownRule
CSSRule.CHARSET_RULE 2 CSSCharsetRule (Removed in most browsers.)

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;
};

规范

Specification Status Comment
CSS Object Model (CSSOM)
CSSRule
Working Draft Obsoleted values CHARSET_RULE and UNKNOWN_RULE. Added value NAMESPACE_RULE.
CSS Animations
CSSRule
Working Draft Added values KEYFRAMES_RULE and KEYFRAME_RULE.
CSS Fonts Module Level 3
CSSRule
Candidate Recommendation Added value FONT_FEATURE_VALUES_RULE.
CSS Counter Styles Level 3
CSSRule
Candidate Recommendation Added value COUNTER_STYLE_RULE.
CSS Conditional Rules Module Level 3
CSSRule
Candidate Recommendation Added value SUPPORTS_RULE. (DOCUMENT_RULE has been pushed to CSS Conditional Rules Level 4)
Document Object Model (DOM) Level 2 Style Specification
CSSRule
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
CSSRuleChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0
cssTextChrome Full support 1Edge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0
parentRuleChrome Full support 1Edge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0
parentStyleSheetChrome Full support 1Edge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0
typeChrome Full support 1Edge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

相关