CSSRule

  • Revision slug: DOM/cssRule
  • Revision title: CSSRule
  • Revision id: 360143
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment This page have URL problem.

Revision Content

{{CSSOMRef}}

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 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.

Syntax

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

cssText
Represents the textual representation of the rule, e.g. "h1,h2 { font-size: 16pt }"
parentRule {{readonlyinline}}
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.
parentStyleSheet {{readonlyinline}}
Returns the CSSStyleSheet object for the style sheet that contains this rule
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 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
CSSRule.STYLE_RULE 1 {{domxref("CSSStyleRule")}}
CSSRule.MEDIA_RULE 4 {{domxref("CSSMediaRule")}}
CSSRule.FONT_FACE_RULE 5 {{domxref("CSSFontFaceRule")}}
CSSRule.PAGE_RULE 6 {{domxref("CSSPageRule")}}
CSSRule.IMPORT_RULE 3 {{domxref("CSSImportRule")}}
CSSRule.CHARSET_RULE 2 {{domxref("CSSCharsetRule")}} {{obsolete_inline}}
CSSRule.UNKNOWN_RULE 0 {{domxref("CSSUnknownRule")}}{{obsolete_inline}}
CSSRule.KEYFRAMES_RULE 7 {{domxref("CSSKeyframesRule")}} [1] {{experimental_inline}}
CSSRule.KEYFRAME_RULE 8 {{domxref("CSSKeyframeRule")}} [1] {{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")}} {{experimental_inline}}
CSSRule.DOCUMENT_RULE 13 {{domxref("CSSDocumentRule")}} {{experimental_inline}}
CSSRule.FONT_FEATURE_VALUES_RULE 14 {{domxref("CSSFontFeatureValuesRule")}} {{experimental_inline}}
CSSRule.VIEWPORT_RULE 15 {{domxref("CSSViewportRule")}} {{experimental_inline}}
CSSRule.REGION_STYLE_RULE 16 {{domxref("CSSRegionStyleRule")}} {{experimental_inline}}

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

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

Browser compatibility

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

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} 9 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

Revision Source

<div>{{CSSOMRef}}</div>
<p>An object implementing the <strong><code>CSSRule</code></strong> DOM interface represents a single CSS at-rule. References to a <code>CSSRule</code>-implementing object may be obtained by looking at a <a href="/en-US/docs/DOM/CSSStyleSheet" title="DOM/stylesheet">CSS style sheet's</a> <a href="/en-US/docs/DOM/CSSStyleSheet" title="DOM/CSSStyleSheet/cssRules"><code>cssRules</code></a> 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.</p>
<h2 id="Properties" name="Properties">Syntax</h2>
<p>The syntax is described using the <a href="http://dev.w3.org/2006/webapi/WebIDL/" title="http://dev.w3.org/2006/webapi/WebIDL/">WebIDL</a> format.</p>
<pre>
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;
};</pre>

