What is specificity?
Specificity is a method of conflict resolution within the cascade.
Specificity is calculated in a very particular way, based on the values of 4
distinct categories. For explanatory purposes, the CSS2 spec represents these
categories using the letters
d. Each has a value of
ais equal to
1if the declaration comes from a
styleattribute in the HTML (“inline styles”) rather than a CSS rule with a selector.
bis equal to the number of ID attributes in a selector.
cis equal to the number of other attributes and pseudo-classes in a selector.
dis equal to the number of elements and pseudo-elements in a selector.
The specificity is given by concatenating all 4 resulting numbers. More specific selectors take precedence over less specific ones.
For example, the selector
#id .class[href] element:hover contains:
- 1 ID (
- 1 class, 1 attribute selector, and 1 pseudo-class (
- 1 element (
Therefore, it has a specificity of
0,1,3,1. Note that a selector containing a
single ID (
0,1,0,0) will have a higher specificity than one containing any
number of other attributes or elements (e.g.,
0,0,10,20). This is one of the
reasons why many modern CSS architectural patterns avoid using IDs for styling
What is inheritance?
Inheritance is distinct from the cascade and involves the DOM tree.
Inheritance is the process by which elements inherit the the values of
properties from their ancestors in the DOM tree. Some properties, e.g.
color, are automatically inherited by the children of the element to which
they are applied. Each property defines whether it will be automatically
inherit value can be set for any property and will force a given element
to inherit its parent element’s property value, even if the property is not
The above should make it apparent that
!important is a separate concept to
specificity. It has no effect on the specificity of a rule’s selector.
!important declaration has a greater precedence than a normal declaration
(see the previously mentioned cascade sorting logic), even declarations
contained in an element’s