A possible mistaken understanding of the offspring selector of small tip:css--Zhang Xin Xu

Source: Internet
Author: User

A question about the class selector

Suppose there is a question on the following side, the CSS code is as follows:

. red {color:red;}. Green {Color:green;}

The HTML is as follows:

<div class= "Red" ><div class= "green" ><p>1. What's the color? </p></div></div><div class= "Green" ><div class= "Red" ><p>2. What's the color? </p></div></div>

Q: What are the first and second lines of text color, respectively?

A. Green and GreenB. Red and RedC. Green RedD. Red and Green

This example is very simple, I guess basically can answer correctly: The deeper the DOM the higher the class name weight, therefore, the first line of the text red, the second line of text green first line of text green, the second line of text red! So the answer to the previous question is C.

You can get a hard click here: CSS class name selector order and hierarchy weights

Second, slightly modified: class selector → descendant selector

One more question, what if we changed the class selector of the previous example to the descendant selector?

. red p {color:red;}. Green p {color:green;}

HTML remains unchanged:

<div class= "Red" ><div class= "green" ><p>1. What's the color? </p></div></div><div class= "Green" ><div class= "Red" ><p>2. What's the color? </p></div></div>

Q: What are the first and second lines of text color, respectively?

A. Green and GreenB. Red and RedC. Green RedD. Red and Green

Experience can be translated into figurative understanding to help us remember, however, it is easy to form a fixed mind and create a false understanding of some similar situations.

If we follow the example of a class selector at the beginning to insinuate this similar example, you might get the wrong answer-this example is notthe correct option for C.

What should be the correct option?

You can ruthlessly click here: CSS descendant selector order and level weights demo

You can see in each browser that the first and second lines of text are green, as shown below:

This means that thecorrect option should be a.

Why?

Seemingly this descendant selector scenario, the ancestor selector corresponds to the DOM no matter how deep and shallow are the same level, and ultimately the role of the selector declaration in the CSS style in the order, that is, the precedence of the following CSS declaration.

In this example, .green p { color: green; } the .red p { color: red; } following is displayed, so the two lines of text are green. If we change to the following:

. green p {color:green;}     /* I am on, soy sauce life */.red p {color:red;}     /* I'm under, show Me */

Then the two lines of text become red.

If you are currently on hand is a modern browser, you can click here, edit the next CSS code order (Firefox cut paste some problems, manual input), you will see the right text is red ~ ~ ~

Three, leave a question again

Change the CSS code:

: Not (. Green)  p {color:red;}. Green p {color:green;}

The HTML code remains unchanged:

<div class= "Red" ><div class= "green" ><p>1. What's the color? </p></div></div><div class= "Green" ><div class= "Red" ><p>2. What's the color? </p></div></div>

Q: What are the first and second lines of text color, respectively?

You can write down your answer in the form of a comment ~ ~

Original article, reprint please indicate from Zhang Xin Xu-Xin space-Xin Life [http://www.zhangxinxu.com]
This address: http://www.zhangxinxu.com/wordpress/?p=2286

(End of this article)

A possible mistaken understanding of the offspring selector of small tip:css--Zhang Xin Xu

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.