css伪类:first-child和:first-of-type的区别

直接举例说明

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <title>引号</title>
    <style>
    .box, .box2{ border:1px solid #dedede; width:2100px; height:200px; text-align:center}
     .box h1:first-child{color:red}
     .box h2:first-child{color:blue}
     .box h3:first-child{color:green}
     
     .box2 h1:first-of-type{color:red}
     .box2 h2:first-of-type{color:blue}
     .box2 h3:first-of-type{color:green}
     
    </style>
</head>
<body>
<div class="box">
	<h1>H1</h1>
	<h2>H1</h2>
	<h3>H1</h4>
</div>
<div class="box2">
	<h1>H1</h1>
	<h2>H1</h2>
	<h3>H1</h4>
</div>
</body>

效果
在这里插入图片描述
从图片上可以很明显的发现,first-child 只对其父元素的第一个子元素 起作用, 而first-of-type对其父元素下同类型子元素中的第一个 起作用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值