The difference between a child selector of a CSS and a descendant selector

Source: Internet
Author: User
Tags pear

Child Selector

There is also a more useful selector sub-selector , the greater than symbol (>), for selecting the first generation of child elements of the specified label element. code in the right-hand code Editor:

. food>li{border:1px solid Red;}

This line of code causes the child element Li (fruit, vegetable) under the class name to add a red solid line border.


<style type="Text/css">

. Food>li{border:1px solid red;} /* Add border Style (1px thickness, solid red color) */

</style>



<H1> food </H1>   <ul class="food">

<li> Fruit

      <ul>

        <li> Banana </li>

        <li> Apple </li>

        <li> Pear </li>

      </ul>

  </li>

  <li> vegetables

    <ul>

      <li> Cabbage </li>

      <li> rape </li>

      <li> Cabbage </li>

    </ul>

  </li>

</ul>


Include (descendant) selectors

contains a selector , which is a space to select the Descendants element under the specified LABEL element . code in the right-hand code Editor:

. First  span{color:red;}

This line of code makes the "timid" font color red in the first paragraph of text content.

Notice the difference between this selector and the sub-selector, where the child selector (children selector) refers only to its immediate descendants, or you can understand the first generation of descendants acting on child elements. The descendant selector is an element that acts on all child descendants. The descendant selector is selected by a space, and the child selector is selected by >.

Summary:> acts on the first generation of descendants of the element, and the space acts on all descendants of the element.

Example:

. Food li{
border:1px solid red;/* Add border style (thickness 1px, solid red color) */

}

[HTML]View PlainCopy
  1. <ul class="food">
  2. <li> Fruit
  3. <ul>
  4. <li> Banana </li>
  5. <li> Apple </li>
  6. <li> Pear </li>
  7. </ul>
  8. </li>
  9. <li> vegetables
  10. <ul>
  11. <li> Cabbage </li>
  12. <li> rape </li>
  13. <li> Cabbage </li>
  14. </ul>
  15. </li>
  16. </ul>


The difference between a child selector of a CSS and a descendant selector

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.