I need to measure the offsetHeight of a div that is inside of a hidden element.
<div id="parent" style="display: none;"> <div id="child">Lorem Ipsum dolor sit amet.</div> </div>
The parent div must be set to "display:none
". I have no control over that. I realize that the offsetHeight of the child div is going to be 0. I need to find a workaround.
Something I've toyed with is when the page loads, I copy the childnodes of parent, inject in a div on the page that is set to "visiblity:hidden
". Then I measure the height of those elements, and remove the nodes when done.
Any other thoughts?
Update: What I wound up having to do was this:
Using YUI 2, on page load, I found all elements of that given classname that were either set to display:none, or whose height and width was 0 (that's one way of measuring whether an element exists, or a parent is set to display:none). I then set that element to display:block. I then checked it's parent for the same thing and showed the parents until it finds a visible parent. Once highest display:none ancestor is set to display:block, I can measure my element.
Once all elements are measured I reset all of the elements back to display:none.
Does display none have height? As mentioned in the first difference, an element with display: none doesn't take any space on the page. Hence, all properties related to the element size, such as clientHeight , clientWidth , height , offsetHeight , offsetWidth , scrollHeight , scrollWidth and width are zero.
Method 1: Using the offsetHeight property: The offsetHeight property of an element is a read-only property and used to return the height of an element in pixels. It includes any borders or padding of the element. This property can be used to find the height of the <div> element.
You can reference the height/width of a div whether its display is "none" or not: var hiddenDiv = document. getElementById('hiddenDiv'); var hiddenDivHeight = hiddenDiv. style.
The style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document.
You need to make element's parent visible for that one very short moment while you're getting element's dimensions. In a generic solution, all ancestors are usually traversed and are made visible. Then their display
values are set back to original ones.
There are performance concerns of course.
We considered this approach in Prototype.js implementation but ended up with getWidth
and getHeight
making only actual element visible, without traversing ancestors.
The problem with alternative solutions - such as taking element out of "hidden" parent - is that certain styles might no longer apply to an element once it's out of its "regular" hierarchy. If you have a structure like this:
<div class="foo" style="display:none;"> <div class="bar">...</div> </div>
and these rules:
.bar { width: 10em; } .foo .bar { width: 15em; }
then taking element out of its parent will actually result in wrong dimensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With