4

Assume that the height of an element is set to 'auto'. Trying to get the height in jQuery returns the calculated height. Is there a way to get the actual CSS value (auto) instead of the height in pixels?

$('#myDiv').height() // returns calculated height

See http://jsfiddle.net/7GrwJ/

M.S
  • 1,580
  • 1
  • 13
  • 22
  • What about `$('#myDiv').css('height')` – avrahamcool Oct 16 '13 at 19:42
  • 2
    See [Get all css styles associated with an element](http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element), that should solve it. – Yogu Oct 16 '13 at 19:46
  • 1
    Yogu, I don't see how that helps. It returns a potentially massive object, not one style value. – isherwood Oct 16 '13 at 19:49
  • @isherwood "auto" can be obtained from that object, but it's probably not worth all the trouble. – bfavaretto Oct 16 '13 at 19:50

1 Answers1

5

It is possible to retrieve the raw CSS value, see this fiddle for the result (tested in Firefox and Chromium)

I used this answer to get the raw css object, and this gist to emulate a required function that is only natively available to Webkit.

After that, accessing the property is easy:

css($('#myDiv')).height
Community
  • 1
  • 1
Yogu
  • 9,165
  • 5
  • 37
  • 58
  • 1
    Of course it's possible, but is this a practical solution? +1 just for making me chuckle. – isherwood Oct 16 '13 at 20:04
  • What is "practical"?? – davidkonrad Oct 16 '13 at 20:06
  • 1
    Whether it is practical depends completely on the OP's requirements. I admit that at least in Firefox it is not performant because it parses all CSS files. If you use it multiple times, you should implement some caching. – Yogu Oct 16 '13 at 21:44