Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1065844 of CSSRule

  • Revision slug: Web/API/CSSRule
  • Revision title: CSSRule
  • Revision id: 1065844
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("CSSOM")}}

The CSSRule interface represents a single CSS rule. References to a CSSRule may be obtained by looking at a {{domxref("CSSStyleSheet")}}'s cssRules 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. See {{anch("Type constants")}} below for the list of rule types.

Properties common to all CSSRule instances

{{domxref("CSSRule.cssText")}}
Represents the textual representation of the rule, e.g. "h1,h2 { font-size: 16pt }"
{{domxref("CSSRule.parentRule")}} {{readonlyinline}}
Returns the containing rule, otherwise null. E.g. if this rule is a style rule inside an {{cssxref("@media")}} block, the parent rule would be that {{domxref("CSSMediaRule")}}.
{{domxref("CSSRule.parentStyleSheet")}} {{readonlyinline}}
Returns the {{domxref("CSSStyleSheet")}} object for the style sheet that contains this rule
{{domxref("CSSRule.type")}} {{readonlyinline}}
One of the {{anch("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 {{domxref("cssRule/type","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 {{domxref("CSSStyleRule")}} The most common kind of rule:
selector { prop1: val1; prop2: val2; }
CSSRule.IMPORT_RULE 3 {{domxref("CSSImportRule")}} An {{cssxref("@import")}} rule. (Until the documentation is completed, see the interface definition in the Mozilla source code: nsIDOMCSSImportRule.)
CSSRule.MEDIA_RULE 4 {{domxref("CSSMediaRule")}}  
CSSRule.FONT_FACE_RULE 5 {{domxref("CSSFontFaceRule")}}  
CSSRule.PAGE_RULE 6 {{domxref("CSSPageRule")}}  
CSSRule.KEYFRAMES_RULE 7 {{domxref("CSSKeyframesRule")}} {{experimental_inline}}  
CSSRule.KEYFRAME_RULE 8 {{domxref("CSSKeyframeRule")}} {{experimental_inline}}  
Reserved for future use 9   Should be used to define color profiles in the future
CSSRule.NAMESPACE_RULE 10 {{domxref("CSSNamespaceRule")}} {{experimental_inline}}  
CSSRule.COUNTER_STYLE_RULE 11 {{domxref("CSSCounterStyleRule")}} {{experimental_inline}}  
CSSRule.SUPPORTS_RULE 12 {{domxref("CSSSupportsRule")}}  
CSSRule.DOCUMENT_RULE 13 {{domxref("CSSDocumentRule")}} {{experimental_inline}}  
CSSRule.FONT_FEATURE_VALUES_RULE 14 {{domxref("CSSFontFeatureValuesRule")}}  
CSSRule.VIEWPORT_RULE 15 {{domxref("CSSViewportRule")}} {{experimental_inline}}  
CSSRule.REGION_STYLE_RULE 16 {{domxref("CSSRegionStyleRule")}} {{experimental_inline}}  
CSSRule.UNKNOWN_RULE 0 {{domxref("CSSUnknownRule")}} {{obsolete_inline}}  
CSSRule.CHARSET_RULE 2 CSSCharsetRule {{obsolete_inline}} (Removed in most browsers.)

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

Specifications

Specification Status Comment
{{SpecName('CSSOM', '#css-rules', 'CSSRule')}} {{Spec2('CSSOM')}} Obsoleted values CHARSET_RULE and UNKNOWN_RULE. Added value NAMESPACE_RULE.
{{SpecName("CSS3 Animations",'#interface-cssrule', 'CSSRule')}} {{Spec2('CSS3 Animations')}} Added values KEYFRAMES_RULE and KEYFRAME_RULE.
{{SpecName('CSS3 Fonts', '#om-fontfeaturevalues', 'CSSRule')}} {{Spec2('CSS3 Fonts')}} Added value FONT_FEATURE_VALUES_RULE.
{{SpecName("CSS3 Counter Styles", "#extentions-to-cssrule-interface", 'CSSRule')}} {{Spec2("CSS3 Counter Styles")}} Added value COUNTER_STYLE_RULE.
{{SpecName("CSS3 Conditional", '#extentions-to-cssrule-interface', 'CSSRule')}} {{Spec2('CSS3 Conditional')}} Added value SUPPORTS_RULE. (DOCUMENT_RULE has been pushed to CSS Conditional Rules Level 4)
{{SpecName('DOM2 Style', 'css.html#CSS-CSSRule', 'CSSRule')}} {{Spec2('DOM2 Style')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} 9 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
CSSKeyframesRule {{CompatUnknown}} 20.0 [4] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
CSSCharsetRule {{CompatNo}} [1] {{CompatNo}} [3] {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}}
CSSUnknownRule {{CompatNo}} [2] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
CSSKeyframesRule {{CompatUnknown}} {{CompatUnknown}} 20.0 [4] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
CSSCharsetRule {{CompatUnknown}} {{CompatNo}} [1] {{CompatNo}} [3] {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}} [1]
CSSUnknownRule {{CompatUnknown}} {{CompatNo}} [2] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatNo}} [2]

