Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding body background with JQuery?

Tags:

jquery

css

$('body').css('background-color', 'blue'); does not work for me.

There seems to be other matched CSS rules that prevail on the page.

Is there a way I can force this CSS to work? This also doesn't work when I try background-image...

With Google Chrome's CSS explorer there are three different matched CSS rules for body.

I don't understand how this works and why this above JQuery isn't working.

Feed me knowledge please!

[EDIT]

The answer is perfect for overriding CSS! Thanks

Note: Clear cache and browsing data from the beginning of time, it helps ;)

like image 344
KRB Avatar asked Feb 15 '12 16:02

KRB


2 Answers

If you want to force this style to be applied in case there are conflicts then use !important after the style.

$(function(){
    $('body').css('background-color', 'blue !important');
});
like image 147
ShankarSangoli Avatar answered Oct 21 '22 04:10

ShankarSangoli


Using !important will push the priority of this style:

$('body').css('background-color', 'blue !important');

However it might be worth posting up more of your source code, as there is often a better solution than using !important

like image 23
Curtis Avatar answered Oct 21 '22 03:10

Curtis