Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

John Rothra
John Rothra
6,711 Points

Which is better: a.class, .class a, or .class > a? What's the difference?

Which is better (if either)? Why? What's the ultimate difference?

a.class {}

.class a {}

or

.class > a {}

2 Answers

Seth Kroger
Seth Kroger
56,403 Points

All three do different things, none of which are better or worse than the other. The main thing is to realize they are NOT equivalents to each other.

a.class selects

<a class="class">only</a>

.class a {} will select

<div class="class"> <!-- or ul, span, etc. -->
  <...>
     <a></a>
  </...>
</div>

or 

<div class="class"> <!-- or ul, span, etc. -->
   <a></a>
</div>

while .class > a {} selects:

<div class="class"> <!-- or ul, span, etc. -->
   <a></a>
</div>
John Rothra
John Rothra
6,711 Points

Thank you for the clarification! Great info!