CSSRule

  • Revision slug: DOM/cssRule
  • Revision title: CSSRule
  • Revision id: 296734
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ CSSOMRef() }}

An object implementing the CSSRule DOM interface represents a single CSS 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.

Properties of all CSSRule instances

cssText
Returns the textual representation of the rule, e.g. "h1,h2 { font-size: 16pt }"
parentRule
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
Returns the CSSStyleSheet object for the style sheet that contains this rule
type
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") }} {{ 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") }} {{ 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() }}

The {{ domxref("CSSMozDocumentRule") }} does not have a corresponding type constant.

An up-to-date informal list of constants can be foune on the CSS Wiki.

Specification

See also

{{ languages( { "pl": "pl/DOM/cssRule" } ) }}

Revision Source

<p>{{ CSSOMRef() }}</p>
<p>An object implementing the <code>CSSRule</code> DOM interface represents a single CSS rule. References to a <code>CSSRule</code>-implementing object may be obtained by looking at a <a href="/en/DOM/CSSStyleSheet" title="en/DOM/stylesheet">CSS style sheet's</a> <code><a href="/en/DOM/CSSStyleSheet" title="en/DOM/CSSStyleSheet/cssRules">cssRules</a></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.</p>
<h2 id="Properties_of_all_CSSRule_instances">Properties of all CSSRule instances</h2>
<dl>
  <dt id="cssText">
    <code><a href="/en/DOM/CSSRule/cssText" title="en/DOM/cssRule.cssText">cssText</a></code></dt>
  <dd>
    Returns the textual representation of the rule, e.g. <code>"h1,h2 { font-size: 16pt }"</code></dd>
  <dt id="parentRule">
    <a href="/en/DOM/cssRule/parentRule" title="en/DOM/cssRule/parentRule"><code>parentRule</code></a></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/CSS/@media" title="en/CSS/@media">@media</a></code> block, the parent rule would be that <code><a href="/en/DOM/CSSMediaRule" title="en/DOM/CSSMediaRule">CSSMediaRule</a></code>.</dd>
  <dt id="parentStyleSheet">
    <code><a href="/en/DOM/CSSRule/parentStyleSheet" title="en/DOM/cssRule.parentStyleSheet">parentStyleSheet</a></code></dt>
  <dd>
    Returns the <code><a href="/en/DOM/CSSStyleSheet" title="en/DOM/CSSStyleSheet">CSSStyleSheet</a></code> object for the style sheet that contains this rule</dd>
  <dt id="type">
    <a href="/en/DOM/cssRule/type" title="en/DOM/cssRule/type"><code>type</code></a></dt>
  <dd>
    One of the {{ anch("Type constants") }}&nbsp;indicating the type of CSS&nbsp;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/DOM/cssRule/type" title="en/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") }} {{ experimental_inline() }}</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>
    </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>The {{ domxref("CSSMozDocumentRule") }} does not have a corresponding type constant.</p>
<p>An up-to-date informal list of constants can be foune on the <a href="http://wiki.csswg.org/spec/cssom-constants" title="http://wiki.csswg.org/spec/cssom-constants">CSS Wiki</a>.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSRule">DOM Level 2 CSS: CSSRule</a></li>
  <li><a class="external" href="http://dev.w3.org/csswg/cssom/#css-rules" title="http://dev.w3.org/csswg/cssom/#css-rules">CSS Object Model: CSS Rules</a></li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en/DOM/Using_dynamic_styling_information" title="en/DOM/Using_dynamic_styling_information">Using dynamic styling information</a></li>
</ul>
<p>{{ languages( { "pl": "pl/DOM/cssRule" } ) }}</p>
Revert to this revision