css选择器中:first-child与:first-of-type的区别

这个伪类要站在子元素的角度来看,来看看是不是父元素的第一个。

:first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素。比如有段代码:
在这里插入图片描述

p:first-child 匹配到的是p元素,因为p元素是div的第一个子元素;

h1:first-child 匹配不到任何元素,因为在这里h1是div的第二个子元素,而不是第一个;

span:first-child 匹配不到任何元素,因为在这里两个span元素都不是div的第一个子元素;

然后,在css3中又定义了:first-of-type这个选择器,这个跟:first-child有什么区别呢?还是看那段代码:
在这里插入图片描述
p:first-of-type 匹配到的是p元素,因为p是div的所有类型为p的子元素中的第一个;

h1:first-of-type 匹配到的是h1元素,因为h1是div的所有类型为h1的子元素中的第一个;

span:first-of-type 匹配到的是第三个子元素span。这里div有两个为span的子元素,匹配到的是它们中的第一个。

所以,通过以上两个例子可以得出结论:

:first-child 匹配的是某父元素的第一个子元素,可以说是结构上的第一个子元素。

:first-of-type 匹配的是某父元素下相同类型子元素中的第一个,比如 p:first-of-type,就是指所有类型为p的子元素中的第一个。这里不再限制是第一个子元素了,只要是该类型元素的第一个就行了。

同样类型的选择器 :last-child:last-of-type:nth-child(n):nth-of-type(n) 也可以这样去理解。

举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        p:first-of-type{
            color:red;
        }
        div:first-of-type {
            color: green;
        }
        /* p:first-child{
            color:red;
        }
        div:first-child {
            color: green;
        } */
    </style>
</head>
<body>
    <header class="wrapper">
        <p>aaa</p>
        <p>bbbb</p>
        <p>ddd</p>
        <div>kkk</div>
        <div>nnn</div>
    </header>
</body>
</html>

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* p:first-of-type{
            color:red;
        }
        div:first-of-type {
            color: green;
        } */
        p:first-child{
            color:red;
        }
        div:first-child {
            color: green;
        }
    </style>
</head>
<body>
    <header class="wrapper">
        <p>aaa</p>
        <p>bbbb</p>
        <p>ddd</p>
        <div>kkk</div>
        <div>nnn</div>
    </header>
</body>
</html>

在这里插入图片描述

:first-child和:first-of-typeCSS选择器的两个不同的选择器。 :first-child选择器匹配其父元素的第一个子元素。例如,如果我们有以下CSS代码: p:first-child { background-color: yellow; } span:first-child { background-color: yellow; } 那么它将选择所有p元素和span元素的第一个子元素,并将其背景颜色设置为黄色。\[1\] :first-of-type选择器匹配其父级是特定型的第一个子元素。例如,如果我们有以下CSS代码: p:first-of-type { color: blue; } 那么它将选择所有p元素的第一个子元素,并将其字体颜色设置为蓝色。注意,这里的:first-of-type只要是该型元素的第一个就行了,不要求是第一个子元素了。\[2\] 所以,两个选择器区别在于:first-child选择器只匹配其父元素的第一个子元素,而:first-of-type选择器匹配其父级是特定型的第一个子元素。 #### 引用[.reference_title] - *1* [css:first-child和first-of-type](https://blog.csdn.net/xiaojinguniang/article/details/119887850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [:first-child和:first-of-type区别](https://blog.csdn.net/weixin_46305214/article/details/104644576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [CSS选择器 :first-of-type/:last-of-type/ :first-child/:last-child 用法](https://blog.csdn.net/momo_mom177/article/details/124008659)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

. . . . .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值