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

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.

Leave a Comment