Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Target Custom HTML Element Patterns with DOM Hierarchy Pseudo Classes in CSS


    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 (odd or even).



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: 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 most siblings. As we scroll down, you can see...I just took of some of the styling of the list.

    Here is our first pseudo-class. This is the first child. I'm changing the background to this blue color. You can see the first child. It's kind of a purplish color. The first child in the list element is going to be this purple and this last one as well.

    If I go ahead and comment these out and save it, then we can look at this next one. We got first child, then last child, and this is the nth child. Here, I passed in the parameter of 2, which just means the second child. If I want to head and put this back to 1, this would be the same thing as same first child.

    I'm going to go ahead and put it back to 2, because that makes more sense. We can pass through more complex patterns to this nth child. Here is an example of 2n. You can see that it's going to be all of the even indexes. Indexes start with 1, egghead would be index 1, but the n counting actually starts with 0We'd first pick the 0th element, but there isn't one in this list, so it'll apply to not 1 but 2, not 3 but 4, 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 0for n, it would be -04, which is the fourth element, and 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. You can do some interesting stuff with the nth child. You can make some complex patterns.

    There are some other pseudo-classes that you might find helpful. There is the nth last child pseudo-class which is the same thing as nth child that just counts from the bottom going up. This only child applies to every element that is an orphan.

    Then, there is only of type which will allow you to target a specific element type. If you want to get a paragraph that was all alone regardless of other sibling, nodes were around it, you could use this.

    If you want to get last of type again if you want to get the last paragraph in a section, you could use this one.

    Then, you've got nth of type that 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 coming backwards up.

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