Sequence of css5 selectors

Source: Internet
Author: User

I wrote a page this evening and found that many selectors are used, especially when multiple selectors exist on a tag.

So what will happen when all the five selectors are added to one tag? I tried it. We will share the following results with you.


<Html>

<Head>

<Style>

. C1 {

Background-color: red;

}

# D1 {

Background-color: blue;

}

Body, div {

Background-color: white;

}

Body # d1 {

Background-color: yellow;

}

Div {

Background-color: green;

}

</Style>

</Head>

<Body>

<Div id = '1' class = 'c1'> Hello </div>

</Body>

</Html>

The background is yellow, that is, the role is the body # d1 derivative selector. First, we need to understand a concept. In fact, each selector works. css is called a Cascading Style Sheet, in fact, I think it makes people better understand the stacked style sheet. As the name suggests, it is a layer-by-layer style sheet. That's right, it is actually like this. All selectors are executed, except that the derived selector is the final execution, so it overwrites the other selector. Here we will talk about priority or something, I think it is better to explain the order of execution.


Now that the derived selector is executed at the end, we can change the code and remove the derived selector to see the result.


The background is blue, so after removing the derived selector, the last one is the id selector.


The id selector is removed and the background is red. Therefore, after the id selector is removed, the last class selector is run.


Remove the class selector and the background is green. Here is a problem. If you replace the group selector and the label selector bit, the background will actually be white.


The following conclusions are as follows: the group selector and the tag selector are in front of each other, and the first two are in front of each other. Then, the class selector is executed, and the next is the id selector, the next step is the derived selector.


Some people may think that the test above is not complete yet. Yes, I can only say that you are very careful. The test above is not complete. You can change the selector order at will and come to the same conclusion as me.


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.