parent>child CSS selector

2019-05-10 16:00发布

问题:

I frequently use this CSS selector parent>child. My design looks good in Mozilla and Opera.

But in IE, it sucks. I know > is not recognizable in IE, but what is the alternate to that in IE?

回答1:

There is no alternate for the direct child selector in IE6 (it should work in IE7 though).

Instead you need to use the descendant selector (a space) and design your classes to compensate.



回答2:

One alternative is to use the universal selector to make a more specific rule which will take effect if the nodes aren't direct children:

div p {color: red;}     // Takes effect if there's a <p> child at some level
div * p {color: black;} // .. but this'll be true if it's not a direct child

However, you need to watch out for specificity conflicts. div * p will be more specific than another rule that works on paragraphs in general, for example.