
script to auto adjust font in table
Ok, so I was bored.
| This is a conceptual, UNTESTED code.
And this... This is tested code! :)
<html><head><script><!--
function fit(element, width, default_size, min_size) {
// HTML PX PT PT
// returns true or false indicating whether the
// actual element content was affected or not
var font_size = default_size,
effective_width = element.offsetWidth,
text = element.innerHTML;
while (font_size > min_size) {
element.style.fontSize = [font_size,'pt'].join('');
effective_width = element.offsetWidth;
if (effective_width <= width) return true; // objective achieved!
font_size--;
Quote:
};
while (effective_width > width) {
text = text.slice(0,-2);
element.innerHTML = text;
effective_width = element.offsetWidth;
Quote:
};
return false; // objective achieved!
Quote:
};
window.onload = function() {
fit(el1, 500, 16, 5);
fit(el2, 500, 16, 5);
fit(el3, 500, 16, 5);
window.status = 'Done!';
Quote:
};
//--></script><style><!--
body { background: black; color: white }
span { background: darkblue }
td, table { border: none; border-collapse: collapse; padding: 0px }
body, td { font-family: "Microsoft Sans Serif",arial }
--></style></head><body>
<span style='width:500px'></span><br>
<table>
<tr><td><span id=el1>
a i u e o ka ki ku ke ko sa shi su se so ta chi tsu te to
na ni nu ne no ha hi fu he ho ma mi mu me mo ya yu yo
ra ri ru re ro wa wo n ga gi gu ge go za ji zu ze zo
da ji zu de do ba bi bu be bo pa pi pu pe po
</span></td></tr>
<tr><td><span id=el2>
a i u e o ka ki ku ke ko sa shi su se so ta chi tsu te to
na ni nu ne no ha hi fu he ho ma mi mu me mo ya yu yo
</span></td></tr>
<tr><td><span id=el3>
a i u e o ka ki ku ke ko sa shi su se so ta chi tsu te to
</span></td></tr>
</body></html>