first-child property

Image Tutorials

Definition and Usage

The :first-child CSS pseudo-class represents any element that is the first child element of its parent.


Syntax

element:first-child { style properties }

Examples

1 span:first-child {
2     background-colorlime;
3 }

…where…

1 <div>
2   <span>This span is limed!</span>
3   <span>This span is not. :(</span>
4 </div>

… should look like …
This span is limed! This span is not. 🙁

Example 2 – Using UL

HTML Content

1 <ul>
2   <li>List 1</li>
3   <li>List 2</li>
4   <li>List 3</li>
5 </ul>

CSS Content

1 ul li{
2   color:red;
3 }
4 ul li:first-child{
5   color:green;
6 }

Compatibility

Desktop browsers

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 3.0 (1.9) 7 9.5 4

Mobile browsers

Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 1.0 (1.9.1) 7 10.0 3.1
Rate article