Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get the correct height property of a div without rendering it to the page?
    primarykey
    data
    text
    <p>I'm implementing a Wordpress theme where content slides into the page vertically. To do this, I need to measure the size of the div containing the content without visibly rendering it first. I'm attempting to do all of this without Ajax.</p> <p>Here's what I've discovered so far:</p> <p>I can easily load the document, read the size, then hide the div with javascript. Unfortunately there's a very obvious (and unacceptable) flicker because javascript doesn't hide the div fast enough. There's also the issue that if images are placed into the content, I have to wait until they're rendered to get the true size... no bueno.</p> <p>The other option is to hide the content with the CSS, but if the user doesn't have javascript enabled, they'll just be staring at a blank page.</p> <p>Which brings me to where I am currently.</p> <p>I have a piece of javascript that runs immediately after the stylesheet is declared that changes the location of the stylesheet link element and then re-renders the page with a javascript specific stylesheet. This solves the problem of having to hide the content before reading the size.</p> <p>This was accomplished by positioning the div containing the content absolutely and off the page 9999pixels.</p> <pre><code>#content { position: absolute; left: -9999px; } </code></pre> <p>At this point, I use jquery to retrieve the height of the content with the following code:</p> <pre><code>$('#content').height(); </code></pre> <p>The problem is, the number that's coming back is the incorrect size and is much smaller than the actual content. When I change the css to:</p> <pre><code>#content { position: absolute; left: 0px; } </code></pre> <p>It renders correctly. What gives?? Is there a bug I don't know about? This happens in both Chrome and Firefox.</p> <p>You can view it for yourself here <a href="http://thethechad.com/wordpress" rel="nofollow">http://thethechad.com/wordpress</a></p> <p>-- UPDATE --------------------------------------------------------------------------</p> <p>I figured out my problem. The div I was using had no specified width. When I moved it outside the flow of the document, it expanded to fill that gap, shifting the content and reducing the height of the element. I went back into my CSS and hardcoded the width and everything is working fine. I feel really dumb. I'm sure we all have those moments. Thanks so much for the help guys!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload