CSS Parent/Ancestor Selector

Possible Duplicate:
Is there a CSS parent selector?

I know this is a shot in the dark, but is there a way, using css only, CSS2, no jquery, no javascript, to select and style an element's ancestor? I've gone through the selectors but am posting this in case I missed something or there is a clever workaround.

For example, say I have a table with classname "test" nested inside a div. Is there some sort of:

<div>
    <table class="test">
    </table>
</div>
div (with child) .test
{
     /*styling, for div, not .test ...*/
}

There is no such thing as parent selector in CSS2 or CSS3. And there may never be, actually, because the whole "Cascading" part of CSS is not going to be pretty to deal with once you start doing parent selectors.

That's what jQuery is for :-)


In CSS there is an :empty selector that allows you to match empty elements, you can negate the effect with :not selector.

div:not(:empty) {
    // your styles here
}

However I'm not sure if all browsers support this.


div:not(:empty) {
    margin:0;
}

is NOT recognized by http://jigsaw.w3.org/css-validator/ as CSS2

it's the purpose of CSS to "cascade" down from the more containing to the more specific elements. I guess it's possible for you to "reverse your logic", like in

div.myclass   { /* format parent */ }
div.myclass * { /* neutralize formats in descendants */}
div.myclass img { /* more specific formats for img children */ }

good luck Mike

链接地址: http://www.djcxy.com/p/22824.html

上一篇: 设计父级如果孩子有特定的班级

下一篇: CSS父/祖选择器