I have a general rule which gives all DIVs a background image.
I have one div (with id='a') which I don't want it to have the background image.
What css rule do I have to give it?

- 28,547
- 16
- 75
- 90

- 52,579
- 61
- 190
- 278
11 Answers
Try:
div#a {
background-image:none
}

- 187,081
- 35
- 232
- 306
-
13Just use `#a {}`. [Don't use element types in selectors unless necessary](http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml#Type_selectors). – Jezen Thomas Jul 18 '12 at 14:51
-
3"Avoiding unnecessary ancestor selectors is useful for performance reasons." Not really an issue when you are not Google. – Davor Dec 26 '13 at 16:04
-
W3C Recommendation on CSS 3 Selectors > Calculating a selector's specificity https://www.w3.org/TR/css3-selectors/#specificity – TarranJones May 09 '16 at 13:01
div#a {
background-image: none !important;
}
Although the "!important" might not be necessary, because "div#a" has a higher specificity than just "div".

- 94,805
- 45
- 217
- 260
-
The !important part is not necessary as the declaration with #a is more precise than a declaration of div and therefore this rule will be applied instead of the general div rule. – Ruud Sep 22 '09 at 16:20
-
2
div#a {
background-image: url('../images/spacer.png');
background-image: none !important;
}
I use a transparent spacer image in addition to the rule to remove the background image because IE6 seems to ignore the background-image: none
even though it is marked !important
.

- 301
- 3
- 4
-
7
-
-1 Your answer is incorrect. I started IE6, and the `background-image:none;` (with ot without `!important`) works flawlessly. – Rob W Oct 15 '12 at 14:06
-
8Actually, I care - one of my clients has an isolated network environment with some specific software that hasn't been updated. IE6 is what they have to use. Sad but true :( – Russ Clarke Nov 02 '12 at 11:49
-
5Have an upvote to mitigate Rob W's downvote. It was quite rude of him to downvote just for backwards compatibility that isn't even a hack, regardless of if he could reproduce it or not – Kavi Siegel Jun 19 '13 at 15:25
Since in css3 one might set multiple background images setting "none" will only create a new layer and hide nothing.
http://www.css3.info/preview/multiple-backgrounds/ http://www.w3.org/TR/css3-background/#backgrounds
I have not found a solution yet...

- 81
- 1
- 1
When background-image: none !important;
have no effect.
You can use:
background-size: 0 !important;

- 31
- 2
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(0.5, #fff));
background-image: -webkit-linear-gradient(center top, #fff 0%, #fff 50%);
background-image: -moz-linear-gradient(center top, #fff 0%, #fff 50%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ffffff', GradientType=0);
background-image: linear-gradient(to bottom, #fff 0%, #fff 50%);
for older browsers.. if you have defined css in some framewokrk.css like select2.css in IE9 background-image: -webkit-gradient etc. and you want it via another .css rewrite with "background-image: none !important" not works. I used same color to color gradient like page background color.

- 41
- 2
If your div rule is just div {...}
, then #a {...}
will be sufficient. If it is more complicated, you need a "more specific" selector, as defined by the CSS specification on specificity. (#a being more specific than div is just single aspect in the algorithm.)

- 4,397
- 25
- 36
HTML :
<div id="a" class="mydiv"></div>
CSS:
div#a {
background-image:none;
}
Another Way:
div:not(#a) {
//all rules goes here
//add image here
//div with id a not effected by these rules
}
Multiple (not pseudo)
div:not(#a):not(#b):not(#c) {
//all rules goes here
//add image here
//div with ids not effected with these rules
}

- 2,263
- 1
- 18
- 12
Doesn't this work:
.clear-background{
background-image: none;
}
Might have problems on older browsers...

- 2,336
- 5
- 31
- 40

- 1,927
- 5
- 25
- 39
Replace the rule you have with the following:
div:not(#a) { // add your bg image here //}

- 1,967
- 12
- 28