0

0

0

修罗

站点介绍

只有了解事实才能获得真正的自由

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

修罗 2022-10-17 1035 0条评论 DIV+CSS

首页 / 正文

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

选择器:nth-child

选择器:nth-of-type

都是找该选择器父元素的子元素;

案例

</head>   
    <style>
        .nth:nth-child(3){
            background-color: red;
        }
        .nth:nth-of-type(3){
            background-color: green;
        }
    </style>
</head>
<body>
    <div class="nth">1</div>
    <div class="nth">2</div>
    <p class="nth">3</p>
    <p class="nth">4</p>
    <div class="nth">5</div>
    <p class="nth">6</p>
</body>

16659891566900.png

.nth:nth-child(3)

1.找到.nth元素父元素下所有的子元素,这里body是父元素;

2.找到第3个元素, 再对该元素匹配类选择器.nth, 匹配成功则添加样式;

.nth:nth-of-type(3)

1.找到.nth元素父元素下所有的子元素,这里body是父元素;

2.找到其中满足类选择器.nth的元素,并归类得到两种标签名:divp(这就是nth-of-type中type的含义);

3.对body下所有的元素按以上的标签名分组;

#第一组
<div class="nth">1</div>
<div class="nth">2</div>
<div class="nth">5</div>
#第二组
<p class="nth">3</p>
<p class="nth">4</p>
<p class="nth">6</p>

4.然后分别找到每组内第3个元素,分别对找到的第3个元素匹配类选择器.nth

评论(0)


最新评论

  • 城市地標

    我喜欢, 这里分享真实经验。你的项目 就是 属于这里的。请继续。

  • 宗教地標

    我热爱这样的想法, 去那么多国家。真的很鼓舞。

  • 觀景高地

    能感受到热爱。继续保持 感受。

  • 最佳季節

    我关注这样的资源, 写得很实在。你的网站 就是 属于这里的。加油。

  • 1

    1

  • 1

    1

  • -1' OR 2+158-158-1=0+0+0+1 or 'TKCTZnRa'='

    1

  • 1

    1

  • 1

    1

  • 1

    1

日历

2026年01月

    123
45678910
11121314151617
18192021222324
25262728293031

文章目录

推荐关键字: Linux webpack js 算法 MongoDB laravel JAVA jquery javase redis