:first-child and :last-child

Not supported by Explorer on Windows.

Explorer 5 on Mac and Opera 7 only support :first-child.

How to select an element that is the first or last child of its parent.

The :first-child pseudo class means "if this element is the first child of its parent". :last-child means "if this element is the last child of its parent". Note that only element nodes (HTML tags) count, these pseudo-classes ignore text nodes.

Testsheet:

span {font-weight: 600;}
span:first-child {text-decoration: underline;}
span:last-child {font-style: italic;}

A paragraph with three spans: the first span (bold and underlined) and the second span (bold) and the third span (bold and italic).