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 trialRyan La Forge
1,670 PointsMisleading quiz question regarding selectors?
In this quiz, there is a question asking which selector is the most specific. The options are
- ID Selector
- Class Selector
- Descendent Selector
- Type Selector
The answer was stated to be an ID selector; however, isn't it really a descendent selector? Since it's really a composition of two selectors you could write
body #id {background-color: blue;}
The corresponding id selector would simply be
#id {background-color: red;}
In the above example, the background-color would end up being set to blue because it is more specific. Doesn't that mean descendent selectors can be the most specific?
Using the following CSS in the example code for Psuedo classes, the resulting background-color is blue.
body #tag {
background-color: blue;
}
#tag {
background-color: red;
}
1 Answer
Michael Hulet
47,913 PointsIn this case, the background color is blue because the combination of the id
selector and the descendent selector makes it more specific than just the id
selector afterwards. If you change the descendent selector to be body div
instead of body #tag
, the background becomes red, regardless of if you put the id
selector before or after the descendent one. This shows that an id
selector is more specific than just a descendent selector on its own, which is what this quiz is asking