4

I have a div which defined as follow:

<div style="background-color: rgb(217, 240, 211) ! important; color: rgb(0, 102, 2) ! important;" class="div_box">... ...
</div>

since the div is currently using the inline style (which I hate this!) I need to override the background color as well as the color.

I have tried:

.div_box[style] {
background-color: rgb(216, 219, 215) ! important; 
color: rgb(94, 94, 94) ! important;
}

but doesn't work. Also tried .div_box { ... } and still not working.

So my question is, how am I override the above div style without manually changing the inline style?

5
  • 2
    !important should be written with no space and sticked to last value :) color: rgb(94, 94, 94)!important;
    – G-Cyrillus
    Jun 4, 2013 at 17:44
  • 1
    you cannot override inline !important styles.
    – bwoebi
    Jun 4, 2013 at 17:48
  • An article to read about specificity in CSS :) Jun 4, 2013 at 18:26
  • specificity doesn't help in this situation.
    – Archonic
    Feb 8, 2016 at 19:00
  • @G-Cyr you saved my life
    – Jim W
    Nov 8, 2017 at 20:45

2 Answers 2

4

http://jsfiddle.net/UkpnZ/3/

Since you can't remove the inline style, as pointed out (that I skipped over), if you can use Jquery, you can use this:

$('.div_box').css('background-color', '');
$('.div_box').css('color', '');

to strip the background-color and color attribute from the inline style where .div box appears. The only added issue here, is that it will strip this everywhere the class is called.

Using this method, you no longer need !important in your stylesheet either.

2
  • So my question is, how can I override the above div style without manually changing the inline style? - Read please
    – Adrift
    Jun 4, 2013 at 17:56
  • I know that I can always use the javascript/jquery to take out/change those styling, it's just I prefer using the css to override. Too bad this can't be done as the inline was marked as !important
    – Josh
    Jun 4, 2013 at 19:09
3

Inline CSS overides CSS stylesheets. Both of them are marked !important, in your case so you can't use this css trick to change the div color.

Short answer, you can't do what you want. It,s possible only if inline CSS would not have been marked as !important.

1
  • 1
    True. A workaround for the example case could be to use an inset box-shadow with a large spread. .div_box { box-shadow: inset 0px 0px 0px 300px rgb(216, 219, 215); } Aug 1, 2014 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.