区分css选择器-:first-child 与:first-of-type

<!DOCTYPE html>
<html>
<head>
<style>
/*嫡长子*/
p:first-child
{
background-color:yellow;
}
/*第一个嫡子*/
p:first-of-type
{
color:red;
}
</style>
</head>
<body>
------下面是我儿子first-of-type找到第一个符合条件的子元素--------------
<div>我才是长子(第一个子元素)</div>
<p>嫡子</p>
<p>嫡子</p>

<div>
-----下面是我儿子first-child找到第一个子元素符合条件----------
<p>我是嫡长子</p>
<p>嫡子</p>
</div>

<p><b>注释:</b>对于 IE8 及更早版本的浏览器中的 :first-child,必须声明 <!DOCTYPE>。</p>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/github_37830343/article/details/84286711