67

CSS Code(what I need)

<style>
    div[id!='div1']// I actually needed an inequality operator for NOT EQUAL TO
    {
        font-size:40px;
    }
</style>

HTML code

<body>
    <div>abc</div>
    <div>def</div>
    <div id='div1'>ghi</div>
</body>

The CSS didn't work as I intended.

I actually wanted to define the style for all <div>-elements except the one with id='div1'.

How can I do that?

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57

3 Answers3

108

Use the :not selector:

div:not(#bar){
    color:red;
}
<div>foo</div>
<div id="bar">bar</div>

Update : name instead of ID:

div:not([name="bar"]){
    color:red;
}
<div>foo</div>
<div name="bar">bar</div>

Update: CSS2 selector, similar answer to Tom Heard-s:

div{
    color:red;
}

div[name="bar"]{
    color:blue;
}
<div>foo</div>
<div name="bar">bar</div>

Also, see selectivizr

Vucko
  • 20,555
  • 10
  • 56
  • 107
20

CSS3 solution:

div:not(#div1)

CSS2 solution:

div {
    color: red;
}

div#div1 {
    color: inherit;
}

By name:

CSS3 solution:

div:not([name=div1]) {
    color: red;
}

CSS2 Solution (note not supported in IE6 - but who cares anymore):

div {
    color: red;
}

div[name=div1] {
    color: inherit;
}
Tom Heard
  • 1,278
  • 10
  • 20
  • Tom, Coincidentally I asked for `id`-validation. But say I needed to check an inequality on the `name`-attribute of the element, then what should I do? – Rajesh Paul Oct 19 '13 at 10:14
  • why it doesn't work for me in `*:not(#email)`, that is, when used for all elements –  Dec 09 '14 at 14:49
  • 1
    @nakinlaulu I have no idea why that doesn't work (it doesn't for me either) but you can do something like `body *:not(#email)` and it will work. See http://jsfiddle.net/1ty8ns3k/1, sorry I couldn't give a better answer – Tom Heard Dec 16 '14 at 00:31
2

If you are only to target browsers supporting CSS3 selectors you could do this:

div:not(#div1) {
...my styles...
}
Michael Falck Wedelgård
  • 2,943
  • 1
  • 27
  • 39