jQuery Example | Add !important CSS Property to DOM Element

In this post, we will introduce two methods to add css !important property to a dom element.

Method 1: use attr()

For example:

<script>
$(document).ready(function(){
    $('p').attr('style', 'width: 250px !important');
});
</script>

All p elements are 250px.

Method 2: use cssText

We can set the content of cssText to add !important property.

For example:

<script>
$(document).ready(function(){

    // Change width
    $('p').css({
        'cssText': 'width: 250px !important'
    });
});
</script>