Web/API/CSSRule

From Get docs


The CSSRule interface represents a single CSS rule. 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.

References to a CSSRule may be obtained by looking at a CSSStyleSheet's cssRules list.

Properties common to all CSSRule instances

CSSRule.cssText
Represents the textual representation of the rule, e.g. "h1,h2 { font-size: 16pt }" or "@import 'url'". To access or modify parts of the rule (e.g. the value of "font-size" in the example) use the properties on the specialized interface for the rule's type.
CSSRule.parentRule Read only
Returns the containing rule, otherwise null. E.g. if this rule is a style rule inside an @media block, the parent rule would be that CSSMediaRule.
CSSRule.parentStyleSheet Read only
Returns the CSSStyleSheet object for the style sheet that contains this rule
CSSRule.type Read only
One of the Type constants indicating the type of CSS rule.

Constants

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:

Type Value Rule-specific interface Comments and examples
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.

Specifications

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

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
CSSRule Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 9

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

cssText Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE Full support Yes

Notes'

Full support Yes

Notes'

Notes' The property normalizes rules and does not return the original verbatim rule text. For example, border-width: 1px; border-style:solid; border-color:white; returns border: 1px solid white;.

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

parentRule Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

?

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

parentStyleSheet Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

?

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

type Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

?

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.'
See implementation notes.


See also

CSSRule by Mozilla Contributors is licensed under CC-BY-SA 2.5.