CSSRule

The CSSRule interface represents a single CSS rule. There are several types of rules which inherit properties from CSSRule.

Properties common to all CSSRule instances

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.

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 This deprecated API should no longer be used, but will probably still work.
Returns one of the Type constants to determine which type of rule is represented.

Examples

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

let myRules = document.styleSheets[0].cssRules; // Returns a CSSRuleList
console.log(myRules);

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. Deprecates CSSRule.type.
CSS Animations Level 1
The definition of 'CSSRule' in that specification.
Working Draft Added values KEYFRAMES_RULE and KEYFRAME_RULE.
CSS Fonts Module Level 4
The definition of 'CSSRule' in that specification.
Working Draft Added value FONT_FEATURE_VALUES_RULE.
CSS Counter Styles Level 3
The definition of 'CSSRule' in that specification.
Candidate Recommendation Added value COUNTER_STYLE_RULE.
CSS Conditional Rules Module Level 3
The 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 Specification
The definition of 'CSSRule' in that specification.
Obsolete Initial definition.

Browser compatibility

BCD tables only load in the browser

See also