Get string length in pixels with JavaScript

后端 未结 2 1403
离开以前
离开以前 2021-01-13 17:45

Let\'s say I have the string \"Hello\". This string is obviously five characters in length, but what is its length in pixels? Is there an easy way to determine this length i

2条回答
  •  深忆病人
    2021-01-13 18:08

    You can determine the number of pixels the container of the string. You can do this by creating a hidden element in the DOM, setting the inner HTML to the string and then asking for the width.

提交回复
热议问题