Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to find height of hidden div on page (set to display:none)

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.

like image 697
Jamis Charles Avatar asked Sep 24 '09 19:09

Jamis Charles


People also ask

Does display none have height?

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.

How can I get specific height of a div?

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.

How do you find the width of a display none 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.

How do I unhide hidden elements in HTML?

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.


1 Answers

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.

like image 83
kangax Avatar answered Sep 21 '22 06:09

kangax