In today’s feature post we’ll talk briefly about three new CSS3 pseudo-classes: only-of-type
, first-of-type
and last-of-type
. These are all very similar to the *-nth
classes we covered in an earlier post.
first-of-type
and last-of-type
These two pseudo-classes allow you to select the first and last item in a list of siblings within a particular element. You can use this to color the first item in a list, use opacity
to make the last paragraph on a page fade out or a number of other things. Here’s an example that sets small caps on the first paragraph of a document:
#type-ex1 div:first-of-type {
font-variant: small-caps;
}
only-of-type
is similar to the previous two, but only selects an element if it has no siblings with the same name. Here’s a somewhat contrived example* that will hide single images inside of a div
. If there’s more than one image in the div
they will be visible:
.type-ex2 img:only-of-type {
display: none;
}
And that’s it. Enjoy!
[ * Note: if someone can come up with a better example for only-of-type
I’m all ears. There are very few examples of where this is useful. ]
7 comments