public
Last active

Simulate vw with rems

  • Download Gist
rem-calibrate.css
CSS
1 2 3 4 5 6 7 8 9
/* Android stock browser won't let you set font-size smaller than 8px unless you apply this. */
 
:root {
-webkit-text-size-adjust: none;
-moz-text-size-adjust: none;
-ms-text-size-adjust: none;
-o-text-size-adjust: none;
text-size-adjust: none;
}
rem-calibrate.js
JavaScript
1 2 3 4 5 6 7 8 9 10
/*
Place this within a <script> in your <head>, prior to your stylesheet
Then use rems instead of px globally - your site will scale 1:1 with viewport width
*/
 
function remCalibrate() {
document.documentElement.style.fontSize = window.innerWidth/100 + 'px';
}
remCalibrate();
window.addEventListener('resize', remCalibrate, false);

Its great, thanks !

however there is few problems with this technique :
1) it could lead to very small or big fonts when screen size is getting smaller or larger, so we could make some min, max to this function

2) it doesn't take into account the aspect ratio of the screen, on a lot of laptop, the users have a much wider screen than the window height, so this computation tend to deliver them a greater font, that it should.

I had to adapt it for a one-page size all project and come up with a naive implementation, that patch the size according to 4:3 aspect ratio to compensate and using a [8,24] clipping range.

so its something like :

 // clip to a decent range size to avoid, making endless size adjustments as screens being bigger and bigger
    var aspect_ratio =  (window.innerHeight/window.innerWidth)/0.75
    var vwh =  window.innerWidth/100
    var rem = Math.max(8,Math.min(24,vwh*aspect_ratio))

    document.documentElement.style.fontSize =  rem + 'px';

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.