Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using !important in jQuery's css() function

I have a dialog with an overlay declared like so:

     .ui-widget-overlay  {          position: absolute;          left: 8px;          top: 9px;          height: 985px !important;          width: 518px !important;        } 

The page I have will have two different page heights. To account for this with the overlay I have done this in my JS file:

If small one visible:

$('.ui-widget-overlay').css("height", "985px !important"); 

else

$('.ui-widget-overlay').css("height", "1167px !important"); 

Apparently this does not work. Is there another way to over ride !important that would?

The page can switch back and forth so I need to always have one or the other. Also if I do not add !important to the css then the overlay will expand in height infinitely (its in facebook so i am assuming there is an issue there)

Any suggestions?

like image 294
Soatl Avatar asked Apr 28 '11 19:04

Soatl


People also ask

How to add important in jQuery CSS?

You can do this: $("#elem"). css("cssText", "width: 100px ! important;");

How do you make a CSS element important?

The ! important rule in CSS is used to add more importance to a property/value than normal. In fact, if you use the ! important rule, it will override ALL previous styling rules for that specific property on that element!

What is the use of CSS () method?

Definition and Usage The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

How to add style in jQuery on click?

$(document). ready(function() { $('#div_one'). bind('click', function() { $('#div_two'). addClass('large'); }); });


2 Answers

There is a trick to do this.

$('.ui-widget-overlay').css('cssText', 'height:985px !important;');  $('.ui-widget-overlay').css('cssText', 'height:1167px !important;'); 

cssText is doing the trick here. It is appending css styles as string, not as variable.

like image 91
ghosh'. Avatar answered Oct 13 '22 16:10

ghosh'.


Dont apply styles to a class. Apply a class to your div as a style!

Let jQuery do all the work for you

You style sheet should have these classes in them

.ui-widget-overlay  {          position: absolute;          left: 8px;          top: 9px;          width: 518px !important;           }  .ui-widget-small { height: 985px;  }  .ui-widget-full { height: 1167px; } 

Ok thats your CSS sorted

now your div

 <div id="myWidget" class="ui-widget-overlay ui-widget-small"> YOUR STUFF </div> 

Now you can use jQuery to manipulate your divs either by attaching to a button/click/hover whatever it is you wanna use

$('#myWidget').removeClass('ui-widget-small').addClass('ui-widget-full') 

And you dont need to use !important - that is really used when you start having issues with large CSS files or several loaded styles.

This is instant but you can also add an effect

$('#myWidget').hide('slow', function(){ $('#myWidget').removeClass('ui-widget-small').addClass('ui-widget-full').show('slow') }  ) 

You can add styles dynamically to your page like this- and to replace all existing classes with another class, we can use .attr('class', 'newClass') instead.

$('body').prepend('<style type="text/css"> .myDynamicWidget { height: 450px; } </style>') $('#myWidget').attr('class', 'ui-widget-overlay') $('#myWidget').addClass('myDynamicWidget') 

But you do not want to be over writing your existing styles using this method. This should be used in a 'lost' case scenario. Just demonstrates the power of jQuery

like image 28
Piotr Kula Avatar answered Oct 13 '22 17:10

Piotr Kula