JavaScript Minimum Font Size


/ Published in: JavaScript
Save to your folder(s)

Run on document load - script checks fonts are not too small and resizes to 10pt if they are.


Copy this code and paste it in your HTML
  1. function MinimumFontSize() {
  2. // Create div with minimal content and move off-screen
  3. var XDivElement = document.createElement('div');
  4. XDivElement.setAttribute('id', 'xDiv');
  5. XDivElement.innerHTML = 'm';
  6. document.body.appendChild(XDivElement);
  7. XDivElement = document.getElementById('xDiv');
  8. XDivElement.style.border = '0';
  9. XDivElement.style.padding = '0';
  10. XDivElement.style.margin = '0';
  11. XDivElement.style.textIndent = '0';
  12. XDivElement.style.letterSpacing = '0';
  13. XDivElement.style.fontSize = '1em';
  14. XDivElement.style.position = 'absolute';
  15. XDivElement.style.marginLeft = '-1000px';
  16. // Element Created. Measure height.
  17. if (XDivElement.offsetHeight < "16") {
  18. tags = new Array ('body', 'div', 'a', 'td', 'th', 'p', 'span', 'h1', 'h2', 'h3');
  19. for (j = 0; j < tags.length; j ++) {
  20. var getbody = document.getElementsByTagName(tags[j]).item(0);
  21. if (getbody) {
  22. getbody.style.fontSize = '10pt';
  23. }
  24. }
  25. }
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.