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

CSS How to Make a Website Customizing Colors and Fonts Organize CSS with Comments

james li
PLUS
james li
Courses Plus Student 460 Points

Css Selectors

When we select id "wrapper" in main.css, we type: wrapper{} However, when we select class "selected", why do we have to type nav a.selected{}? why can't we just type .selected{} directly like we did with wrapper? Thanks for your reply!

4 Answers

Jacob Miller
Jacob Miller
12,466 Points

A certain ID cannot be used more than once in the HTML, but classes can be used multiple times. So if you write #wrapper you know you are selecting only one element, but if you write .selected you could be selecting multiple elements because that class may be applied to more than the just the anchor in the nav. So it's just a little safer to be more specific and write nav a.selected. It's perfectly valid to write .selected, but in this case I guess they want to be more specific.

maze
maze
17,027 Points

You can type .selected{} directly like wrapper but if you type nav a.selected{} is to asign styles in this particular case, only when the a tag is inside the nav tag with this class.

Example

main.css:

.selected{color:blue;} nav a.selected{color:red;}

index.html:

<nav class="selected"> <a>Home</a> <a class="selected">Contact</a> </nav>

In this example you can see the class selected in the nav tag and the a tag, but "Home" is blue and "Contact" is red.

I hope I explained well!

maze
maze
17,027 Points

Sorry Jacob, I didn't see your answer, jeje.