Specificity
What Is Specificity? If two CSS selectors apply to the same element, the one with higher specificity wins.
Apart from floats, the CSS Specificity is one of the most difficult concepts to grasp in Cascading Stylesheets. The different weight of selectors is usually the reason why CSS-rules don’t apply to some elements in your code, although you think they should have. In order to minimize the time for bug hunting you need to understand, how browsers interpret your code. And to understand that, you need to have a firm understanding on how specificity works.
Specificity determines, which CSS rule is applied by the browsers.
Specificity is usually the reason why your CSS-rules don’t apply to some elements, although you think they should.
Every selector has its place in the specificity hierarchy.
If two selectors apply to the same element, the one with higher specificity wins.
There are four distinct categories which define the specificity level of a given selector: inline styles, IDs, classes, attributes, and elements.
You can understand specificity if you love poker: CSS Specificity for Poker Players
When selectors have an equal specificity value, the latest rule is the one that counts.
When selectors have an unequal specificity value, the more specific rule is the one that counts.
Rules with more specific selectors have a greater specificity.
The last rule defined overrides any previous, conflicting rules.
The embedded style sheet has a greater specificity than other rules.
ID selectors have a higher specificity than attribute selectors.
You should always try to use IDs to increase the specificity.
A class selector beats any number of element selectors.
The universal selector and inherited selectors have a specificity of 0, 0, 0, 0.
You can calculate CSS specificity with CSS Specificity Calculator.
Specificity Hierarchy
Every selector has its place in the specificity hierarchy. There are four distinct categories which define the specificity level of a given selector:
Inline styles (Presence of style in document). An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g.
<h1 style=“color: #fff;”>
IDs (# of ID selectors) ID is an identifier for your page elements, such as
#div
.Classes, attributes and pseudo-classes (# of class selectors). This group includes
.classes
,[attributes]
and pseudo-classes such as:hover
,:focus
etc.Elements and pseudo-elements (# of Element (type) selectors). Including for instance
:before
and:after
.
If you don’t know what exactly each of these terms stands for, you can take a look at the brief overview of them; in the last section of this article.
🤔 How Specificity can be measured?
Memorize how to measure specificity. “Start at 0, add 1000 for style attribute, add 100 for each ID, add 10 for each attribute, class or pseudo-class, add 1 for each element name or pseudo-element. So in
the specificity value would be 122 (0,1,2,2 or 0122): 100 for #content, 10 for .data, 10 for :hover, 1 for body and 1 for img.” [CSS Specificity]
Alternative way: “Count the number of ID attributes in the selector (= a). Count the number of other attributes and pseudo-classes in the selector (= b). Count the number of element names and pseudo-elements in the selector (= c). Concatenating the three numbers a-b-c gives the specificity.
Specificity Principles
Equal specificity: the latest rule is the one that counts. “If you have written the same rule into your external style sheet twice, then the lower rule in your style sheet is closer to the element to be styled, it is deemed to be more specific and therefore will be applied. When selectors have an equal specificity value, such as
where both rules have the specificity 0, 1, 0, 1, the latter rule is always applied.
Specificity Rules
ID selectors have a higher specificity than attribute selectors. For example, in HTML, the selector #p123
is more specific than [id=p123]
in terms of the cascade. Example: in
the first rule (A) is more specific than the second one (B). [W3C CSS 2.1 Specification]
Contextual selectors are more specific than a single element selector. It also holds for other selectors involving more than one HTML element selector.
The embedded style sheet is closer to the element to be styled. So in the following situation
CSS:
(X)HTML:
the latter rule will be applied.
The last rule defined overrides any previous, conflicting rules. For example, given these two rules
…paragraphs would appear in green text. They would also have a yellow background, however, because the first rule is not completely negated.
A class selector beats any number of element selectors. .introduction
beats html body div div h2 p
.
The universal selector has a specificity of 0, 0, 0, 0. *
, body *
and similar selectors have a zero specificity. Inherited values also have a specificity of 0, 0, 0, 0.
Specificity Example
Consider three code fragments:
The specificity of A is 0,0,0,1 (one element), the specificity of B is 0,1,0,1 (one ID reference point and one element), the specificity value of C is 1,0,0,0, since it is an inline styling.
Since
0001 = 1 < 0101 = 101 < 1000,
…the third rule has a greater level of specificity, and therefore will be applied. If the third rule didn’t exist, the second rule would have been applied.
Specificity In Practice
Use LVHA for link styling. “To ensure that you see your various link styles, you’re best off putting your styles in the order “link-visited-hover-active”, or “LVHA” for short.” [Link Specificity]
Never use !important
. “If you’re having specificity issues, there’s some quick ways to solve it. First, avoid !important
.” “The !important
declaration overrides normal declarations, but is unstructured and rarely required in an author’s style sheet.”
Use id to make a rule more specific. Replacing a.highlight
with ul#blogroll a.highlight
changes the specificity from 0, 0, 1, 1 to 0, 1, 1, 2.
Minimize the number of selectors. “Use the least number of selectors required to style an element.”
CSS Specificity Tools
CSS Specificity for Poker Players If you’re not from the programming world and CSS seems a bit confusing, perhaps this analogy may help clear some concepts up. Think of CSS rules as poker hands. The best hand determines an element’s style.
CSS specificity calculator Calculates the specificity of a given selector.
A selector is the element that is linked to a particular style. E.g. p
in
A class selector is a selector that uses a defined class (multiple per page). E.g. p.section
in
An ID selector is a selector that uses an individually assigned identifier (one per page). E.g. p#section
in
A contextual selector is a selector that defines a precise cascading order for the rule. E.g. p span
in
defines that all span-elements within a p-element should be styled in italics.
An attribute selector matches elements which have a specific attribute or its value. E.g. p title
in
matches all p-elements which have a title
attribute.
Pseudo-classes are special classes that are used to define the behavior of HTML elements. They are used to add special effects to some selectors, which are applied automatically in certain states. E.g. :visited
in
Pseudo-elements provide designers a way to assign style to content that does not exist in the source document. Pseudo-element is a specific, unique part of an element that can be used to generate content “on the fly”, automatic numbering and lists. E.g. :first-line
or :after
in
Last updated