Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

George Anagnostopoulos
George Anagnostopoulos
3,436 Points

css selectors

difference between child selector and descendant selector

descendant selector example :

ul li {
color:blue; }


child selector example :

ul > li { color:blue; }


applied on an html file, both have the same effect. What is that that differentiates them ?

5 Answers

James Barnett
James Barnett
39,199 Points

To add to @Sam's already excellent answer ...

div li

div > ul

samiff
samiff
31,206 Points

The first one is selecting all li elements in a ul, the second one is selecting any li element that is directly/immediately descended from a parent ul element only. Using the second rule, try wrapping one of the li elements in a span tag and see what happens.

George Anagnostopoulos
George Anagnostopoulos
3,436 Points

oh, i see. i get it now. thanks a lot , I appreciate it !

George Anagnostopoulos
George Anagnostopoulos
3,436 Points

Thank you @james ! The visual explanation helped out.

James Barnett
James Barnett
39,199 Points

@George - Your very welcome, those visual aids are courtesy of selectutorial