Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to overwrite css height set by jquery/javascript?

I have this:

 var setHeight = $(this).outerHeight(); 
 // returns e.g. 687

 $("#someElement").css({'height': $setHeight+"px !important" });
 // I want to override this jquery-set height

I'm not sure this is the right way... probably not, since it's not working.

Thanks for helping out!

like image 640
frequent Avatar asked Apr 11 '12 15:04

frequent


People also ask

Can jQuery override CSS?

cssHooks. Hook directly into jQuery to override how particular CSS properties are retrieved or set, normalize CSS property naming, or create custom properties.

How do you overwrite height in CSS?

To override a set height in a CSS element, you can simply put this in the inherited element (either in a style block or inline): height: auto; This will override the set value inherited, without statically setting a new one.

How can get offset height in jQuery?

The offsetHeight property includes the vertical padding and borders in the height calculation, therefore the . outerHeight() method would be the jQuery equivalent. $('. site-header').


1 Answers

Your variable name doesn't have a leading $. Also, the !important flag will cause this not to work in Firefox, however as you're applying this style directly to the element, you shouldn't need it.

$("#someElement").css('height', setHeight + "px");

Also note that if you're only setting the element's height you can also just shorten this to a height() call:

$("#someElement").height(setHeight);
like image 55
Rory McCrossan Avatar answered Sep 28 '22 02:09

Rory McCrossan