nth-of-type和nth-child的区别

本文详细解释了CSS中nth-of-type与nth-child选择器的区别及使用方法,并通过实例展示了如何为不同类型的子元素设置样式。

---恢复内容开始---

nth-of-type这一类的选择器是针对同类型的子元素进行计算;

nth-child这一类的先择器是连同父元素中的所有子元素一起计算;

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       /*h2:nth-child(even){*/
           /*background: blue;*/
       /*}*/
      /*h2:nth-child(odd){*/
           /*background: aquamarine;*/
       /*}*/
        h2:nth-of-type(even){
            background: blue;
        }
       h2:nth-of-type(odd){
           background: yellowgreen;
       }
    </style>
</head>
<body>
<div>
    <h2> 标题1</h2>
    <p>内容 ......三万字</p>
    <h2>标题2</h2>
    <p>内容 ......三万字</p>
    <h2>标题3</h2>
    <p>内容 ......三万字</p>
    <h2>标题4</h2>
    <p>内容 ......三万字</p>
    <h2>标题5</h2>
    <p>内容 ......三万字</p>
    <h2>标题6</h2>
    <p>内容 ......三万字</p>
</div>
</body>
</html>

 

标题1

内容 ......三万字

标题2

内容 ......三万字

标题3

内容 ......三万字

标题4

内容 ......三万字

标题5

内容 ......三万字

标题6

内容 ......三万字

还有相似的有选择器nth-last-child和nth-last-of-type;

                          

转载于:https://www.cnblogs.com/flxy-1028/p/6053670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值