-3

I am trying to resize a text that is inside a div, like this example.

When i resize the div, also the font-size must be resized.

I try to set the text element with pure css with all the relative length units, but doesn't work. The font-size must be depends only by div size (so vw is not appropriate). The only alternative that I think is set font-size by javascript.

<style> 
div {
  border: 2px solid;
  padding: 20px; 
  width: 300px;
  resize: both;
  overflow: auto;
}
</style>

<div>
  <p style="font-size: 2rem">Hello World</p>
</div>

Which is the best way, preferably in bootstrap 4 or pure css ?

Erestor
  • 1
  • 2

1 Answers1

0

Check out this Question. Onresize for div elements?

You can change this:

var myElement = document.getElementById('my_element'),
    myResizeFn = function(){
        /* do something on resize */
    };
addResizeListener(myElement, myResizeFn);
removeResizeListener(myElement, myResizeFn);

Into

 var myElement = document.getElementById('my_element'),
    myResizeFn = function(){
        document.getElementById(*ID OF OBJECT*).style.fontSize = "20%"; //use % or vh/vw f.e.

    };
addResizeListener(myElement, myResizeFn);
removeResizeListener(myElement, myResizeFn);
kabooya
  • 447
  • 3
  • 14