Source: http://www.ido321.com/1063.html
First, let's show you an example of CSS precedence: http://www.ido321.com/76.html
I. Basic PRIORITY rules
Compares the number of the same level, the number of high priority, if the same is compared to the next level of the number, as to the level of priority is as follows:
important > Inline > ID > class > tags | pseudo-Class | attribute selection > Pseudo-object > Wildcard > Inheritance
Second, the CSS weight rule
As mentioned in the modular design in page refactoring, there are two important factors that affect CSS style weights:
A: The precedence of a style is related to the order in which the style is defined
B: The weight value depends on the type and number of selectors
In general, in the same CSS file, if there are two styles with the same name, then the definition will overwrite the first defined. The priority of the selector type is as shown in (i).
So, how is the weight of the CSS calculated?
A selector weight representation: 0.0.0.0, according to the calculation rules to each bit fill number, corresponding position is equal, then compare the next bit.
The calculation rules for weights are as follows: (for convenience, use A.B.C.D instead of values for each position)
1. Inline style: a=1,b=c=d=0 (i.e. 1.0.0.0)
2, ID style: Selector with a few ID, 2nd bit add a few 1 such as #header{color:red}, is a=c=d=0,b=1 (that is, 0.1.0.0)
3, class, Pseudo-class, and the number of attributes is the third-digit value:
. a.b[type= "Text"]:hover{}, the selector has 2 classes, 1 properties, 1 pseudo-classes, so its 3rd bit is 4, a=b=d=0,c=4, (ie 0.0.4.0)
4, the number of pseudo-elements and tag elements is the value of the fourth bit
P:first-letter, with a tag element p and a pseudo-element first-letter, with a value of 2,a=b=c=0,d=2 (i.e. 0.0.0.2)
5. Wildcard characters and inherited CSS properties have no effect on weights
Three, a few examples
For the above, see the following examples
1.. a. B. c {color:red;} 0.0.3.0 Description: Only three classes
2, *
{...} 0.0.0.0 Description: Wildcards have no effect on weights
3. A. b a {color:green}//0.0.2.1 Description: Two classes and one label
4, #hid {color:black;} 0.1.0.0 Description: Only one ID
Next: HTML 5 History API "former present"
CSS: Weights and cascading rules determine their precedence