0

0

0

修罗

站点介绍

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

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

修罗 2022-10-17 946 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

  • 1

    1

  • @@5Qa2D

    1

  • 1

    1

  • 1

    1

日历

2025年09月

 123456
78910111213
14151617181920
21222324252627
282930    

文章目录

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