0

I'm trying to find a way to get an element's CSS height property, or actually, just tell if a height property is set.

The problem is, when I use

$(elem).css('height');

I get the display height of the element, but I'm trying to see if the element has a height property that was set in either a class, id, or directly on the div.

Any suggestions?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
pedalpete
  • 21,076
  • 45
  • 128
  • 239

5 Answers5

2

You can use height also.

$(elem).height(); // to get the height.

Also see this Q/A

if you want to get correct CSS value, i can advise don't use jQuery

if we have HTML:

<div id="elem" style="height: auto"></div>

we can write JS:

$('#elem').get(0).style.height    // "auto"

if we have HTML:

<div id="elem"></div>

JS:

$('#elem').get(0).style.height    // ""

universal function:

var height = function(elem){
      return $(elem).get(0).style.height === "" ? $(elem).height() : $(elem).get(0).style.height;
}
Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 5
    Note: `height()` returns the *computed height* which might not match the CSS property; it also doesn't seem to answer the buried question: "I'm trying to see if the element has a height [CSS] property that was set in either a class [selector], id [selector], or directly .." (I'm not entirely sure what that means, but it's in the post.) – user2864740 Jan 14 '14 at 04:45
  • Thanks, you're correct, what I mean by 'set in a class or directly is that the class can be in an external css file, directly is (as this answer shows) directly on the html element. – pedalpete Jan 14 '14 at 10:14
0

Likely not the best way but I would just look at the outerHTML, see if a height value is set. If it isn't then it's in the CSS (or nothing is set).

s = $(elem)[0].outerHTML;
if (s.indexOf("height:") > 0) {
    // inline style
} else {
    // somewhere else
}
dug
  • 340
  • 3
  • 7
  • Unless no value is set in the css. That's what I'm trying to find. Does this element have a css height set, or is it just using the heights of it's children? – pedalpete Jan 14 '14 at 10:15
0
    $( "div" ).click(function() {

you can check if height is defined for this div parent element or children using *this* reference. For now i am just fetching the height of the div which has been clicked.
    var height= $( this ).css( "height" );
    //If height is truthy  
    if(height){
    //your code here
    }
    });

Hope this answers your query. For more details..

gokul
  • 383
  • 1
  • 8
  • that returns the computed height, not the height that is defined in the css. I'm going to update my question to make it clearer. – pedalpete Jan 14 '14 at 10:18
  • check out the fiddle http://jsfiddle.net/gokuldahal/mYT7s/ try removing the height from the class, it will return computed value, however if you specify height in the class, this specified value will be retrieved. – gokul Jan 14 '14 at 10:27
  • Here's where that doesn't work. If there is no height applied to the div, I still get a height value. http://jsfiddle.net/mYT7s/4/ – pedalpete Jan 14 '14 at 10:58
0

The problem is what do you mean by 'is set'?

In vanilla javascript you can do:

element.style.height

This will return an empty string if no height has been set INLINE.

However, if a height has been applied via a stylesheet, it will still return an empty string.

The problem is, if you return a computed height by either .height() in jQuery or window.getComputedStyle(element).height in Javascript, then there is no way of telling if it was calculated by applying a style sheet (what you would call 'having a height property set'), or was generated by extending the height of the element to fit its contents (which you'd call 'not having a height property set').

---------------------Update----------------------

To make it clearer, I'm trying to see if the height of a div is a computed height, or if it a height that was defined in css.

A div can contain the height of it's children, or it can have a height set specifically on the div. The heights for either the children or the div in question can be set in a CSS file, style tag on the page, or on the div itself.

I'm trying to see if the div has a css set height property.

  • by pedalpete

---------------------Update----------------------

I understood your question, but perhaps my answer was a bit opaque.

There is nothing you can call that will tell you if an element has a style property applied to it by a style sheet. In other words, you can't do what you want to do.

The only thing you can find out (via element.style) is if there is an inline style declared.

getComputedStyle will tell you how high an element currently is, but it won't tell you how it got that way.

  • by Graham Nicol
Graham Nicol
  • 264
  • 2
  • 14
0

Looks like there isn't an easy way of doing this, my method to resolve this is to hide the children of the div, and and check if the height of the div has changed. If it has, the height was not set, if it hasn't the height was set.

This fails when the div has nothing but text, but as this is a layout tag, it will likely always have sub tags.

var elHeight = $(elem).height();
$(elem).children().hide();
var checkHeight = elHeight===$(elem).height();
$(elem).children().show();
console.log(checkHeight);
if(checkHeight===false) return setSize($(elem).parent().height()/2);
pedalpete
  • 21,076
  • 45
  • 128
  • 239