:first-child pseudo-class

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

CSS 2.1, Section 5.11.1

Syntax

selector:first-child {...}

Parameters

selector

A CSS simple selector.

Standards information

Remarks

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

Note   Requires a Windows Store app using JavaScript 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>

See also

Reference
::first-letter
::first-line

 

 

Show:
© 2014 Microsoft