Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm unable to inject a style with an "!important" rule [duplicate]

I tried to inject a style using this code:

document.body.style.color='green!important'; 

Per the CSS cascade ref, by applying the !important rule I can trump origin and specificity.

I tried to inject this using Firefox Firebug into www.google.com however no luck.

How do I inject a foreground color with an !important rule?

like image 909
android_baby Avatar asked Oct 27 '11 14:10

android_baby


People also ask

Can I change CSS with js?

JavaScript can change Css styles such as color, font size etc. of elements using some methods such as getElementById(), getElementByClassName() etc. In the following example font style and font size of the elements have changed using getElementById() method.


2 Answers

Per the spec, if you want to set the priority, not just the value, you have to use setProperty, like so:

document.body.style.setProperty ("color", "green", "important"); 
like image 136
Boris Zbarsky Avatar answered Nov 05 '22 05:11

Boris Zbarsky


element.style.cssText = 'color:green !important'; 

should work for you.

like image 20
Jay Avatar answered Nov 05 '22 05:11

Jay