CSS Basic Learning 11: Selector priority

Source: Internet
Author: User

In the CSS basic Learning chapter, from the fourth blog began to say that the selector, to yesterday basically has been said finished. Today we summarize the selector as

use: tells the browser which DOM element style to set. Finally , the selector is an important issue, the selector priority. To determine the priority level

Square method is to try!!!

Priority of a simple selector

The simple selector includes our element selector (tag Selector), class selector and ID selector in the fourth, fifth, sixth blog posts.

Let's test it out:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The result of the run is: The ID selector has the highest precedence.


The result of commenting out the ID selector is that the class selector takes precedence over the next.


So the order of precedence for these three simple selectors is:HTML tag Properties >id selector > class selector > Element selector

Precedence of two type selectors

Same type: Refers to the same type of selector, the theoretical priority is the same. For example: Div and P. btn and. button. #header和

#footer, their priority is the same. But the precedence is different when the same type of selectors acts on the same HTML tag.

We continue to test:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The result of the operation is:


We tried the result: CSS rules written in the last side of the effective!!

If that's not convincing, we'll try again. <div class= "test1 test" ></div> What's the difference?

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The result of the operation is:

Finally, we come to the conclusion that the same type of selector, CSS rules written in the last side of the effective!

Three-selector priority

CSS selectors combine a lot of complex selector rules, so we can't try to do it like a simple selector. Below we introduce a

A very useful method of judging the priority level.

Judge Priority: We agree that the ID selector has a weight of 100, a class selector weight of 10, and a label selector weight of 1. The right of a complex selector

Weight equals the sum of the weights of all selectors. In general, the more special The selector, the higher the priority (weight).

Let's take a look at two complex selector rules first:

The weight of the first selector is: 1+10+10+1=22

Div.test. Item span{

Background-color: #00FF00;

}

The weight of the second selector is: 100+1+1=102

#test Div span{

Background-color: #FF0000;

}

Judging from our agreed rules, it is clear that the second one is in force!

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The results of the operation are consistent with our theory!


Then we may have a question: the same weight, the selector function? From a series of theories and experiments we can draw this with the same kind

The priority of the type selector is similar, we can easily conclude that the same weights should be chosen in the final order of validity.

Four!important

Style Highest priority: none Depending on the priority, precede the last semicolon of a declaration in a style to make the style work .

Let's try it out:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
First run results we certainly know is red, then we!important behind the tag Selector, we will run the results to see?

/* The element selector renders a red */div{color: #0000FF!important;}
The result of the operation is:


About the priority of selectors I have learned this, CSS selector is over!!









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CSS Basic Learning 11: Selector priority

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.