Faster, Higher, Stronger
更快,更高,更强

Specificity

The concept

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

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

How is it calculated?

The specificity is a weight that is applied to a given CSS declaration based on the count of each selector type. In the case of specificity equality, the latest declaration found in the CSS is applied to the element. Specificity only applies when the same element is targeted. CSS rules that directly target an element will always take precedence over rules that an element inherits from an ancestor.

Selector Types

The following list of selector types is by increasing specificity:

  1. Universal selectors (i.e., *).
  2. Type selectors (e.g., h1) and pseudo-elements (e.g., :before).
  3. Class selectors (e.g., .example), attributes selectors (e.g., [type=”radio”]) and pseudo-classes (e.g., :hover).
  4. ID selectors (e.g., #example).

Inline style added to an element (e.g., style=”font-weight:bold”) always overwrites any styles in the CSS and thus can be though as having the biggest specificity.

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 and should be avoided because it makes debugging more difficult by breaking the natural cascading in your stylesheets. When two conflicting declarations with the !important rule are applied to the same element, the declaration with greater specificity will be applied.

Some rules of thumb
  • Always look for a way to use specificity before even considering !important
  • 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.
  • Never use !important on site-wide css.
Instead of using !important, 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 becomes more specific and gets higher priority:

Text

div#test span { color: green }
div span { color: blue }
span { color: red }

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, notwithstanding the order of the rules)

You should use it when:

A) Scenario one:

  1. You have a global CSS file that sets visual aspects of your site globally
  2. ou (or others) use inline styles on elements themselves which is a 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.(!important与specificity无关,不影响specificity的计算,它的出现直接无视各种规则的specificity(当元素内部的inline styles遇到CSS文件里带!important的规则时也会被带!important的规则覆盖掉),除非其他规则也有!important。)

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

A) 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;}

B) Or add the same selector after the existing one:


td {height: 50px !important;}

C) Or rewrite the original rule to avoid the use of !important altogether.

The :not exception

The negation pseudo-class :not is not considered a pseudo-class in the specificity calculation. But selectors placed into the negation pseudo-class count as normal selectors when determining the count of selector types.

Here is a CSS chunk:


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

when used with the following HTML:


This is in the outer div.

This text is in the inner div.

Shall 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 *[id=”foo”] counts as an attribute selector for the purpose of determining the selector’s specificity, even though it selects an ID.(如果是以属性选择器的形式来选择id,那么就按属性选择器来计算specificity,而不是按id来计算specificity)

The following style declarations:


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

when used with this markup:


I am a sample text.

Will end up looking like:

I am a sample text.

Because it matches they match the same element but the ID selector has a higher specificity.

Tree proximity ignorance

The proximity of an element to other elements that are referenced in a given selector has no impact on specificity. The following style declaration:


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

With the following HTML:




  

Here is a title!

Will render as:

Here is a title!

Because the two declarations have equal selector type counts, but the html h1 selector is declared last.

Directly targeted elements versus inherited styles

Styles for a directly targeted element will always take precedence over inherited styles, regardless of the specificity of the inherited rule.


#parent {
  color: green;
}
h1 {
  color: purple;
}

With the following HTML:




  

Here is a title!

Will also render as:

Here is a title!

Because the h1 selector targets the element specifically, but the green selector is only inherited from the parent.

赞(0) 打赏
未经允许不得转载:峰间的云 » Specificity

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