How to detect if element has 'auto' height
Update:
Based on other answers and lot of online research I came up with a mix of everything in a single function. Check out the jsfiddle here:
https://jsfiddle.net/oriadam/d01ap7r6/3/
// input a jQuery element
// return true for elements with auto height (90-100% is considered auto as well)
// return false for elements with fixed height
function is_height_auto($e) {
var e = $e[0],
// check fixed style:
chk = function(value) {
return /\d/.test(value) && !/^(100|9\d)\%/.test(value);
};
// start from the first, easiest, inline styles
if (chk(e.style.height)) {
// console.log('fixed for having style', e.style.height)
return false;
}
// start from the first, easiest, inline styles
var overflow = getComputedStyle(e)['overflow'];
if (overflow == 'scroll' || overflow == 'auto' || (e.tagName == 'BODY' && overflow == 'visible')) {
// console.log('auto for having overflow or is body', getComputedStyle(e)['overflow'], e.tagName);
return true;
}
// deprecated chrome way - check each rule that applies to the element
if (typeof getMatchedCSSRules == 'function') {
var i, MatchedCSSRules = getMatchedCSSRules(e) || [];
for (i = MatchedCSSRules.length; i; i--) {
if (MatchedCSSRules[i - 1].style.height) {
// console.log('found height at MatchedCSSRules[' + (i - 1) + ']: ', MatchedCSSRules[i - 1], ' All matches: ', MatchedCSSRules)
return !chk(MatchedCSSRules[i - 1].style.height);
}
}
}
// append something, see if height was changed, remove the something
var originalHeight = $e.height(),
$ghost = jQuery('<b style="display:block;height:1px;width:1px;padding:0;margin:0;">').appendTo($e),
newHeight = $e.height();
$ghost.remove(); // cleanup
// console.log('Using a ghost got ',newHeight > originalHeight,' originalHeight=' + originalHeight + ' newHeight=' + newHeight)
return newHeight > originalHeight;
} //is_height_auto()
** Ghost element method explained (Previous answer):**
Greg Pettit had a pretty good answer in his blog, here is the main idea:
What’s unique about having auto height? Well, the fact that it allows height to change dynamically, of course!
- Clone the element
- Put it in visibility:hidden and position:absolute
- Remove it's content
- See if height changed (it should be around 0
now). Cleanup
var isAutoHeight = function(element) {
// make a staging area for all our work.
$('body').append('');// assume false by default
var autoHeight = false;
// clone the div and move it; get its height
var clone = element.clone();
clone.appendTo('#stage');
var initialHeight = clone.height();
// destroy all the content and compare height
clone.html('');
var currentHeight = clone.height();
if (currentHeight < initialHeight) {
autoHeight = true;
}
// get that clone and its smelly duplicate ID out of the DOM!
clone.remove();
// do the same for the stage
$('#stage').remove();
return autoHeight;};
Get element height when style = auto or 100%?
You could do it with pure javascript like this :
Javascript
var divHeight;
var obj = document.getElementById('id_element');
if(obj.offsetHeight) {
divHeight=obj.offsetHeight;
} else if(obj.style.pixelHeight) {
divHeight=obj.style.pixelHeight;
}
With jQuery library it's easier :
JQuery
var height = $('#element').height();
Edit
Now with many elements within a container :
Html
<div id="divId">
<img class="node" src="path/to/pic" style="z-index:10; opacity:0;"/>
<img class="node" src="path/to/pic" style="z-index:9; opacity:0;"/>
<img class="node" src="path/to/pic" style="z-index:8; opacity:1;"/>
<img class="node" src="path/to/pic" style="z-index:7; opacity:1;"/>
<img class="node" src="path/to/pic" style="z-index:6; opacity:1;"/>
</div>
I changed your opacity to visibility for compatibility purposes. Don't use display:none; or the height parsing will fail ;).
JQuery
$("#divId img").each(function(index, picture) {
var height = $(picture).height();
//Do everything you want with the height from the image now
});
See fiddle to see it working.
Get actual height of object whose set to auto
In vanilla JavaScript:
element.offsetHeight;
To get correct value, make sure your image is already loaded. For this, either use the load
event of window
, or preload your image via JS. Do not use the DOMContentLoaded
event of document
or ready
in jQuery.
Can you detect when a dom node's style is set to 'auto'?
Yes there is a way, but it's not a funny one. What you have to do is:
- Loop through all
styletags
and linked stylesheets. Then get the
selectorText
for allcssRules
in all style tagsstyletag.sheet.cssRules.selectorText
or for IE < 9
styletag.styleSheet.cssRules.selectorText
Get all of your elements parents
id
,class
andtagName
to find out what possible ways for the tag to get the attribute.- Find all possible combinations that point towards your element in your
list ofcssRules
- check those
cssRules
atcssRules.style.width
if it is auto.
or do it some reverse way and find all cssRules
with style.width == 'auto';
either way its not something easy to get without a lot of code
Related Topics
How to Prevent Material Icon Text from Showing Up When Google's Js Fails to Convert Them
Iframe Dynamic Height Width Change Based on Inner Content
How to Jump to Top of Browser Page
Jquery.Lazy(): Plugin Is Not Loading My 'Li' Contents
How to Check That an Element Is Visible with Puppeteer and Pure JavaScript
How to Find Element Has Specific Color Using Jquery
Scrolltop Jquery, Scrolling to Div with Id
Semantic-Ui Modal Size Keeps Extending to the Height of a Page
How to Pause CSS Transition Mid-Way
Add Custom Buttons on Slick Carousel
Rotating Clickable Elements in IE8+
Detecting Physical Screen Dimensions of Webkit Devices in JavaScript
Is It Possibly to Keep Vertical Rhythm Using Only CSS
How to Save an Image with CSS Filter Applied
Webview Methods on Same Thread Error
Using 'Order' Property to Position Flex Item Between Siblings