[1] CSSCharsetRule was removed in Chrome version 41.0.

[2] CSSUnknownRule was removed in Chrome version 45.0.

[3] CSSCharsetRule was removed in Gecko version 40.

[4] 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.

See also

Revision Source

<div>{{APIRef("CSSOM")}}</div>

<p>The <strong><code>CSSRule</code></strong> interface represents a single CSS rule. References to a <code>CSSRule</code> may be obtained by looking at a {{domxref("CSSStyleSheet")}}'s <code>cssRules</code> list.</p>

<p>There are several kinds of rules. The <code>CSSRule</code> 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. See {{anch("Type constants")}} below for the list of rule types.</p>

<h2 id="Properties_common_to_all_CSSRule_instances">Properties common to all CSSRule instances</h2>

<dl>
 <dt id="cssText">{{domxref("CSSRule.cssText")}}</dt>
 <dd>Represents the textual representation of the rule, e.g. <code>"h1,h2 { font-size: 16pt }"</code></dd>
 <dt id="parentRule">{{domxref("CSSRule.parentRule")}} {{readonlyinline}}</dt>
 <dd>Returns the containing rule, otherwise <code>null</code>. E.g. if this rule is a style rule inside an {{cssxref("@media")}} block, the parent rule would be that {{domxref("CSSMediaRule")}}.</dd>
 <dt id="parentStyleSheet">{{domxref("CSSRule.parentStyleSheet")}} {{readonlyinline}}</dt>
 <dd>Returns the {{domxref("CSSStyleSheet")}} object for the style sheet that contains this rule</dd>
 <dt id="type">{{domxref("CSSRule.type")}} {{readonlyinline}}</dt>
 <dd>One of the {{anch("Type constants")}} indicating the type of CSS rule.</dd>
</dl>

<h2 id="CSSRule" name="CSSRule">Constants</h2>

<h3 id="Type_constants">Type constants</h3>

<p>The <code>CSSRule</code> interface specifies integer constants that can be used in conjunction with a <code>CSSRule</code>'s {{domxref("cssRule/type","type")}} property to discern the rule type (and therefore, which specialized interface it implements). The relationships between these constants and the interfaces are:</p>

