开发者

Is there a way to know the height of a paragraph?

开发者 https://www.devze.com 2023-02-21 00:44 出处:网络
I would like to kn开发者_运维问答ow the height of a paragraph. Is there any solution? Thanks.Not in PHP, no. The paragraph will be rendered in the browser; a number of variables can influence the actu

I would like to kn开发者_运维问答ow the height of a paragraph. Is there any solution? Thanks.


Not in PHP, no. The paragraph will be rendered in the browser; a number of variables can influence the actual height: Operating System font size settings, Zoom settings, availability of fonts.... therefore, the height can be determined only in client-side JavaScript after the page has been rendered.

The jQuery framework has the convenient .height() that will give you the computed height of a HTML element and bridges a number of cross-browser issues one would have to take care of manually:

<p id="paragraph>Lorem Ipsum Dolor amet....</p>

$(document).ready(function() {
 alert("Height is "+ $("#paragraph").height() + "Pixels");    
} );


You'll want to use offsetHeight or clientHeight in JavaScript.

Example http://jsfiddle.net/nFRra/

More info http://programming.top54u.com/post/Javascript-Get-Height-of-Div-onclick.aspx


For a browser, Pekka's answer is correct, but it should be noted that it is possible to calculate the width/height of a given (and known) font (for inclusion in PDF's and images). That information can be readily found in the GD library (there are others as well, but that one is the one I've used)

0

精彩评论

暂无评论...
验证码 换一张
取 消