Applies one or more styles to any element that is the first child of its parent.

Syntax

:first-child { sRules }

Possible values

sRules

Value that specifies one or more cascading style sheets attribute/value pairs.

Remarks

The :first-child pseudo-class matches an element that is the first child element of some other element.

Inline text is not considered to be part of the document tree, and is not counted when calculating the first child. For example, the EM element is the first child of the P element in the following HTML code:

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

Examples

In the following example, the selector matches any P element that is the first child of its parent (the style rule suppresses indentation for the first paragraph only):

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

The preceding selector would match the P element inside the DIV of the first fragment that follows, but would not match the P element in the second fragment:

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

Applies to

A, ABBR, ACRONYM, ADDRESS, B, BIG, BLOCKQUOTE, BODY, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, DD, DFN, 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, LI, OL, P, S, SELECT, SPAN, SUB, TABLE, TBODY, TD, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP

See also