An object implementing the CSSRule DOM interface represents a single CSS at-rule. References to a CSSRule-implementing object may be obtained by looking at a CSS style sheet's cssRules (en-US) list.

There are several kinds of rules. 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.


The syntax is described using the WebIDL format.

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;

Properties common to all CSSRule instances

Represents the textual representation of the rule, e.g. "h1,h2 { font-size: 16pt }"
CSSRule.parentRule (en-US) Schreibgeschützt
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 (en-US) Schreibgeschützt
Returns the CSSStyleSheet (en-US) object for the style sheet that contains this rule
CSSRule.type (en-US) Schreibgeschützt
One of the Type constants indicating the type of CSS rule.


Type constants

The CSSRule interface specifies integer constants that can be used in conjunction with a CSSRule's type (en-US) 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
CSSRule.STYLE_RULE 1 CSSStyleRule (en-US)
CSSRule.FONT_FACE_RULE 5 CSSFontFaceRule (en-US)
CSSRule.IMPORT_RULE 3 CSSImportRule (en-US) : IDL: nsIDOMCSSImportRule
CSSRule.CHARSET_RULE 2 CSSCharsetRule Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
CSSRule.UNKNOWN_RULE 0 CSSUnknownRule Dies ist eine veraltete API und es wird nicht mehr garantiert, dass sie funktioniert.
CSSRule.KEYFRAMES_RULE 7 CSSKeyframesRule (en-US) [1] Experimental
CSSRule.KEYFRAME_RULE 8 CSSKeyframeRule (en-US) [1] Experimental
Reserved for future use 9 Should be used to define color profiles in the future
CSSRule.NAMESPACE_RULE 10 CSSNamespaceRule (en-US) Experimental
CSSRule.COUNTER_STYLE_RULE 11 CSSCounterStyleRule (en-US) Experimental
CSSRule.SUPPORTS_RULE 12 CSSSupportsRule (en-US)
CSSRule.DOCUMENT_RULE 13 CSSDocumentRule Experimental
CSSRule.VIEWPORT_RULE 15 CSSViewportRule Experimental
CSSRule.REGION_STYLE_RULE 16 CSSRegionStyleRule Experimental

An up-to-date informal list of constants can be found on the CSSWG Wiki.

[1] On Gecko, before Firefox 19, the keyframe-related constants existed only in their prefixed version: CSSRule.MOZ_KEYFRAMES_RULE and CSSRule.MOZ_KEYFRAME_RULE. From Firefox 20 onwards, both versions are available. The prefixed version will be removed in the future.


Specification Status Comment
CSS Object Model (CSSOM)
Die Definition von 'CSSRule' in dieser Spezifikation.
Arbeitsentwurf Obsoleted values CHARSET_RULE and UNKNOWN_RULE. Added value NAMESPACE_RULE.
CSS Animations Level 1
Die Definition von 'CSSRule' in dieser Spezifikation.
Arbeitsentwurf Added values KEYFRAMES_RULE and KEYFRAME_RULE.
CSS Fonts Module Level 3
Die Definition von 'CSSRule' in dieser Spezifikation.
Empfehlung Added value FONT_FEATURE_VALUES_RULE.
CSS Counter Styles Level 3
Die Definition von 'CSSRule' in dieser Spezifikation.
Anwärter Empfehlung Added value COUNTER_STYLE_RULE.
CSS Conditional Rules Module Level 3
Die Definition von 'CSSRule' in dieser Spezifikation.
Anwärter Empfehlung Added value SUPPORTS_RULE. (DOCUMENT_RULE has been pushed to CSS Conditional Rules Level 4)
Document Object Model (DOM) Level 2 Style Specification
Die Definition von 'CSSRule' in dieser Spezifikation.
Veraltet Initial definition.

Browser compatibility

To get browser compatibility information about the different type constant value, please consult the compatibility table of the associated interface.

Wir konvertieren die Kompatibilitätsdaten in ein maschinenlesbares JSON Format. Diese Kompatibilitätstabelle liegt noch im alten Format vor, denn die darin enthaltenen Daten wurden noch nicht konvertiert. Finde heraus wie du helfen kannst! (en-US)
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Ja) (Ja) 9 (Ja) (Ja)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Ja) (Ja) (Ja) (Ja) (Ja)

See also