Tip of the Day #21: Prefer the use of first-child CSS selector over last-child

I just got this fantastic tip from Jamie Boyd , a colleague of mine:

The :first-child and :last-child selectors are super-useful for applying alternate styling to items in lists and things like that (e.g. removing the margin from the last item in a container-spanning nav bar). But when it comes to browser support, they are not equal.

:last-child is actually only supported in IE9+, whereas :first-child has had partial support since IE7 (where it works, but styles won?t update if dynamic content is added).

So if you can, use :first-child rather than :last-child.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s