Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save albert-zhang/78873a00641568ae500029b13730c908 to your computer and use it in GitHub Desktop.
Save albert-zhang/78873a00641568ae500029b13730c908 to your computer and use it in GitHub Desktop.
adjust font size based on screen size
// http://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651551510&idx=1&sn=ad6f87385c39c90c2501ca94b5f5c8dc
var designWidth = 640, rem2px = 100;
var d = window.document.createElement('div');
d.style.width = '1rem';
d.style.display = "none";
var head = window.document.getElementsByTagName('head')[0];
head.appendChild(d);
var defaultFontSize = parseFloat(window.getComputedStyle(d, null).getPropertyValue('width'));
d.remove();
document.documentElement.style.fontSize = window.innerWidth / designWidth * rem2px / defaultFontSize * 100 + '%';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment