W3cubDocs

/Web APIs

CSSRule: type property

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The read-only type property of the CSSRule interface is a deprecated property that returns an integer indicating which type of rule the CSSRule represents.

If you need to distinguish different types of CSS rule, a good alternative is to use constructor.name:

js

const sheets = Array.from(document.styleSheets);
const rules = sheets.map((sheet) => Array.from(sheet.cssRules)).flat();

for (const rule of rules) {
  console.log(rule.constructor.name);
}

Value

CSSRule.STYLE_RULE (1)

The rule is a CSSStyleRule, the most common kind of rule: selector { prop1: val1; prop2: val2; }.

CSSRule.IMPORT_RULE (3)

The rule is a CSSImportRule and represents an @import rule.

CSSRule.MEDIA_RULE (4)

The rule is a CSSMediaRule.

CSSRule.FONT_FACE_RULE (5)

The rule is a CSSFontFaceRule

CSSRule.PAGE_RULE (6)

The rule is a CSSPageRule.

CSSRule.KEYFRAMES_RULE (7)

The rule is a CSSKeyframesRule.

CSSRule.KEYFRAME_RULE (8)

The rule is a CSSKeyframeRule.

CSSRule.NAMESPACE_RULE (10)

The rule is a CSSNamespaceRule.

CSSRule.COUNTER_STYLE_RULE (11)

The rule is a CSSCounterStyleRule.

CSSRule.SUPPORTS_RULE (12)

The rule is a CSSSupportsRule.

CSSRule.DOCUMENT_RULE (13)

The rule is a CSSDocumentRule.

CSSRule.FONT_FEATURE_VALUES_RULE (14)

The rule is a CSSFontFeatureValuesRule.

CSSRule.VIEWPORT_RULE (15)

The rule is a CSSViewportRule.

CSSRule.REGION_STYLE_RULE (16)

The rule is a CSSRegionStyleRule.

Both CSSRule.UNKNOWN_RULE (0) and CSSRule.CHARSET_RULE (2) are deprecated and cannot be obtained any more

Examples

js

let myRules = document.styleSheets[0].cssRules;
console.log(myRules[0].type);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
type 1 12 1 9 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CSSRule/type