0

Why can't I do div:not(.wrapper2 .exception)? Ie. all divs except .wrapper2 .exception.

http://jsfiddle.net/frank_o/4swjmhtr/

HTML:

<div class="wrapper1">
    <div>
        <p>Hi</p>
    </div>
    <div class="exception">
        <p>Hi</p>
    </div>
</div>
<div class="wrapper2">
    <div>
        <p>Hi</p>
    </div>
    <div class="exception">
        <p>Hi</p>
    </div>
</div>

CSS:

div:not(.wrapper2 .exception) {
    background: blue;
    color: white;
}
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Mark Boulder
  • 13,577
  • 12
  • 48
  • 78

2 Answers2

3

You can't combine not() selectors like this, only one selector at a time can go in.

To select all divs except .exception inside .wrapper2:

div:not(.wrapper2) div:not(.exception)
Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
  • How do you know the OP didn't intend for it to only apply to the last `div`, which is `.wrapper2 .exception`. – Scimonster Sep 23 '14 at 12:13
  • You're right, I misunderstood the question. Updated my answer – Stephan Muller Sep 23 '14 at 12:16
  • Note that this will only work if there are exactly two levels of divs - any more and it will not work correctly. See http://stackoverflow.com/questions/20869061/is-the-css-not-selector-supposed-to-work-with-distant-descendants – BoltClock Sep 23 '14 at 16:41
  • It seems BoltClock and Fabrizio Calderan are right. I think the answer below by Marc is better in retrospect. – Stephan Muller Sep 23 '14 at 16:44
3

If you are targeting all the child div's except for the last one, this is what is needed.

div > div {
    background: blue;
    color: white;
}
div.wrapper2 > div.exception {
    background: none;
    color: inherit;
}
<div class="wrapper1">
    <div>
        <p>Hi</p>
    </div>
    <div class="exception">
        <p>Hi</p>
    </div>
</div>
<div class="wrapper2">
    <div>
        <p>Hi</p>
    </div>
    <div class="exception">
        <p>Hi - not this one!</p>
    </div>
</div>
Marc Audet
  • 46,011
  • 11
  • 63
  • 83