Specificity

  • Revision slug: CSS/Specificity
  • Revision title: Specificity
  • Revision id: 12226
  • Created:
  • Creator: FredB
  • Is current revision? No
  • Comment 458 words added, 121 words removed

Revision Content

The concept

Specificity is the means by which a browser decides which property values are the most relevant to an element and gets to be applied. Specificity is only based on the matching rules which are composed of selectors of different sorts.

How is it calculated?

The specificity is calculated on the concatenation of the count of each selectors type. It is not a weight that is applied to the corresponding matching expression.

In case of specificity equality, the latest declaration found in the CSS is applied to the element.

Note: Proximity of elements in the document tree has no effect on the specificity.

Crescent order of specificity

The following list of selectors is by increasing specificity:

  • Universal selectors
  • Type selectors
  • Class selectors
  • Attributes selectors
  • Pseudo-classes
  • ID selectors
  • Inline style

The !important exception

When an !important rule is used on a style declaration, this declaration overrides any other declaration made in the CSS, wherever it is in the declaration list. Although, !important has nothing to do with specificity.

The :not exception

The negation pseudo-class :not is not considered as a pseudo-class in the specificity calculation. Although, selectors placed into the negation pseudo-class count as normal selectors.

Here is a CSS chunk:

div.outer p {
  color:orange;
}
div:not(.outer) p {
  color: lime;
}

Used with the following HTML:

<div class="outer">
  <p>This is in the outer div.</p>
  <div class="inner">
    <p>This text is in the inner div.</p>
  </div>
</div>

Will appear on the screen as:

This is in the outer div.

This text is in the inner div.

Form-based specificity

Specificity is based on the form of a selector. In the following case, the selector counts as an attribute in the specificity determination algorithm although it selects an ID.

The following style declarations:

* #foo {
  color: green;
}
*[id="foo"] {
  background: purple;
}

Used with this markup:

<p id="foo">I am a sample text.</p>

Will end up looking like:

I am a sample text.

Because it matches the same element but the ID selector has a superior specificity.

Tree proximity ignorance

The following style declaration:

body h1 {
  color: green;
}
html h1 {
  color: purple;
}

With the following HTML:

<html>
<body>
  <h1>Here is a title!</h1>
</body>
</html>

Will render as:

Here is a title!

See also

Revision Source

<h2>The concept</h2>
<p>Specificity is the means by which a browser decides which property values are the most relevant to an element and gets to be applied. Specificity is only based on the matching rules which are composed of <a href="/en/CSS/CSS_Reference#Selectors" title="en/CSS/CSS_Reference#Selectors">selectors</a> of different sorts.</p>
<h2>How is it calculated?</h2>
<p>The specificity is calculated on the concatenation of the count of each selectors type. It is not a weight that is applied to the corresponding matching expression.</p>
<p>In case of specificity equality, the latest declaration found in the CSS is applied to the element.</p>
<div class="note">Note: Proximity of elements in the document tree has no effect on the specificity.</div>
<h3>Crescent order of specificity</h3>
<p>The following list of selectors is by increasing specificity:</p>
<ul> <li>Universal selectors</li> <li>Type selectors</li> <li>Class selectors</li> <li>Attributes selectors</li> <li>Pseudo-classes</li> <li>ID selectors</li> <li>Inline style</li>
</ul>
<h3>The <code>!important</code> exception</h3>
<p>When an <code>!important</code> rule is used on a style declaration, this declaration overrides any other declaration made in the CSS, wherever it is in the declaration list. Although, <code>!important</code> has nothing to do with specificity.</p>
<h3>The <code>:not</code> exception</h3>
<p>The negation pseudo-class <code>:not</code> is not considered as a pseudo-class in the specificity calculation. Although, selectors placed into the negation pseudo-class count as normal selectors.</p>
<p>Here is a CSS chunk:</p>
<pre class="brush: css">div.outer p {
  color:orange;
}
div:not(.outer) p {
  color: lime;
}
</pre>
<p>Used with the following HTML:</p>
<pre class="brush: html">&lt;div class="outer"&gt;
  &lt;p&gt;This is in the outer div.&lt;/p&gt;
  &lt;div class="inner"&gt;
    &lt;p&gt;This text is in the inner div.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
</pre>
<p>Will appear on the screen as:</p>
<p><span style="color:#ffa500;">This is in the outer div.</span></p>
<p><span style="color:#00ff00;">This text is in the inner div.</span></p>
<h3>Form-based specificity</h3>
<p>Specificity is based on the form of a selector. In the following case, the selector counts as an attribute in the specificity determination algorithm although it selects an ID.</p>
<p>The following style declarations:</p>
<pre>* #foo {
  color: green;
}
*[id="foo"] {
  background: purple;
}
</pre>
<p>Used with this markup:</p>
<pre class="brush: html">&lt;p id="foo"&gt;I am a sample text.&lt;/p&gt;
</pre>
<p>Will end up looking like:</p>
<p><span style="color:#008000;">I am a sample text.</span></p>
<p>Because it matches the same element but the ID selector has a superior specificity.</p>
<h3>Tree proximity ignorance</h3>
<p>The following style declaration:</p>
<pre>body h1 {
  color: green;
}
html h1 {
  color: purple;
}
</pre>
<p>With the following HTML:</p>
<pre>&lt;html&gt;
&lt;body&gt;
  &lt;h1&gt;Here is a title!&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Will render as:</p>
<p><span style="color:#800080;">Here is a title!</span></p>
<h2>See also</h2>
<ul> <li>CSS3 Selectors Specificity - <a class=" external" href="http://www.w3.org/TR/selectors/#specificity" rel="freelink">http://www.w3.org/TR/selectors/#specificity</a></li> <li>{{ CSS_key_concepts() }}</li>
</ul>
Revert to this revision