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 Selectors Using Combinators in Selectors

What's the precedence of combinators compared to a descendent selector? Or all other rules in general?

I am curious to know precedence order when it comes to combinators and selectors. Does the element, class than ID precedence still apply implicitly in that order.

For example, .main + p will overrule .main p? I could try it and figure it out, but this may be a question that will come to surface for others as well. And someone may throw in a curveball I've never thought about??

Any answers or clarification will be greatly appreciated.

1 Answer

Hi Phouthalang,

Even though I think it would have been good to fiddle out yourself, I made an example that shows you the difference between selectors ".main p" and ".main + p".

The demo can be found at: http://jsfiddle.net/dq4vfxju/

Explanation:

  • CSS selector .main p means select all p-(tag)elements within an element having the main-class
  • CSS selector .main + p means select the next p-(tag)element after a .main-element. In other words the next sibling, meaning it is nested at the same level.

Cheers!