CSS: the first-of-type selector

The CSS3 :first-of-type selector is a pseudo-class that selects an element only when this is the first element of its type. For example, if you have a paragraph with a series of span elements inside, this selector will match only the very first element of its type. This turns out to be very useful especially when you have to deal with many elements or mixed content. Example:

p span:first-of-type {color: green}
<p>Test <span>this should be green</span> lorem ipsum dolor sit <span>amet</span> et ideo autem <span>aquis</span>.</p>

Demo

Live demo

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Leave a Reply

Note: Only a member of this blog may post a comment.