tags:

views:

118

answers:

3

Let say I got this page:

<body>
    <h1>Hello!</h1>
    <div class="wrapper">
        <div class="anotherclass">
            <h1>Another heading 1</h1>
        </div>
        <div class="yetanotherclass">
            <h1>Yet another heading 1</h1>
        </div>
    </div>
    <h1>Good bye!</h1>
    <div class="class">
        <h1>Good bye. And this time I mean it.</h1>
    </div>
</body>

And I want to select all H1 elements that are NOT within the wrapper-class. How can I do that with CSS? I don't want a "solution" like

body h1, body .class h1 {style definitions}

I'm more after some kind of this:

h1:not(.wrapper > h1) {style definitions}

Is there any way to do this?

+2  A: 

What if you did something like this:

h1 { /* rules for everything without the class */ }
h1.class { /* rules for everything with the class */ }

In h1.class you would override everything that you defined in your h1 rule.

Here is an example:

<html>
    <head>
        <style type="text/css">
                div { color:#00f; }
                div.foo { color:#f00; }
        </style>
    </head>
    <body>
        <div class="foo">foo</div>
        <div>bar</div>
        <div>baz</div>
    </body>
</html>

In this example I have effectively targeted all divs that do not have a class of foo.

Andrew Hare
That's not possible with my current solution. I can't change the HTML-code since it's loaded with a Javascript from an external server, so I can't set a class on the headings.
Zyberzero
I edited my answer to remove the classes on the example. You will not need to change the HTML at all as the first rule will set the style for _all_ elements and the second rule will only apply to the one with the class you want to exclude.
Andrew Hare
A: 

You can use the universal selector * to apply global styling and then apply a nested universal selector: .wrapper * to undo the styling you applied originally

* {font-weight:bold; border:thin solid red;}

.wrapper * {font-weight:normal; border: 0px solid white;}
James Conigliaro
The thing is I'm going to use this selector thing as an input to a javascript which sort of understands css selectors - but not those nested variants. It must be a "one-row" thing
Zyberzero
"sort of understands css selectors" suggests jQuery. You can certainly add classes target parents, etc. in jQuery.
Steve Perks
It's actually a sIFR implentation I'm doing combined with a Rich Text editor. The rich text editor is within a specific div that contains elements of the type h2 and h3 and for those I don't want sIFR to replace the text but on every other occurring element of that type.
Zyberzero
A: 

You can't do what you're asking with css. The whole idea around css is the cascade, and what you're wanting to do is work against the flow of the cascade.

Work with the tide do regular css:

h1 {style definitions}
.wrapper h1 {style definitions}
Steve Perks