JSFiddle - React, Tailwind, and code Playground
HTML
<div class="linecontainer">
<div><span class="lineone">this is the first line of text</span></div>
<div><span class="linetwo">another line of text</span></div>
</div>
<br>
<div class="linecontainer">
<div><span class="lineone">first line of text</span></div>
<div><span class="linetwo">this is another line of text</span></div>
</div>
CSS
.linecontainer { width:250px; overflow:hidden; }
.linecontainer span { white-space:nowrap; }
.lineone { font-size:20px; }
.linetwo { font-size:10px; }
JavaScript
$(".linecontainer").each(function() {
var maxWidth = $(this).width();
var largestWidth = 0;
var largestSpan;
$(this).find("span")
.each(function() {
$(this).data("originalSize", parseInt($(this).css("font-size")));
if ($(this).width() > largestWidth)
{
largestWidth = $(this).width();
largestSpan = $(this);
}
});
var largestSizeFound = false;
var largestSize = 0;
var sizeRatio;
while (largestSizeFound == false && largestSize < 1000)
{
largestSize++;
largestSpan.css("font-size", largestSize + "px");
if (largestSpan.width() > largestSpan.parents(".linecontainer").width())
{
largestSize--;
largestSizeFound = true;
sizeRatio = largestSize / largestSpan.data("originalSize");
}
}
$(this).find("span")
.each(function() {
$(this).css("font-size", parseInt(sizeRatio * $(this).data("originalSize")) + "px")
})
});