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?
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?
Apparently it's possible to do this in jQuery:
$("#tabs").css("cssText", "height: 650px !important;");
$('img').css('cssText', "max-width: " +ui.value + 'px !important;' + "max-height: "+ui.value + 'px !important;');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');
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.
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.
!important in the stylesheet that you’re trying to override, you’d need it in $().css().style attribute overrides all CSS which is applied without !important, it does not affect CSS which is applied with !important.