找回密码
 立即注册
搜索
查看: 1496|回复: 0

CSS3选择器:first-child、last-child、nth-child 和 nth-last-child

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2015-12-30 18:26:21 | 显示全部楼层 |阅读模式
利用first-child、last-child,nth-child和nth-last-child能够特别针对一个父元素中的第一个子元素、最后一个子元素、指定序号的系元素,甚至第偶数个或第奇数个子元素进行样式的指定。
first-child:子元素中的第一个元素
last-child-child:子元素中的最后一个元素
nth-child() 选择器,该选择器选取父元素的第 N 个子元素,与类型无关。
nth-child(postition),postition代表当前的个数,顺序是从上之下
nth-last-child(postition),postition代表当前的个数,顺序是从下之上
nth-child(odd||even奇数偶数进行判断)
nth-last-child(odd||even奇数偶数进行判断)
odd:奇数 even:偶数
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4.     <meta charset="UTF-8">
  5.     <title>CSS3选择器:first-child、last-child、nth-child 和 nth-last-child</title>

  6. <style>
  7.         li:first-child {
  8.             background-color: yellow;
  9.         }
  10.         li:last-child {
  11.             background-color: blue;
  12.         }
  13.         li:nth-child(3){
  14.             background-color: firebrick;
  15.         }
  16.         li:nth-last-child(2) {
  17.             background-color: darkgreen;
  18.         }
  19.         li:nth-child(even) {
  20.             background-color: darkgreen;
  21.         }
  22.     </style>

  23. </head>
  24. <body>

  25. <h2>列表</h2>


  26. <ul>

  27. <li>列表1</li>


  28. <li>列表2</li>


  29. <li>列表3</li>


  30. <li>列表4</li>


  31. <li>列表5</li>


  32. <li>列表6</li>

  33.     </ul>

  34. </body>
  35. </html>
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 17:29 , Processed in 0.048820 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表