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 trialWen Hsu
580 Pointswhen you need to put html element in front of period "." in class selector?
I don't understand why in previous example we have to put "nav a" in front of period for it to work
nav a.selected { color: #32673f; }
but
.selected { color: #32673f; }
won't work. (I have tried.)
.social-icon { width: 20px; height: 20px; margin: 0 2px; }
and
.profile-photo{ display: block; max-width: 150px; margin: 0 auto 30px; border-radius: 100%; }
will work fine. When we need to put html in front of period "." and when we don't in class selector ?
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Wen,
The reason .selected
won't work is due to css specificity.
You have the following css from the project:
nav a, nav a:visited {
color: #fff;
}
nav a.selected, nav a:hover {
color: #32673f;
}
This works because nav a:visited
has the same specificity as nav a.selected
(1 class and 2 type selectors) so whichever one comes later takes precedence. So a visited link that is selected will take on the darker green color, not white.
This is the css that doesn't work:
nav a, nav a:visited {
color: #fff;
}
.selected, nav a:hover {
color: #32673f;
}
.selected
has a lower specificity than nav a:visited
so even though a link is the selected one it still remains white because nav a:visited
takes precedence even though it comes before the .selected
rule.
To test the use of .selected
for yourself you can do a couple of things.
You can first make sure you're on the portfolio page and then clear you browser history so that "portfolio" is no longer a visited link and you will see that .selected
temporarily works. Once you refresh the page or click on the portfolio link it will be treated as a visited link and remain white.
Secondly, you can temporarily change the css to this:
nav a, :visited {
color: #fff;
}
.selected, nav a:hover {
color: #32673f;
}
This will work similar to the project css because :visited
and .selected
have equal specificity. I don't recommend that you use :visited
by itself in a real project because you won't be able to differentiate between visited nav links and visited links elsewhere on your page.
So in this case, the reason we have to put nav a
in front is because we need to match the specificity of an earlier rule so that we can override it's property/value pair.
Let me know if anything is still not clear.
Adam Duffield
30,494 PointsI think you just need a space between a and .selected :)
Wen Hsu
580 PointsMy question is why .selected won't work in this case. nav a.selected works fine...
Darry Berger
Courses Plus Student 6,818 Pointsin order to use functions such as hover or visited or selected you must use colons not periods.
nav a:selected { color: #32673f; }
Wen Hsu
580 PointsI understand when it's Pseudo-classes such as visited and hover, we have to use : not period. However in this case selected is a class name defined by myself.
Wen Hsu
580 PointsI understand when it's Pseudo-classes such as visited and hover, we have to use : not period. However in this case selected is a class name defined by myself.
Wen Hsu
580 PointsI understand when it's Pseudo-classes such as visited and hover, we have to use : not period. However in this case selected is a class name defined by myself.