CSS priority issues

Source: Internet
Author: User

Before reading this article, the problem has not been thoroughly studied, resulting in some problems will sometimes be very troublesome, see this article let me enlightened, reprint back to save for the future review.

Found that many friends do not know the priority of CSS, the rules are simple. To illustrate, if your style management needs to deeply judge the precedence of CSS, it should be more reflective of their own CSS code, is it reasonable? Is it optimized?

CSS2.1 provides a method for calculating the CSS rules specificity (specificity), using a four-bit number string (Note: The CSS2 is three-bit) to represent, and finally to specificity the high and low to determine the priority of CSS.

Specificity specific calculation rules:

    • The style style property of the element, plus 1,0,0,0.
    • Each ID selector (#id), plus 0,1,0,0.
    • Each class selector (. Class), each attribute selector (example [attr= "]), each pseudo-class (example: hover), plus 0,0,1,0.
    • Each element or pseudo-element (example: firstchild), etc., plus 0,0,0,1.
    • Additional selectors (example global selector *, sub-selector >), plus 0,0,0,0.

Finally, the final specificity value is added to the number string, which is compared to the left-to-right order by bit.

There are some other rules besides specificity:

    • !important declares a rule above all, and if!important declares a conflict, it compares precedence.
    • If the priority is the same, then according to the source code in the "later on" principle.
    • Style attributes derived from inheritance do not participate in the calculation of specificity, below all other rules (example global selectors *).

Example Analysis:

H1{Color:Red;}/ * Only one normal element is added, the result is 0,0,0,1 * /Body H1{Color:Green;}/ * Two ordinary element bonus, the result is 0,0,0,2 * //*0,0,0,1 less than 0,0,0,2, the latter wins * /H2.Grape{Color:Purple;}/* An ordinary element, a class selector bonus, and the result is 0,0,1,1*/H2{Color:Silver;}/ * An ordinary element, the result is 0,0,0,1 * //*0,0,1,1 greater than 0,0,0,1, the former WINS * /Html>Body Table TR[Id= "totals"]  TD ul  >  li  {color:  Maroon ;} /  * 7 ordinary elements, one attribute selector, two other selectors (sub-selector >), the result is 0,0,1,7 */li#answer {color:navy;} /  * An ID selector, a normal selector, the result is 0,1,0,1 *//*0,0,1,7 less than 0,1,0,1, the latter wins *      / 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.