JSFiddle - React, Tailwind, and code Playground
HTML
<div id="outer">
<div id="inner" style="font-size:16px">
Lorem ipsum dolor sit amet, dui orci interdum sagittis,
proin metus dui, justo in suspendisse dolor.
</div>
</div>
<button onclick="checkFontSize()">Check font size</button>
CSS
#outer {
width:100px;
height:40px;
overflow-x:hidden;
overflow-y:auto; /* To make it obvious the text is too long. */
border:1px solid red;
}
#inner {
width:100px;
}
JavaScript
window.checkFontSize = function() {
var o = document.getElementById('outer');
var i = document.getElementById('inner');
var fs = parseInt(i.style.fontSize, 10);
while(i.offsetHeight > o.offsetHeight) {
fs--;
i.style.fontSize = fs + 'px';
}
}