The following code decreases the font size of text in an element where the text is cut off because the width of the element is too small, until it either fits within the width of the element or the font hits the minimum size.
It works perfectly for elements that have a fixed width; BUT it doesn't work with elements that don't have a fixed width because their width can change.
I need to find a way to stop it from shrinking the text of elements without a fixed width automatically, ALTHOUGH it should still shrink them if the text is cut off.
var sizer = document.createElement('div');
sizer.id = 'Sizer';
sizer.style.position = 'absolute';
sizer.style.top = '-9999px';
sizer.style.left = '-9999px';
$(document.body).append(sizer);
sizer = $("#Sizer");
element = $(element);
var min = 10;
sizer.text(value);
var size = parseInt(element.css('font-size'));
sizer.css('font-size', size);
while (sizer.width() > element.width() && size > min) {
size--;
sizer.css('font-size', size);
}
$(element).css('font-size', size);
sizer.remove()