78

I am trying to add !important in the css attribute using jQuery like

$("tabs").css('height','650px;!important');

but !important has no effect. How to include !important in jquery?

3
  • 1
    Can you try without the semicolon between the value and !important? Commented Dec 31, 2009 at 17:04
  • 2
    Marc W's answer is the answer. You don't need !important. Commented Dec 31, 2009 at 17:12
  • Though Marc W's Answer is technically right answer, klokop's answer works Commented Jan 1, 2010 at 1:49

8 Answers 8

166

Apparently it's possible to do this in jQuery:

$("#tabs").css("cssText", "height: 650px !important;");

Src: http://bugs.jquery.com/ticket/2066

Sign up to request clarification or add additional context in comments.

6 Comments

Although, you can do $(elem).css('cssText', $(elem).css('cssText') + "your new css") right? ;)
What is cssText here?
@Diffy, cssText is the 'cssText' property of the 'style' property. (ie, element.style.cssText)
I had to set both height and width using this method, you have to write it as a one liner. Example $('img').css('cssText', "max-width: " +ui.value + 'px !important;' + "max-height: "+ui.value + 'px !important;');
@Juan Although it will bring you also non inline styles. You will end up with a newspaper of CSS.
|
7

I solved this problem with:

inputObj.css('cssText', inputObj.attr('style')+'padding-left: ' + (width + 5) + 'px !IMPORTANT;');

So no inline-Style is lost, an the last overrides the first

Comments

6

It is also possible to add more important properties:

inputObj.attr('style', 'color:black !important; background-color:#428bca !important;');

Comments

6
var tabsHeight = 650;

$("tabs").attr('style', 'height: '+ tabsHeight +'px !important');

OR

#CSS
.myclass{height:650px !important;}

then

$("tabs").addClass("myclass");

Comments

2

If you need to have jquery use !important for more than one item, this is how you would do it.

e.g. set an img tags max-width and max-height to 500px each

$('img').css('cssText', "max-width: 500px !important;' + "max-height: 500px !important;');

Comments

2

For those times when you need to use jquery to set !important properties, here is a plugin I build that will allow you to do so.

$.fn.important = function(key, value) {
    var q = Object.assign({}, this.style)
    q[key] = `${value} !important`;
    $(this).css("cssText", Object.entries(q).filter(x => x[1]).map(([k, v]) => (`${k}: ${v}`)).join(';'));
};

$('div').important('color', 'red');

Comments

0

If you really need to override css that has !important rules in it, for instance, in a case I ran into recently, overriding a wordpress theme required !important scss rules to break the theme, but since I was transpiling my code with webpack and (I assume this is why --)my css came along in the chain after the transpiled javascript, you can add a separate class rule in your stylesheet that overrides the first !important rule in the cascade, and toggle the heavier-weighted class rather than adjusting css dynamically. Just a thought.

Comments

-3

You don't need !important when modifying CSS with jQuery since it modifies the style attribute on the elements in the DOM directly. !important is only needed in stylesheets to disallow a particular style rule from being overridden at a lower level. Modifying style directly is the lowest level you can go, so !important has no meaning.

4 Comments

Unless you want to override an existing !important rule…
Yeah: if there’s an !important in the stylesheet that you’re trying to override, you’d need it in $().css().
As the other comments have implied, this is just wrong. While the style attribute overrides all CSS which is applied without !important, it does not affect CSS which is applied with !important.
Not only is this incorrect, but OP asked HOW not if he needed

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.