Multiline text to fit parent container in React JS

前端 未结 4 2203
情歌与酒
情歌与酒 2021-02-19 19:51

I am using a flexbox layout and am trying to get the text in a specific div to be resized to fit that div.

So for example, if I have code as follows:

&l         


        
4条回答
  •  耶瑟儿~
    2021-02-19 20:24

    Here is some code that should do what you are asking.

    let size_div = document.querySelector("#font_size");
    let div = document.querySelector("#fixed");
    
    while (div.scrollHeight > div.clientHeight) {
      let style = window.getComputedStyle(div, null).getPropertyValue('font-size');
      let fontSize = parseFloat(style);
    
      if (fontSize <= 1) {
        break;
      }
    
      div.style.fontSize = "" + (fontSize - 1) + "px";
      size_div.innerHTML = " " + div.style.fontSize;
    }
    

    You can try it out here: https://codepen.io/lowtex/pen/xNawEO

提交回复
热议问题