60

(This is using content scripts in a chrome extension)

I need to overwrite some css properties that the webpage has labeled as !important. Is this possible?

For instance, if I want to get rid of the border that is labeled important:

$(".someclass").css('border','none'); //does not work
j08691
  • 204,283
  • 31
  • 260
  • 272
Wilson
  • 8,570
  • 20
  • 66
  • 101
  • 2
    Did you search SO? I found http://stackoverflow.com/questions/462537/overriding-important-style-using-javascript – Šime Vidas Aug 15 '12 at 01:29
  • @ŠimeVidas: That question is about Javascript; this question is about jQuery. The solution may be the same in the end, but the question's author wouldn't've known that (nor did I), and I think most devs would agree it's best practice to use a jQuery solution, when one exists. – Michael Scheper Apr 21 '15 at 22:30
  • Possible duplicate of [How to apply !important using .css()?](https://stackoverflow.com/questions/2655925/how-to-apply-important-using-css) – Prateek Nov 19 '18 at 15:49

6 Answers6

128

Here you go:

$( '.someclass' ).each(function () {
    this.style.setProperty( 'border', 'none', 'important' );
});

Live demo: http://jsfiddle.net/Gtr54/

The .setProperty method of an element's style object enables you to pass a third argument which represents the priority. So, you're overriding an !important value with your own !important value. As far as I know, it is not possible to set the !important priority with jQuery, so your only option is the built-in .setProperty method.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
51

You can also do this:

$(".someclass").css("cssText", "border: none !important;");
9ete
  • 3,692
  • 1
  • 34
  • 32
14

This should help.

$(".someclass").attr("style","border:none!important");

Updated, so as not to overwrite all styles:

var existingStyles = $(".someclass").attr("style");
$(".someclass").attr("style", existingStyles+"border:none!important");
Pallavi Dwivedi
  • 186
  • 2
  • 12
1

we can just add class using jquery

$("someclass").addClass("test");

<style>
.test{
border:none !important;
}
</style>
Shubham Chopra
  • 1,678
  • 17
  • 30
  • This won't work with a dynamically generated style (such as height or color). It's just ridiculous that [jQuery devs are pitching this as a viable workaround.](https://bugs.jquery.com/ticket/11173) – ADTC Feb 15 '21 at 23:32
0

there is also another way

$("#m_divList tbody").find("tr[data-uid=" + row.uid + "]").find('td').css("cssText", "color: red !important;");

css("cssText", "color: red !important;");

user1548843
  • 670
  • 12
  • 20
-1

Use this below code to override the important in jQuery.

<style>  
  div.woocommerce-variation-add-to-cart-disabled {
    display: none ! important;
  }
</style>

<div class="woocommerce-variation-add-to-cart variations_button woocommerce-variation-add-to-cart-disabled">
  <script>
    jQuery(document).ready(function() {
      jQuery('.woocommerce-variation-add-to-cart').attr('style', 'display: block !important;');
    });
  </script>
</div>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31