views:

213

answers:

6
+2  Q: 

CSS Selectors

What is the difference between these two CSS statements:

h1 em { color:#ddd; }

and

h1 > em { color:#ddd; }

As far as I can tell they do exactly the same thing (though according to what I've read at W3C in the first case em is considered a 'descendant' where as in the second it is considered a 'child', though I have no idea how that is actually different). Can anyone explain how these are different and why you would choose to use one syntax over the other. I've always just used the first method, but every now and again I run across the second style in other people's code.

+12  A: 

This one:

h1 em { color:#ddd; }

matches any em that's within an h1, whether it's a child, grandchild, great-grandchild, etc. For instance:

<h1><strong><em>This matches</em></strong></h1>

This one:

h1 > em { color:#ddd; }

matches only an em that's a direct child of an h1. So:

<h1><strong><em>This doesn't match</em></strong></h1>
<h1><em>But this does</em></h1>
RichieHindle
Thanks for clearing that up. One of those things that I'd always kind of wondered about but was able to get away with out using. I can see how that would come in handy.
Note that the > selector is not supported in Internet Explorer 6. It is supported in IE7 and IE8.
Sander Marechal
And just to confirm the terminology, the first is the descendant selector, the second is the child selector. For the difference between “descendant” and “child”, you want dictionary.com.
Paul D. Waite
"For the difference between “descendant” and “child”, you want dictionary.com" Well said Paul. Is the OP's first language not English? Even if it isn't, they know enough English to write the question, they should know enough to look up two words.
AmbroseChapel
Well, it’s not always easy to switch between reading English and reading code, I think they’re different mindsets (which I think is why [Jeff and others find pseudocode hard](http://www.codinghorror.com/blog/archives/001264.html)). But hopefully getting those concepts into your head will help with understanding HTML and CSS.
Paul D. Waite
+2  A: 

Probably not the best example that you've given but I'll run with it:

<h1>Stuff here    
  <em>Something
    <p>More stuff</p>
    <p>Something <em>here</em></p>
  </em>
<h1>

h1 em will match both em in my example.

h1>em will only match the em that is a direct descendant of the h1 not the second inner em.

rball
A: 
h1 em { color:#ddd; }

means: "em within h1"

h1 > em { color:#ddd; }

means: "em with parent h1"

eKek0
A: 

A descendant is any element that appears below the containing element at any point in the DOM tree (a child, grand-child type relationship). A child is an element that is directly below the containing element in the DOM.

The real world implications in using this are that the child selector, alongside the sibling selector, are not support by IE6.

roryf
A: 

Just fyi, there are still a lot of browser compatibility issues with using > and it is a case where I would use jQuery or at least test in all your target browsers before deploying.

jarrett
A: 

The child selector in this the > symbol lets you select only those em tags that are direct descendants of an h1 tag.

Example is the easiest way to explain it:

<h1>H1 Tag <em id="first">child selector<em id="second">not a direct child descendant</em></em> </h1>

In this case the first em tag will have the h1 > em style and the h1 em style applied, however the second em tag or the non direct child will not have h1 > em style applied since it is not a direct child or descendant of the h1.

You can see the doctree spec here: http://www.w3.org/TR/CSS2/conform.html#doctree

jtyost2