开发者

Switching font-family while retaining width

开发者 https://www.devze.com 2023-02-22 23:51 出处:网络
Is it possible to keep the exact width of a <span> while changing the font-family of the text inside ? I am thinking about balancing th开发者_运维百科is width with font-size or font-size-adjust

Is it possible to keep the exact width of a <span> while changing the font-family of the text inside ? I am thinking about balancing th开发者_运维百科is width with font-size or font-size-adjust because letter-spacing will not render gracefully.

I chose deliberately two very different typefaces: Times and Verdana.

Rollover the demo: http://jsbin.com/ahiba4/20


It is possible, but only using JavaScript.

  1. Store the container's width in a variable
  2. Change the font in the container
  3. Set the font size so the container matches the previous width again using one of the methods shown in these questions:

    • How to auto resize text in fixed DIV according to the text's length?
    • JavaScript Scale Text to Fit in Fixed Div (this is the best one IMO)
0

精彩评论

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