Style overlay order in CSS style sheets

Source: Internet
Author: User

When I wrote Zenktodo, I changed the style of a div by adding class dynamically, and it never worked.

CSS Code
    1. #navigator {
    2. Height: 100%;
    3. Width: 200;
    4. Position:absolute;
    5. Left: 0;
    6. Border:solid 2 #EEE;
    7. }
    8. . current_block {
    9. Border:solid 2 #AE0;
    10. }

Find some textbooks (w3schools, etc.), just say that the order of the CSS is "style on the element" > "style element on file header" > "External Style File", but the precedence of multiple same styles in the style file is not explained in detail. After testing and continuing the search, you know that the priority is listed as follows:

1. The more precise the element selector selection of a style sheet, the higher the style precedence:

The ID selector Specifies the style > class selector for the specified style > element type selector for the specified style

So in the example above, #navigator的样式优先级大于. Current_block priority, even if. Current_block is newly added and does not work.

2. For styles specified by the same type selector, the higher the precedence in the style sheet file

Note that this is the higher priority in the style sheet file, rather than the order in which the element class appears. For example. Class2 appears in the style sheet after the. Class1:

CSS Code
    1. . Class1 {
    2. Color:black;
    3. }
    4. . class2 {
    5. color:red;
    6. }

When an element is specified as class, it is specified in class= "Class2 Class1", at which point the Class1 is followed by Class2 when specified in the element, but because Class1 in the style sheet file is in front of Class2, At this point, the Class2 is still a higher priority, and the color property is red, not black.

3. If you want a style to have a higher priority, you can use!important to specify:

CSS Code
    1. . Class1 {
    2. Color:black!important;
    3. }
    4. . class2 {
    5. color:red;
    6. }

At this point, the class will use black rather than red.

There are two solutions for the problem at first:

1. Take the border out of the #navigator and put it in a class. block, and. Block before the. Current_block:

CSS Code
    1. #navigator {
    2. Height: 100%;
    3. Width: 200;
    4. Position:absolute;
    5. Left: 0;
    6. }
    7. . Block {
    8. Border:solid 2 #EEE;
    9. }
    10. . current_block {
    11. Border:solid 2 #AE0;
    12. }

Need to specify class= "block" by default for #navigator element

2. Use!important:

CSS Code
    1. #navigator {
    2. Height: 100%;
    3. Width: 200;
    4. Position:absolute;
    5. Left: 0;
    6. Border:solid 2 #EEE;
    7. }
    8. . current_block {
    9. Border:solid 2 #AE0!important;
    10. }

No other changes are required to take effect at this time. It can be seen that the second scenario is simpler.

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.