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
Reggie Sibley
6,018 PointsSelectors...
Ok so lets say i'm trying to target the <p> element which is nested within the <h1> element right below it in my HTML document with CSS, whats the difference between just selecting
h1 p { background: red; }
OR..........
h1 + p { background:red; }
whats a scenario were they will work differently depending on the situation? because even though the <p> element is not nested right below the <h1> you can still select it with...
h1 ~ p { background:red; }
but i know this will select every <p> element. But my overall question is what's the biggest difference when using these selectors to select an element?
1 Answer
Tim Rohweder
25,527 PointsHi Reggie,
The first one --- h1 p {background: red; } --- is a descendant selector. If you had a paragraph nested inside an h1 tag (unlikely, you would do this...), you would use a selector like that.
The second one --- h1 + p --- is an adjacent sibling combinator selector. So in this case, it will select each paragraph that comes immediately after an h1 tag.
Check out this article for an easy-to-understand and more thorough explanation:
http://css-tricks.com/child-and-sibling-selectors/
Hope that helps!
-Tim