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

HTML How to Make a Website Adding Pages to a Website Style New Pages

Matias Valenzuela
Matias Valenzuela
7,554 Points

Using classes

Why in the video Nick used "nav a.selected" and in other parts he only uses " (DOT).social-icon"? To access a class should I just type .(name of the class) or the selector .(name of the class)?

3 Answers

Lauren Clark
Lauren Clark
33,155 Points

nav a.selected refers to the anchor with a .class of selected which is inside the nav element. It depends on how specific you need to get, if you don't want EVERY nav anchor to have the css applied, then you'd be more specific by saying just to select the one with the class of selected.

Here's some resources for better understanding CSS selectors if you're getting a bit lost.

CSS Diner

CSS Vocabulary

James Barnett
James Barnett
39,199 Points

Lauren Clark -

Like woah, CSS Diner, is a pretty neat way to review CSS selectors.

Also, CSS Vocabulary is pretty neat.

Lauren Clark
Lauren Clark
33,155 Points

I love CSS Diner - so much fun. Makes me hungry though!

Hi Matias,

Given the markup for the nav, nav a.selected and nav .selected will both target the same element. In some cases, you can use either one and it won't make a difference.

In this particular case we need to use the more specific selector nav a.selected

Here's the relevant css from the project:

/* nav link */
nav a, nav a:visited {
  color: #fff;
}

/* selected nav link */
nav a.selected, nav a:hover {
  color: #32673f;
}

If you use .selected instead of a.selected I think you'll find that the selected link remains white.

The selected link also happens to be a visited link. Since the visited selector has higher specificity it will take precedence.

nav a:visited is 2 type selectors and 1 class selector
nav .selected is only 1 type selector and 1 class selector

By using nav a.selected, the specificity now matches and whichever one comes later will take precedence. This way the green color will override the white color.