How can I autoscale the font size to fit the contents of a div?

后端 未结 5 1456
广开言路
广开言路 2021-01-04 13:42

I have a div with some text:

With some text in it

How can

5条回答
  •  鱼传尺愫
    2021-01-04 14:08

    Short Answer: You don't.

    You would have to try a size, render it, see if it fits, try another size, render it see if it fits, etc. Then you have to handle the case where the calculated font size is so small no one can read the text.

    There are other options, if the text doesn't fit, add an ellipsis (...) to the end of the text, when you mouse over it, the div could expand, you could use a popup window or tooltip with the full text, or put the full text in a larger area of the screen.

    Find another way.

提交回复
热议问题