How to get the real height of a text?

后端 未结 1 1440
無奈伤痛
無奈伤痛 2020-12-10 03:19

Referring go this example
http://jsfiddle.net/uzgJX/

The result is the height of the box containing the text (the one you can see if you select the text with

相关标签:
1条回答
  • 2020-12-10 04:04

    Get the computed font-size for your text element instead:

    parseInt(window.getComputedStyle(text[0]).fontSize, 10);
    

    font-size represents the size of an em square for a font. It should be noted that, while most glyphs will stay inside the bounds of an em square, some may exceed those bounds. This doesn't usually occur on the vertical dimentions, though.

    Give it a try: http://jsfiddle.net/uzgJX/1/. Tip: screenshot and copy into your favourite image editor, then select the pixels exactly to the height of the text and compare with the value given in the fiddle.

    0 讨论(0)
提交回复
热议问题