What is the best way to get a browser's minimum font size? I whipped up the following code, which does a binary search. It works, but I would like to know if there is a standard or more efficient method.
function getMinimumFontSize() {
var el = document.createElement('div');
document.body.appendChild(el);
el.innerHTML = "<div><p>a b c d e f g h i j k l m n o p q r s t u v w x y z</p></div>";
el.style.fontSize = '1px';
el.style.width = '64px';
var minimumHeight = el.offsetHeight;
var least = 0;
var most = 64;
var middle;
for (var i = 0; i < 32; ++i) {
middle = (least + most)/2;
el.style.fontSize = middle + 'px';
if (e.offsetHeight === minimumHeight) {
least = middle;
} else {
most = middle;
}
}
return middle;
}
Just setting the style and reading it back does not work, because it will return the setting rather than what the browser is actually using.
I need the minimum font size so that I can dynamically re-size content to keep everything on one page, without scrolling. That is hard to do if the browser ignores my font size changes because I have gone below the minimum. So for that situation, I will shrink other content instead, such as graphics.