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
John Fisher
7,974 PointsWhat's the difference between these selectors?
Hi
I'm having trouble understanding what the difference is exactly between these two selectors...
div span {
color: blue;
}
and
div > span {
color: blue;
}
Don't they do the same thing?
5 Answers
moonshine
8,449 Pointsdiv > span {
color: blue;
}
The ">" selects spans that are immediate children of a div.
<div>
<span> <!-- selected -->
<span> <!-- not selected -->
</span>
</span>
</div>
Likewise:
div span {
color: blue;
}
<div>
<span> <!-- selected -->
<span> <!-- selected -->
</span>
</span>
</div>
Robert Richey
Courses Plus Student 16,352 PointsHi John,
When two elements are separated with a space, that is considered an explicit Descendant selector, which - in your example - will select all span elements that are children of div elements, no matter how far down the hierarchical tree they go.
Whereas the greater than symbol is a Child selector that will select all span elements that are direct children of div elements.
Hope this was helpful!
Cheers
Brian Romine
9,518 PointsFloris has it, > indicates only immediate children.
div > span would select ONLY the spans that are direct children of a div.
div span would select ALL spans that are children of a div.
Ayoub AIT IKEN
12,314 Pointsin addition to what Floris Creyf has said, the child selector ( div > span ). would also select and other span tag that is a child of a div tag.
<div>
<span> <!-- selected -->
<span> <!-- not selected -->
</span>
</span>
</div>
<div>
<span> <!-- selected --></span>
</div>
John Fisher
7,974 PointsRight, after a bit of experimenting in my text editor with your examples, I think I've got it.
Thanks.