Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 827 of the free egghead.io lessons, plus get CSS content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



DOM Hierarchy Pseudo Classes :first-child :last-child :nth-child (demystified)

3:00 CSS lesson by

DOM hierarchy pseudo-classes allow you to style specific elements based on where they fall in the hierarchy and what type of elements they are. You can also use :nth-child to target custom element patterns, like every other element.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

DOM hierarchy pseudo-classes allow you to style specific elements based on where they fall in the hierarchy and what type of elements they are. You can also use :nth-child to target custom element patterns, like every other element.

Avatar
Jaime Rocha

What is the name of that atom theme?

These last pseudo-classes have to deal with the location of the element in the DOM hierarchy. Here, we're back to an unordered list of mostly links. As we scroll down, you can see I just took off some of the styling of the list.

Then here is our first pseudo-class. This is the first child. I am changing the background to this blue color. So you can see the first child -- it's kind of a purplish color -- in the list element is going to be this purple, and this last one as well.

So if I go ahead and comment these out, and save it, then we can look at this next one. We got first child and last child, then this is the nth child. Here, I passed in the parameter of two, which just means the second child.

If I went ahead and put this back to one, this would be the same thing as saying first child, so I'm going to go ahead and put it back to two, because that makes more sense. We can pass through more complex patterns to this nth child.

Here's an example of 2n. You can see that it's going to be all of the even indexes. The indexes start with one -- egghead would be index one -- but the n counting actually starts with zero. It would first pick the zeroth element, but there isn't one in this list. So then it will apply to not one but two, not three but four, etc.

If we want to get the odd elements, then we can say 2n + 1. That way, it'll choose the first element, the third and so forth. You can get more complex with this. This pattern will actually pick the first four elements and apply the style to it.

The reason, again, why it does that is if we pass in zero for n, it would be -0 + 4 which is the fourth element. Then it's -1 + 4, which is the third element, and so on. You can also do something like this, which would be everything except for the first three elements.

So you can do some interesting stuff with the nth child, you can make some complex patterns. There's some other pseudo-classes that you might find helpful. There's the nth last-child pseudo-class, which is the same thing as nth child. It just counts from the bottom going up.

This only-child applies to every element that is an orphan. Then there's only-of-type, which will allow you to target a specific element type. So if you wanted to get a paragraph that was all alone, regardless of what other sibling nodes were around it, you could use this.

If you wanted to get last-of-type, again, if you wanted to get the last paragraph of the section, you could use this one. Then you've got nth-of-type. It has the same pattern that you can use for the nth child, and same thing with nth-last-of-type. It does the same thing, but it's counting backwards up.

So you can get really complex with some of the pseudo-classes. In fact, some of them make it a little bit hard to read and maintain the code, so you're going to want to be careful with these. Just use them where they make sense.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?