title: function to calculate display height of html+css
body: <p>(Following this question: <a href="https://stackoverflow.com/questions/11536806/html-columns-whose-width-automatically-changes-according-to-their-content">html columns whose width automatically changes according to their content</a> )</p>
  
  <p>From a PHP script, I print some HTML code, and I would like to know what will be the height of the result (for paging calculation). Suppose I have the following input:</p>
  
  <ul>
  <li>$html code I want to print (string);</li>
  <li>$css code, with full details of the font and size of all items that appear in the HTML (string);</li>
  <li>all relevant browser settings (fonts, sizes, etc.);</li>
  <li>page $width, in pixels (int).</li>
  </ul>
  
  <p>I create a page that looks like this:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;style&gt;$css&lt;/style&gt;
    &lt;/head&gt;
    &lt;body style='width:$width'&gt;
      $html
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I want to know, from within the PHP script, the number of pixels from the top of the page to the bottom of the text.</p>
  
  <p>EDIT: I need to do this on the server side, <em>before</em> the content is displayed, because I use this to calculate what content to put. For example, if the content is too high, I might decide to put only the first half, etc.</p>
  
  <p>Another possible use is to calculate what width is needed to achieve a particular height. For example, I might try to put it in a div with width 200px, but then the result may be too high; then I try width 400px, etc., until I find the width the gives the height that I want.</p>