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 0 by default.
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:
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 purposes.
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 inherited.
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 normally inherited.
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 style attribute.
But this apparent simplicity is deceitful. If after a few hours of work, your perfectly crafted website looks great in Safari, all hell might break loose if you haven’t taken the necessary measures to make it work in Internet Explorer. In a panic, you add hacks and filters where only a few tweaks or a different approach might do. Knowing how to deal with these issues comes with experience, with trial and error and with failing massively and then learning the correct way.
Understanding a few often overlooked concepts is also important. The concepts may be hard to grasp and look boring at first, but understanding them and knowing how to take advantage of them is important.
A more indepth article then that of Nicolas Gallagher, but a most read. This article goes in great detail of what it means to understand the CSS specificity and inheritance model.
By default, this is the order in which the different sources are processed, so the author’s rules will override those of the user and user agent, and so on.
Every CSS rule has a particular weight (as mentioned in the introduction), meaning it could be more or less important than the others or equally important. This weight defines which properties will be applied to an element when there are conflicting rules.
A succinct and clear explanation of inheritance is in the CSS3 Cascading and Inheritance module specifications (still in “Working draft” mode):
Inheritance is a way of propagating property values from parent elements to their children.