<h2 id="Properties_common_to_all_CSSRule_instances">Properties common to all CSSRule instances</h2>
<dl>
  <dt id="cssText">
    <code><a href="/en-US/docs/DOM/CSSRule/cssText" title="DOM/cssRule.cssText">cssText</a></code></dt>
  <dd>Represents the textual representation of the rule, e.g. <code>"h1,h2 { font-size: 16pt }"</code></dd>
  <dt id="parentRule">
    <a href="/en-US/docs/DOM/cssRule/parentRule" title="DOM/cssRule/parentRule"><code>parentRule</code></a> {{readonlyinline}}</dt>
  <dd>Returns the containing rule, otherwise <code>null</code>. E.g. if this rule is a style rule inside an <code><a href="/en-US/docs/CSS/@media" title="CSS/@media">@media</a></code> block, the parent rule would be that <code><a href="/en-US/docs/DOM/CSSMediaRule" title="DOM/CSSMediaRule">CSSMediaRule</a></code>.</dd>
  <dt id="parentStyleSheet">
    <code><a href="/en-US/docs/DOM/CSSRule/parentStyleSheet" title="DOM/cssRule.parentStyleSheet">parentStyleSheet </a></code>{{readonlyinline}}</dt>
  <dd>Returns the <code><a href="/en-US/docs/DOM/CSSStyleSheet" title="DOM/CSSStyleSheet">CSSStyleSheet</a></code> object for the style sheet that contains this rule</dd>
  <dt id="type">
    <a href="/en-US/docs/DOM/cssRule/type" title="DOM/cssRule/type"><code>type</code></a> {{readonlyinline}}</dt>
  <dd>One of the {{anch("Type constants")}}&nbsp;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 <a href="/en-US/docs/DOM/cssRule/type" title="DOM/CSSRule/type"><code>type</code></a> 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>
    </tr>
    <tr>
      <td><code>CSSRule.STYLE_RULE</code></td>
      <td style="text-align: center;"><code>1</code></td>
      <td>{{domxref("CSSStyleRule")}}</td>
    </tr>
    <tr>
      <td><code>CSSRule.MEDIA_RULE</code></td>
      <td style="text-align: center;"><code>4</code></td>
      <td>{{domxref("CSSMediaRule")}}</td>
    </tr>
    <tr>
      <td><code>CSSRule.FONT_FACE_RULE</code></td>
      <td style="text-align: center;"><code>5</code></td>
      <td>{{domxref("CSSFontFaceRule")}}</td>
    </tr>
    <tr>
      <td><code>CSSRule.PAGE_RULE</code></td>
      <td style="text-align: center;"><code>6</code></td>
      <td>{{domxref("CSSPageRule")}}</td>
    </tr>
    <tr>
      <td><code>CSSRule.IMPORT_RULE</code></td>
      <td style="text-align: center;"><code>3</code></td>
      <td>{{domxref("CSSImportRule")}}</td>
    </tr>
    <tr>
      <td><code>CSSRule.CHARSET_RULE</code></td>
      <td style="text-align: center;"><code>2</code></td>
      <td>{{domxref("CSSCharsetRule")}} {{obsolete_inline}}</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>
    </tr>
    <tr>
      <td><code>CSSRule.KEYFRAMES_RULE</code></td>
      <td style="text-align: center;"><code>7</code></td>
      <td>{{domxref("CSSKeyframesRule")}} [1] {{experimental_inline}}</td>
    </tr>
    <tr>
      <td><code>CSSRule.KEYFRAME_RULE</code></td>
      <td style="text-align: center;"><code>8</code></td>
      <td>{{domxref("CSSKeyframeRule")}} [1] {{experimental_inline}}</td>
    </tr>
    <tr>
      <td><em>Reserved for future use</em></td>
      <td style="text-align: center;"><code>9</code></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>
    </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>
    </tr>
    <tr>
      <td><code>CSSRule.SUPPORTS_RULE</code></td>
      <td style="text-align: center;"><code>12</code></td>
      <td>{{domxref("CSSSupportsRule")}} {{experimental_inline}}</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>
    </tr>
    <tr>
      <td><code>CSSRule.FONT_FEATURE_VALUES_RULE</code></td>
      <td style="text-align: center;"><code>14</code></td>
      <td>{{domxref("CSSFontFeatureValuesRule")}} {{experimental_inline}}</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>
    </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>
    </tr>
  </tbody>
</table>
<p>An up-to-date informal list of constants can be found on the <a href="http://wiki.csswg.org/spec/cssom-constants" title="http://wiki.csswg.org/spec/cssom-constants">CSSWG Wiki</a>.</p>
<p>[1] 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="Specification" name="Specification">Specification</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&nbsp; <code>NAMESPACE_RULE</code>.</td>
    </tr>
    <tr>
      <td>{{SpecName("CSS3 Conditional", '#extentions-to-cssrule-interface', 'CSSRule')}}</td>
      <td>{{Spec2('CSS3 Conditional')}}</td>
      <td>Added values <code>SUPPORTS_RULE</code> (<code>DOCUMENT_RULE</code> has been pushed to CSS Conditional Rules Level 4),</td>
    </tr>
    <tr>
      <td>{{SpecName("CSS3 Animations",'#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('DOM2 Style', 'css.html#CSS-CSSRule', 'CSSRule')}}</td>
      <td>{{Spec2('DOM2 Style')}}</td>
      <td>&nbsp;</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>To get browser compatibility information about the different type constant value, please consult the compatibility table of the associated interface.</p>
<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>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/DOM/Using_dynamic_styling_information" title="DOM/Using_dynamic_styling_information">Using dynamic styling information</a></li>
  <li>{{CSSOM}}</li>
</ul>
Revert to this revision