Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Selectors Going Further with Attribute Selectors and Pseudo-Classes :first-child and :last-child

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Clarity on "child" of "parent"

I understand pseudo classes and can code them when need be, but I'm still confused on why you would code

li:first-child {};

to target the first child in a list. Wouldn't it be the "first-child" of the 'ul' and not the "first-child" of the 'li'?

Joshua Erskine
Joshua Erskine
7,706 Points

Agree, the language is rather counter-intuitive. A more accurate name would be first-occurrence-as-a-child, but that is rather long.

1 Answer

Hey Jason,

The "first-child" pseudo class is used to represent any element that is the first child element of its parent element. You call "first-child" on the child selector you wish to target, not on the parent element. From the MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child