209

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?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278

11 Answers11

372

Try:

div#a {
    background-image:none
}
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 13
    Just 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
36
div#a {
    background-image: none;
}
Ruud
  • 3,118
  • 3
  • 39
  • 51
32
div#a {
  background-image: none !important;
}

Although the "!important" might not be necessary, because "div#a" has a higher specificity than just "div".

M4N
  • 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
    I found !important to be necessary in Chrome – dlchambers Aug 27 '15 at 21:35
18
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.

MatthewPearson
  • 301
  • 3
  • 4
  • 7
    Who cares about IE6 these days. – Rob W Oct 15 '12 at 14:00
  • -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
  • 8
    Actually, 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
  • 5
    Have 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
8

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...

ItsMe
  • 81
  • 1
  • 1
3

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

background-size: 0 !important;
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.

buTs
  • 41
  • 2
1

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.)

Justin Love
  • 4,397
  • 25
  • 36
1

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
   }
infomasud
  • 2,263
  • 1
  • 18
  • 12
0

Doesn't this work:

.clear-background{
background-image: none;
}

Might have problems on older browsers...

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Ben Hall
  • 1,927
  • 5
  • 25
  • 39
0

Replace the rule you have with the following:

div:not(#a) { // add your bg image here //}
circusdei
  • 1,967
  • 12
  • 28