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

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

I have a div with some text:

With some text in it

How can

5条回答
  •  感情败类
    2021-01-04 13:58

    Also came across this Jquery script when I was looking for the same thing. It has the added benefit over the others, as far as I quickly tell, is that it also adjusts for height as well as width.

    Comes from here: http://www.metaltoad.com/blog/resizing-text-fit-container

     function adjustHeights(elem) {
          var fontstep = 2;
          if ($(elem).height()>$(elem).parent().height() || $(elem).width()>$(elem).parent().width()) {
            $(elem).css('font-size',(($(elem).css('font-size').substr(0,2)-fontstep)) + 'px').css('line-height',(($(elem).css('font-size').substr(0,2))) + 'px');
            adjustHeights(elem);
          }
        }
    

提交回复
热议问题