<table class="standard-table">
 <tbody>
  <tr>
   <th>Type</th>
   <th>Value</th>
   <th>Rule-specific interface</th>
   <th>Comments and examples</th>
  </tr>
  <tr>
   <td><code>CSSRule.STYLE_RULE</code></td>
   <td style="text-align: center;"><code>1</code></td>
   <td>{{domxref("CSSStyleRule")}}</td>
   <td>The most common kind of rule:<br />
    <code>selector { prop1: val1; prop2: val2; }</code></td>
  </tr>
  <tr>
   <td><code>CSSRule.IMPORT_RULE</code></td>
   <td style="text-align: center;"><code>3</code></td>
   <td>{{domxref("CSSImportRule")}}</td>
   <td>An {{cssxref("@import")}} rule. (Until the documentation is completed, see the interface definition in the Mozilla source code: <a href="https://mxr.mozilla.org/mozilla-central/source/dom/interfaces/css/nsIDOMCSSImportRule.idl#9">nsIDOMCSSImportRule</a>.)</td>
  </tr>
  <tr>
   <td><code>CSSRule.MEDIA_RULE</code></td>
   <td style="text-align: center;"><code>4</code></td>
   <td>{{domxref("CSSMediaRule")}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.FONT_FACE_RULE</code></td>
   <td style="text-align: center;"><code>5</code></td>
   <td>{{domxref("CSSFontFaceRule")}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.PAGE_RULE</code></td>
   <td style="text-align: center;"><code>6</code></td>
   <td>{{domxref("CSSPageRule")}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.KEYFRAMES_RULE</code></td>
   <td style="text-align: center;"><code>7</code></td>
   <td>{{domxref("CSSKeyframesRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.KEYFRAME_RULE</code></td>
   <td style="text-align: center;"><code>8</code></td>
   <td>{{domxref("CSSKeyframeRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><em>Reserved for future use</em></td>
   <td style="text-align: center;"><code>9</code></td>
   <td>&nbsp;</td>
   <td>Should be used to define color profiles in the future</td>
  </tr>
  <tr>
   <td><code>CSSRule.NAMESPACE_RULE</code></td>
   <td style="text-align: center;"><code>10</code></td>
   <td>{{domxref("CSSNamespaceRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.COUNTER_STYLE_RULE</code></td>
   <td style="text-align: center;"><code>11</code></td>
   <td>{{domxref("CSSCounterStyleRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.SUPPORTS_RULE</code></td>
   <td style="text-align: center;"><code>12</code></td>
   <td>{{domxref("CSSSupportsRule")}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.DOCUMENT_RULE</code></td>
   <td style="text-align: center;"><code>13</code></td>
   <td>{{domxref("CSSDocumentRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.FONT_FEATURE_VALUES_RULE</code></td>
   <td style="text-align: center;"><code>14</code></td>
   <td>{{domxref("CSSFontFeatureValuesRule")}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.VIEWPORT_RULE</code></td>
   <td style="text-align: center;"><code>15</code></td>
   <td>{{domxref("CSSViewportRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.REGION_STYLE_RULE</code></td>
   <td style="text-align: center;"><code>16</code></td>
   <td>{{domxref("CSSRegionStyleRule")}} {{experimental_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.UNKNOWN_RULE</code></td>
   <td style="text-align: center;"><code>0</code></td>
   <td>{{domxref("CSSUnknownRule")}} {{obsolete_inline}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td><code>CSSRule.CHARSET_RULE</code></td>
   <td style="text-align: center;"><code>2</code></td>
   <td><code>CSSCharsetRule</code> {{obsolete_inline}}</td>
   <td>(Removed in most browsers.)</td>
  </tr>
 </tbody>
</table>

<p>An up-to-date informal list of constants can be found on the <a href="https://wiki.csswg.org/spec/cssom-constants">CSSWG Wiki</a>.</p>

<h2 id="Specification" name="Specification">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSSOM', '#css-rules', 'CSSRule')}}</td>
   <td>{{Spec2('CSSOM')}}</td>
   <td>Obsoleted values <code>CHARSET_RULE</code> and <code>UNKNOWN_RULE</code>. Added value <code>NAMESPACE_RULE</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName("CSS3 Animations",'#interface-cssrule', 'CSSRule')}}</td>
   <td>{{Spec2('CSS3 Animations')}}</td>
   <td>Added values <code>KEYFRAMES_RULE</code> and <code>KEYFRAME_RULE</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Fonts', '#om-fontfeaturevalues', 'CSSRule')}}</td>
   <td>{{Spec2('CSS3 Fonts')}}</td>
   <td>Added value <code>FONT_FEATURE_VALUES_RULE</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName("CSS3 Counter Styles", "#extentions-to-cssrule-interface", 'CSSRule')}}</td>
   <td>{{Spec2("CSS3 Counter Styles")}}</td>
   <td>Added value <code>COUNTER_STYLE_RULE</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName("CSS3 Conditional", '#extentions-to-cssrule-interface', 'CSSRule')}}</td>
   <td>{{Spec2('CSS3 Conditional')}}</td>
   <td>Added value <code>SUPPORTS_RULE</code>. (<code>DOCUMENT_RULE</code> has been pushed to CSS Conditional Rules Level 4)</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Style', 'css.html#CSS-CSSRule', 'CSSRule')}}</td>
   <td>{{Spec2('DOM2 Style')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>9</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>CSSKeyframesRule</code></td>
   <td>{{CompatUnknown}}</td>
   <td>20.0 [4]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>CSSCharsetRule</code></td>
   <td>{{CompatNo}} [1]</td>
   <td>{{CompatNo}} [3]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>CSSUnknownRule</code></td>
   <td>{{CompatNo}} [2]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>CSSKeyframesRule</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>20.0 [4]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>CSSCharsetRule</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} [1]</td>
   <td>{{CompatNo}} [3]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} [1]</td>
  </tr>
  <tr>
   <td><code>CSSUnknownRule</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} [2]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} [2]</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] <code>CSSCharsetRule</code> was removed in Chrome version 41.0.</p>

<p>[2]&nbsp;<code>CSSUnknownRule</code> was removed in Chrome version 45.0.</p>

<p>[3]&nbsp;<code>CSSCharsetRule</code> was removed in Gecko version 40.</p>

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

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/Guide/DOM/Using_dynamic_styling_information">Using dynamic styling information</a></li>
</ul>
Revert to this revision