:first-child

套用一或多個樣式至作為其父元素第一個子項的任何元素。

語法

:first-child { sRules }

可能的值

sRules

指定一或多個階層式樣式表屬性/值組的值。

備註

:first-child 虛擬類別會與作為某些其他元素第一個子元素的元素相符。

系統不會將內嵌文字視為文件樹狀目錄的一部分,而且在計算第一個子項時也不會列入計算。例如,在下列 HTML 程式碼中,EM 元素是 P 元素的第一個子項:

<p>abc <em>default</em> def</p>

範例

在下列範例中,選取器會與其父元素第一個子系的任何 P 元素相符 (樣式規則只會隱藏第一個段落的縮排):

<style>
    p:first-child {text-indent: 0}
</style> 

先前的選取器會符合後續第一個片段之 DIV 內部的 P 元素,但不會符合第二個片段中的 P 元素:

<!-- First fragment -->
<div class="note">
   <p> The first P inside the note. </p>
</div>

<!-- Second fragment -->
<div class="note">
   <h2>Note</h2>
   <p> The first P inside the note. </p>
</div>

適用範圍

a , abbr , acronym , address , b , big , blockquote , body , caption , center , cite , code , col , colgroup , dd , dfn , dir , div , dl , dt , em , form , hn , html , i , img , input , input type=button , input type=checkbox , input type=file , input type=hidden , input type=image , input type=password , input type=radio , input type=reset , input type=submit , input type=text , kbd , label , legend , li , listing , marquee , menu , ol , p , plaintext , pre , s , samp , select , small , span , strike , strong , sub , sup , table , tbody , td , textarea , tfoot , th , thead , tr , tt , u , ul , var , xmp

另請參閱

概念

:empty
:first-of-type
:last-child
:last-of-type
:nth-child
:nth-last-child
:nth-last-of-type
:nth-of-type
:only-child
:only-of-type
:root

Copyright © 2011 by Microsoft Corporation. All rights reserved.