dynamically changing the size of font size based on text length using css and html

前端 未结 8 1236

I need to display user entered text into a fixed size div. What i want is for the font size to be automatically adjusted so that the text fills the box as much as possible.<

8条回答
  •  春和景丽
    2020-12-08 04:57

    Say you have this:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis dui felis, vel vehicula tortor cursus nec

    Then you can do something like:

    $(document).ready(function () {
        resize_to_fit();
    });
    
    function resize_to_fit(){
        var fontsize = $('div#outer div').css('font-size');
        $('div#outer div').css('fontSize', parseFloat(fontsize) - 1);
    
        if($('div#outer div').height() >= $('div#outer').height()){
            resize_to_fit();
        }
    }
    

    Working Sample

    $(document).ready(function() {
      resize_to_fit();
    });
    
    function resize_to_fit() {
      var fontsize = $('div#outer div').css('font-size');
      $('div#outer div').css('fontSize', parseFloat(fontsize) - 1);
    
      if ($('div#outer div').height() >= $('div#outer').height()) {
        resize_to_fit();
      }
    }
    
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis dui felis, vel vehicula tortor cursus nec

提交回复
热议问题