I need to position an element in the exact centre of the page (using fixed positioning) and need to calculate the expected size. For example I would add HTML like this:
<div class="window" style="left: X; top: Y">
<h3 class="title">Title</h3>
<img src="file.jpg" width="300" height="200" />
<div class="controls">Some stuff</div>
</div>
The width will be the image width plus some padding. I want to determine what the height of the element will be by getting the styles from the stylesheet, then calculate the correct position, then write the HTML with the right styles.
You can only tell once it’s been added to the document (that is, you can’t tell the expected size and you won’t be able to do it server-side).
So, in Javascript, set the visibility to
hidden, add it to the document, check itsouterHeightor with jQuery call$('.window').height(), reposition as needed, and then change visibility back tovisible.…or, use this plugin:
Usage: