CSS3 Daily One practice selector-Structural pseudo-class selector

Source: Internet
Author: User

  1. <! DOCTYPE html>
  2. <meta charset= "gb2312" >
  3. <TITLE>CSS3 Daily One practice selector-Structural pseudo-class selector [IV] | Front End Development Network (w3cfuns.com)!</title>
  4. <style type= "Text/css" >
  5. *{margin:0; padding:0;}
  6. #list {font-family: "Microsoft Yahei"; font-size:14px;}
  7. #list DT, #list dd{height:30px; line-height:30px; text-align:center; color: #000; list-style:none;}
  8. #list Dt:nth-child (odd) {color: #fff; background: #900;} /* The odd number of sub-elements of the current DL list if it is a dt child element, it will be selected */
  9. #list Dt:nth-child (even) {background: #090;} /* The sub-element of the current DL list if it is a DT child, it will be selected */
  10. </style>
  11. <body>
  12. <DL id= "List" >
  13. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  14. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  15. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  16. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  17. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  18. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  19. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  20. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  21. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  22. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  23. </dl>
  24. </body>

Copy the code from the above code, we can find that clearly set the odd and even lines of the DT, according to the truth, it should be as if the effect of this operation is:

<ignore_js_op>



This is because Nth-child and nth-last-child are calculated with all child elements in the connected parent element when calculating whether the child element is an odd or even line, in other words, the meaning of Dt:nth-child (odd) does not mean " The current DL list of the odd number of DT sub-elements to calculate, but that "the current DL list of the odd number of sub-elements if it is a dt sub-element, then it will be selected", then this calculation and we "understand" the need for this calculation?



Nth-of-type selector and Nth-last-of-type selector
These two selectors can avoid the above problem, CSS3 is calculated only for the same type of child element when calculating whether the child element is the odd number or the odd number of elements, and the two selectors are used as follows:

  1. <! DOCTYPE html>
  2. <meta charset= "gb2312" >
  3. <TITLE>CSS3 Daily One practice selector-Structural pseudo-class selector [IV] | Front End Development Network (w3cfuns.com)!</title>
  4. <style type= "Text/css" >
  5. *{margin:0; padding:0;}
  6. #list {font-family: "Microsoft Yahei"; font-size:14px;}
  7. #list DT, #list dd{height:30px; line-height:30px; text-align:center; list-style:none; color: #fff;}
  8. #list dd{color: #000;}
  9. #list Dt:nth-of-type (odd) {background: #900;} /* Odd Line */
  10. #list Dt:nth-of-type (even) {background: #090;} /* Even lines */
  11. </style>
  12. <body>
  13. <DL id= "List" >
  14. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  15. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  16. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  17. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  18. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  19. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  20. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  21. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  22. <dt> Front-end Development Network (w3cfuns.com)!</dt>
  23. <dd>web front-end development take-off area, web front-end development experts gathered, tutorials, resources completely FREE! </dd>
  24. </dl>
  25. </body>
Copy Code

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.