Css2.1 specifies the specificity (specificity) Calculation Method of CSS rules, which is represented by a four-digit numeric string (Note: three digits are used in css2, finally, the priority of CSS is determined based on specificity.
Specific specificity calculation rules:
Add 1, 0, 0, 0 to the style attribute of the element.
Select the identifier (# ID) for each ID, and add 0, 1, 0, and 0.
Each Class Identifier (. Class), each attribute identifier (for example, [ATTR = ""]), and each pseudo class (for example, hover), plus 0, 0, and 1. 0.
Add 0, 0, 0, and 1 to each element or pseudo element (for example, firstchild.
Other delimiters (for example, global delimiters *, sub-delimiters>), plus 0, 0, 0.
Finally, the final specificity value is obtained through the phase-by-phase addition string, which is compared by bit from left to right.
In addition to specificity, there are other rules:
! Important declares more rules than anything else. If! If important declares a conflict, the priority is compared.
If the priorities are the same, the "post-Come" Principle in the source code is used.
The inherited style attributes do not participate in specificity calculation. They are less than all other rules (for example, global selector *).
Sample Analysis:
H1 { Color : Red ;}
/* Only one common element is added. The result is 0, 0, 1. */
Body H1 { Color : Green ;}
/* The addition of two common elements. The result is 0, 0, 0, 2. */
/* 0, 0, less than 0, 0, the latter wins */
H2.grape { Color : Purple ;}
/* A common element and a class operator addition result: 0, 0, 1, 1 */
H2 { Color : Silver ;}
/* A common element. The result is 0, 0, and 1. */
/* 0, 0, is greater than 0, 0, 1, and the former wins */
HTML> body table tr [ID = "totals"] TD ul> Li { Color : Maroon ;}
/* Seven common elements, one attribute selector, and two other delimiters (sub-Selector>). The result is ,. */
Li # answer { Color : Navy ;}
/* An ID selector and a common selector. The result is 0, 1, 1. */
/* , Is smaller than, and the latter wins */
-- Import from http://www.cnblogs.com/Hotcocoa/articles/2104147.html