CSS style overlay order in style sheet for instance sharing

Source: Internet
Author: User

Sometimes in the process of writing CSS, some restrictions always do not work, which involves the CSS style overlay problems, as follows


CSS Code

#navigator {      height:100%;      width:200;      Position:absolute;      left:0;      Border:solid 2 #EEE;  }  . Current_block {      border:solid 2 #AE0;  }

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, timely. Current_block is newly added and does not work.

2. For styles developed 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

. class1 {      color:black;  }  . Class2 {      color:red;  }

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

. Class1 {      color:black!important;  }  . Class2 {      color:red;  }

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

#navigator {      height:100%;      width:200;      Position:absolute;      left:0;  }  . Block {      border:solid 2 #EEE;  }  . Current_block {      border:solid 2 #AE0;  }

Requires Moren to specify class= "block" for #navigator element

2. Use!important:

CSS Code

 #navigator {height:100%;      width:200;      Position:absolute;      left:0;  Border:solid 2 #EEE;  }. Current_block {border:solid 2 #AE0!important; }
Related Article

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.