Calculate text width with JavaScript

前端 未结 24 1719
陌清茗
陌清茗 2020-11-21 05:08

I\'d like to use JavaScript to calculate the width of a string. Is this possible without having to use a monospace typeface?

If it\'s not built-in, my only idea is t

24条回答
  •  梦谈多话
    2020-11-21 05:33

    Text
    
    
    

    This should work as long as the tag has no other styles applied to it. offsetWidth will include the width of any borders, horizontal padding, vertical scrollbar width, etc.

提交回复
热议问题