CSS style overrides in priority order

Source: Internet
Author: User

Cascade Priority is:

Browser defaults < external style Sheets < internal style Sheets < inline styles

The style sheet is also:

Class Selector < class derivation Selector < ID selector < ID derivation Selector

The derivation selector was previously called the context selector, so the complete cascade priority is:

Browser default < external style sheet < external style sheet class selector < external style sheet class derivation selector < external style sheet ID selector < external style sheet ID derivation selector < internal style sheet < internal style Sheet class selector < internal style sheet class derivation Selector < internal style Sheet ID selector < internal style sheet ID derivation selector < inline style ... A total of 12 priority

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

The code is as follows Copy 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 the order of the CSS is "style on the element" > "style element" > "external Style File" on the header, but there is no detail about how many of the same styles in the style file are prioritized. After testing and continuing the search, it is known that priority is arranged as follows:

1. The more accurate the element selector selection of the style sheet, the higher the style precedence.

The style specified by the ID selector > class selector specified by the style > element type selector specified by the
So in the example above, #navigator的样式优先级大于. Current_block priority, even if. Current_block is the most recent addition, it 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 precedence in the style sheet file, rather than the order in which the element class appears. For example. Class2 appears in the style sheet after. Class1:

The code is as follows Copy Code
. Class1 {
Color:black;
}

. class2 {
color:red;
}

When an element specifies class, it is specified in class= "Class2 Class1″, which, while Class1 is specified in the element after Class2, because Class1 is in front of class2 in the style sheet file, This is still a higher priority for class2, and the color is red, not black.

3. If you want a style to have a higher priority, you can specify it by using!important.

The code is as follows Copy Code
. Class1 {
Color:black!important;
}

. class2 {
color:red;
}

Solution:

The class will now use black, not red.

There are two solutions to the problem at first:

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

The code is as follows Copy Code
#navigator {
height:100%;
width:200;
Position:absolute;
left:0;
}

. Block {
Border:solid 2 #EEE;
}

. current_block {
Border:solid 2 #AE0;
}

You need to specify class= "block" by default for the #navigator element

2. Use!important:

The code is as follows Copy Code
#navigator {
height:100%;
width:200;
Position:absolute;
left:0;
Border:solid 2 #EEE;
}

. current_block {
Border:solid 2 #AE0!important;
}

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

Add an example

The code is as follows Copy Code

<title></title>
<style type= "Text/css" >
div {color: #00FF00}/* Green *
. A1 {color: #0000FF}/* Blue */
. A1 div {color: #00FFFF}/* Cyan * *
. a2 {color: #FF0000}/* Red *
#a2 {color: #FFFF00}/* Yellow *
#a2 div {color: #FF00FF}/* Purple * *
</style>
<body>
<div> I am green, the internal style sheet takes precedence over the browser default </div>
<div class= "A2" > I am Red, class selector takes precedence over internal style sheets </div>
<div class= "A2" id= "A2" > I am yellow, id selector takes precedence over class selector </div>
<div class= "A1" >
<span> I am blue, class selector takes precedence over internal style sheets </span>
<div> I am cyan, class derivation selector takes precedence over class selector </div>
<div class= "A2" > Me or Cyan, class derivation selector takes precedence over all class selectors </div>
<div id= "A2" >
<span> I am yellow, id selector takes precedence over class derivation selector </span>
<div> I am purple, id derivation selector takes precedence over class derivation selector </div>
<div class= "A1" > I'm still purple. The ID derivation selector takes precedence over all class selectors </div>
<div class= "A1" id= "A1" > I am still purple, the ID derivation selector takes precedence over all ID selectors </div>
<div class= "A1" id= "A1" style= "Color: #000000;" > I'm black, inline style, unauthorized people, etc. retreat </div>
</div>
</div>
</body>

Effect

In addition, if the same element is affected by the effect of no other style, its class is defined as multiple and separated by a space, in the order of precedence:

An element applies more than one class at the same time, the priority of the definition (i.e. near priority), plus the!important first!

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title></title>
<style type= "Text/css" >
. A2 {
font-size:18pt;
Color: #0000FF!important;
}
. A1 {
font-size:9pt;
Color: #FF0000;
}
</style>

<body>
<br/>
<span class= "A1" >a1</span><br/>
<span class= "A2" >a2</span><br/>
<span class= "A2 A1" >a2 A1</span><br
<span class= "A1 A2" >a1 a2</span><br
</body>

After-run effect chart:
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.