We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Specificity Redirect 1

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.  Using !important is bad practice because it makes debugging hard since you break the natural cascading in your stylesheets.

Some rules of thumb

  • Never use !important on site-wide css.
  • Only use !important on page-specific css that overrides site-wide or foreign css (from ExtJs or YUI for example).
  • Never use !important when you're writing a plugin/mashup.
  • Always look for a way to use specificity before even considering !important



Instead of using it, you can:

  1. Make better use of CSS' cascading properties
  2. Use more specific rules. By indicating one or more elements before the element you're selecting the rule is more specific and gets higher priority:

    <div id="test">
    div#test span { color: green }
    span { color: red }
    div span { color: blue }

No matter what the order, the text will be green because that rule is most specific. (Also, the rule for blue overwrites the rule for red, no matter the order of the rules)

You should use it when:

A) One scenario

  1. you have a global CSS file that sets visual aspects of your site globally
  2. you (or others) use inline styles on elements themselves which is usually very bad practice

In this case you could set certain styles in your global CSS file as important thus overriding inline styles set directly on elements.

Real world example: Some badly written jQuery plugins that use inline styles.

B) Another scenario

#someElement p {
    color: blue;

p.awesome {
    color: red;

How do you make awesome paragraphs always turn red, even ones inside #someElement? Without !important, the first rule will have more specificity and will win over the second rule.


How to override !important

Simply add another CSS rule with !important, and either give the selector a higher specificity (adding an additional tag, id or class to the selector), or add a CSS rule with the same selector at a later point than the existing one (in a tie, the last one defined wins).

Some examples with a higher specificity:

table td    {height: 50px !important;}
.myTable td {height: 50px !important;}
#myTable td {height: 50px !important;}

Or add the same selector after the existing one:

td {height: 50px !important;}


For more information, visit:







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

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"] {
  color: 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:

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

Will render as:

Here is a title!

See also

Document Tags and Contributors

Last updated by: Sheppy,