1. 程式人生 > >區分css選擇器-:first-child 與:first-of-type

區分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>