dsy1971 2012-07-26 04:57
浏览 355

函数来计算html + css的显示高度

(Following this question: html columns whose width automatically changes according to their content )

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:

  • $html code I want to print (string);
  • $css code, with full details of the font and size of all items that appear in the HTML (string);
  • all relevant browser settings (fonts, sizes, etc.);
  • page $width, in pixels (int).

I create a page that looks like this:

<html>
  <head>
    <style>$css</style>
  </head>
  <body style='width:$width'>
    $html
  </body>
</html>

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.

EDIT: I need to do this on the server side, before 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.

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.

  • 写回答

3条回答

  • dq23171 2012-07-26 04:59
    关注

    HTML rendering is done at client-side while PHP resides at server.
    As such, PHP cannot determine HTML element height from server-side.

    You would, however, be able to obtain that using JavaScript.
    See: Get full height of a clipped DIV

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!