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 CSS Foundations Values and Units Numeric and Textual Data Types

Becky Castle
Becky Castle
15,294 Points

which is correct syntax: "a.div {}" or "div a{}"???

I'm confused. Which is correct syntax for selecting classes and ids of links: "a.div { }" or "div a { }", "a#div { }" or "#div a { }" Thanks!

4 Answers

James Ingmire
James Ingmire
11,901 Points
#div a {}

This would be selecting the link inside a div with an ID selector.

.div a {}

This would be selecting the link inside a div with a CLASS selector.

You would not call a div div tho, i presume you know this. e.g. The HTML

<div class="myfirstdiv"><a href="www.link.com">click here</a></div>

And the CSS

.myfirstdiv a { color: blue;}
Becky Castle
Becky Castle
15,294 Points

Thanks for your speedy reply, James. I Thought so. I was just taking a quiz, and it wouldn't let me pass the level when I used ".div a { }". I had to use "a.div { }" instead. Must be just a glitch in the quizzing program. Thanks again!

James Ingmire
James Ingmire
11,901 Points

No problem, hope helps.

Although doubt will be a glitch in the program as you can use "a.div{}" if that (a)nchor element had a class assigned to it, then it would work and you would be targeting the div inside the anchor element. Little confusing so ignore this for now if you like but worth noting that is possible for the future.

Becky Castle
Becky Castle
15,294 Points

I see that I did phrase my question in a confusing way -- I should have used an example like ".myclass" not ".div". In actuality, the focus of the lesson I was trying to pass had little to do with that -- that's why I suspect it was a glitch. However, it did confuse me because I ended up having to write my code like this: "a.myclass { }" instead of ".myclass a { }". Thanks so much for helping me out!

James Ingmire
James Ingmire
11,901 Points

No problem, to be honest its confused me now thinking about it!! lol, hope i am correct and it all works out!