Priority is actually a problem solved by a conflict. When the same element (or content) is selected by the CSS selector, different CSS rules should be selected based on priority, this involves a lot of problems.
The first is the specificity (particularity) of CSS rules. css2.1 has a set of computing methods for specificity, represented by a four-digit numeric string (css2 is a three-digit, the higher the specificity, the more special the rule will be. The following general rules apply to the calculation of specificity in various situations:
- Select the identifier (# someid) for each ID, and add 0, 1, 0, and 0.
- Each Class Identifier (. someclass), each attribute identifier (such as [ATTR = ""]), and each pseudo class (such as hover) plus
- For each element or pseudo element (: firstchild), add
- Other delimiters include the Global delimiters *, which are 0, 0, and 0. This is equivalent to not being added, but it is also a specificity, which will be explained later.
According to these rules, the number strings are added by bit to obtain the final specificity calculated. Then, the comparison results are compared by bit in the order from left to right.
For example:
-
- H1{Color:Red;}
- /* Only one common element is added. The result is 0, 0, 1 */
-
- Body H1{Color:Green;}
-
- /* Add two common elements. The result is 0, 0, 0, 2 */
-
- -- 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, 1 */
-
- -- The former wins
-
-
- HTML> body table tr [ID = "totals"] TD ul> Li {Color:Maroon;}
-
- /* Seven common elements, one attribute selector, and two other delimiters. The result is */
- Li# Answer{Color:Navy;}
-
- /* An ID selector and a common selector. The result is 0, 1, 1 */
-
- -- The latter wins
In addition to specificity, there are other rules
- The style priority is, so it is always higher than the external definition. Here, the style finger is shown in <Div style = "color: Red"> blah </div>, external definitions refer to rules defined by <link> or <style> labels.
- Yes! Important declares more rules than anything else.
- If! If important declares a conflict, the priority is compared.
- If the priority is the same, it is determined by the order in which the source code appears.
- The inherited style does not calculate specificity. It is lower than all other rules (such as rules defined by the global selector ).
- For external styles loaded by @ import, because @ import must appear before all other rule definitions (if not, the browser should ignore it), follow the principle of "post-Import, generally, priority conflicts take a disadvantage.
Here we need to mention that IE can identify @ import with incorrect location. However, no matter where @ import is located, it is considered to be located before all other rule definitions, this may cause some misunderstandings.
Therefore, although the priority issue looks simple, there is still a complicated mechanism behind it, and you need to pay more attention to it.