CSS3新增结构伪类选择器

5 篇文章 1 订阅

CSS3新增结构伪类选择器

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>CSS3新增结构伪类选择器</title>
    <style>
      /* 1. 选择ul里面的第一个孩子 li标签 */
      ul li:first-child {
        background-color: pink;
      }
      /* 2. 选择ul里面的最后一个孩子 li标签 */
      ul li:last-child {
        background-color: pink;
      }
      /* 3. 选择ul里面的第2个孩子 li标签 */
      ul li:nth-child(2) {
        background-color: skyblue;
      }
      ul li:nth-child(6) {
        background-color: skyblue;
      }
    </style>
  </head>
  <body>
    <ul>
      <li>第1个孩子</li>
      <li>第2个孩子</li>
      <li>第3个孩子</li>
      <li>第4个孩子</li>
      <li>第5个孩子</li>
      <li>第6个孩子</li>
      <li>第7个孩子</li>
      <li>第8个孩子</li>
    </ul>
  </body>
</html>

1

CSS3新增结构伪类选择器-nth-child

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>CSS3新增结构伪类选择器-nth-child</title>
    <style>
      /* 把所有的偶数 even的孩子选出来 */
      ul li:nth-child(even) {
        background-color: pink;
      }

      /* 把所有的奇数 odd的孩子选出来 */
      ul li:nth-child(odd) {
        background-color: aqua;
      }
      /* nth-child(n) 从0开始 每次加1 往后面计算  这里面必须是n 选择了所有的孩子*/
      /* ol li:nth-child(n) {
            background-color: pink;
        } */
      /* nth-child(2n) 选择了所有的偶数孩子 等价于 even*/
      /* ol li:nth-child(2n) {
            background-color: pink;
        } */
      /* nth-child(2n+1)  选择了所有的基数孩子 等价于odd*/
      /* ol li:nth-child(2n + 1) {
        background-color: skyblue;
      } */
      /* 从第三个开始选 */
      /* ol li:nth-child(n+3) {
            background-color: pink;
        } */
      /* 前三个 */
      ol li:nth-child(-n + 3) {
        background-color: pink;
      }
    </style>
  </head>

  <body>
    <ul>
      <li>我是第1个孩子</li>
      <li>我是第2个孩子</li>
      <li>我是第3个孩子</li>
      <li>我是第4个孩子</li>
      <li>我是第5个孩子</li>
      <li>我是第6个孩子</li>
      <li>我是第7个孩子</li>
      <li>我是第8个孩子</li>
    </ul>
    <ol>
      <li>我是第1个孩子</li>
      <li>我是第2个孩子</li>
      <li>我是第3个孩子</li>
      <li>我是第4个孩子</li>
      <li>我是第5个孩子</li>
      <li>我是第6个孩子</li>
      <li>我是第7个孩子</li>
      <li>我是第8个孩子</li>
    </ol>
  </body>
</html>

2

CSS3新增选择器nth-type-of

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>CSS3新增选择器nth-type-of</title>
    <style>
      ul li:first-of-type {
        background-color: pink;
      }
      ul li:last-of-type {
        background-color: pink;
      }
      ul li:nth-of-type(even) {
        background-color: skyblue;
      }
      /* nth-child 会把所有的盒子都排列序号 */
      /* 执行的时候首先看  :nth-child(1) 之后回去看 前面 div */

      section div:nth-child(1) {
        background-color: red;
      }
      /* nth-of-type 会把指定元素的盒子排列序号 */
      /* 执行的时候首先看  div指定的元素  之后回去看 :nth-of-type(1) 第几个孩子 */
      section div:nth-of-type(1) {
        background-color: blue;
      }
    </style>
  </head>
  <body>
    <ul>
      <li>我是第1个孩子</li>
      <li>我是第2个孩子</li>
      <li>我是第3个孩子</li>
      <li>我是第4个孩子</li>
      <li>我是第5个孩子</li>
      <li>我是第6个孩子</li>
      <li>我是第7个孩子</li>
      <li>我是第8个孩子</li>
    </ul>
    <section>
      <p>光头强</p>
      <div>熊大</div>
      <div>熊二</div>
    </section>
  </body>
</html>

在这里插入图片描述

1
2
3
ps:(三张图来自黑马程序员)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值