November 28, 2012

Using the :nth child selector in CSS3

There is a CSS selector called nth-child. If you put simply a number in the parentheses, it will match only that number element. For example, here is how to select only the 3rd element: The :nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent.

[code] ul li:nth-child(3) { color: #ccc; } [/code] 

This selects the 3rd element in a list of related elements and changes its color. nth-child also accepts two keywords in the parentheses spot: even and odd. Those should be pretty obvious. “Even” selects even numbered elements, like the 2nd, 4th, 6th, etc. “Odd” selects odd numbered elements, like 1st, 3rd, 5th, etc.

[code] ul li:nth-child(even) { color: #ccc; } ul li:nth-child(odd) { color: #ccc; } [/code] 

It is also possible to simply select the last element in a list.

[code] ul li: last-child { color: green; } [/code] 

Conclusion:

As you can see, CSS3 makes it a lot easier to select specific elements in a list and style them accordingly in an intuitive fashion. Interestingly enough, the nth-child selector can also be applied to divs in the same way. If you are interested in playing around with the nth-child property, go here. CSS-tricks has put together a very cool nth Tester.

Demo

Below, are some examples of using the nth child selector on paragraphs:

CSS

[code] ul li:nth-child(even) { color: rgb(0,0,255); } ul li:nth-child(odd) { color: rgb(0,150,0); } ul li:last-child { color: rgb(139,134,78); } [/code] 

Result

Itemized List

Support:

The :nth-child() selector is supported in all major browsers, except IE8 and earlier.

Paul Weinert
Author

Paul Weinert is our founder, managing principal and spirit animal. He’s one of those odd people you meet that was born in Portland, grew up here and still lives here for some reason. If you had two words to describe Paul after meeting him, we’d bet you’d use “charmingly quirky”.

Paul started GRAYBOX in February 2009, and for the first four years, GRAYBOX had a blend of a contract staffing firm and a traditional agency. GRAYBOX then hired our first employees in 2012 and has been growing ever since. Professionally, Paul has a background in internet business consulting, working especially with mid-market ecommerce, healthcare, and tech companies. He worked as a user experience designer, web developer and internet marketer. Nowadays, Paul serves as our company visionary, team mentor and head of executive leadership — Paul is extremely proud of the entire team and the company. He’s constantly humbled by the team’s awesomeness and the caring community that’s sprung up from his initial ideas.

Beyond GRAYBOX, Paul is married, has two young boys and lives in NE Portland. He’s also an active foster parent, mentors other local entrepreneurs, and volunteers on local non-profit boards. Businesswise, aside from GRAYBOX, Paul has started a few other businesses and is an active investor in the local startup scene.

For the sheer joy of it, let’s play 2 truths and a lie:

  1. Paul firmly believes that any activity would be better if on a boat.
  2. Other than this whole Internet thing, Paul has basically no other skills or expertise in anything.
  3. Paul is rare unicorn, actually born and raised in Portland.