It's really hard for me to figure this out. But can anyone give me a sample script where I can just add !important to an already existing inline css?
Asked
Active
Viewed 1,091 times
6 Answers
3
You can add !important to inline using below code,
Suppose you want to add inline
css for div
tag then you can do that using below code.
CSS way
<div style="color:red !important"
jQuery
$('div').attr('style', 'color:red !important;');

Dipesh Parmar
- 27,090
- 8
- 61
- 90
-
I believe this will overwrite any existing inline styles. – Nate Aug 02 '14 at 23:20
2
You can get the inline styles using the attr, and manipulate them like below,
var styleCode = $('p').attr('style').split(";");
$('p').attr('style',styleCode.join(' !important;'));
Check this Fiddle

RevanthKrishnaKumar V.
- 1,855
- 1
- 21
- 34

Ashis Kumar
- 6,494
- 2
- 21
- 36
1
$(element).attr('style', 'height:200px !important;')

Tushar Gupta - curioustushar
- 58,085
- 24
- 103
- 107
1
HTML
<div class="yourelementclass"></div>
JS Code
$('.yourelementclass').attr('style', 'width:500px !important;')
Here is jsfiddle

Love Trivedi
- 3,899
- 3
- 20
- 26
1
A non-library specific way would be to use the += operator to append !important
to the value.
<div style="width:50px;" id="id1"></div>
document.getElementById("id1").style.width += " !important";

James Bruckner
- 909
- 7
- 10
0
$('img')[0].style.setProperty( 'width', '500px', 'important' );

Igor
- 33,276
- 14
- 79
- 112

Max Hartshorn
- 709
- 7
- 19