Andrew Hedges
Andrew Hedges

Reputation: 21796

Computed height of an element set to display: none

To calculate a top position for an element of variable height, I was thinking of doing the following:

  1. Move the element 1000px off the top of the viewport
  2. Set the element to display: block
  3. Get the height of the element
  4. Set the element to display: none
  5. Continue on as if everything is normal and good

Any pitfalls in this approach? Is there a more elegant solution?

Upvotes: 1

Views: 513

Answers (2)

Steven A. Lowe
Steven A. Lowe

Reputation: 61223

can you not just set the visibility to hidden and get the offsetHeight?

Upvotes: 0

wombleton
wombleton

Reputation: 8376

I'd look at prototype's implementation of getDimensions.

It sets the position to absolute, visibility to hidden, and display to block briefly. I've written one which handles getting the height of something which is contained within a display:none element, but it's a bit shonky in some edge cases.

Upvotes: 3

Related Questions