:first-child Pseudo-class

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

Syntax

HTML :first-child { sRules }
Scripting N/A

Possible Values

sRules Value that specifies one or more Cascading Style Sheets (CSS) attribute/value pairs.

Remarks

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

Note   Requires Windows Internet Explorer 7 or later.

Note   Pseudo-class enabled only in standards-compliant mode (strict !DOCTYPE).

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.

<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 below applies indentation to the P of the First fragment only.

<style>
    p:first-child { text-indent: 5px; }
</style> 

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

<!-- 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>

Standards Information

This pseudo-class is defined in CSS, Level 2 Revision 1 (CSS2.1) World Wide Web link.

Applies To

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

See Also

:first-letter, :first-line