Skip to content

Instantly share code, notes, and snippets.

@johnpmorris
Last active October 13, 2015 01:43
Show Gist options
  • Select an option

  • Save johnpmorris/a08f2f8fa3f5dfd5c2cc to your computer and use it in GitHub Desktop.

Select an option

Save johnpmorris/a08f2f8fa3f5dfd5c2cc to your computer and use it in GitHub Desktop.
some javascript to replicate this css and keep ie compatibility http://codepen.io/Johnm__/pen/bVdNWj

vmin and vmax support for ie

As you probably know if you like using CSS viewport units, the support for them (in ie) suck.

  • vmax Isn't supported in any version of ie or edge.
  • vmin Isn't supported in ie9

##The CSS Viewport Units

  • vw: 1/100th viewport width
  • vh: 1/100th viewport height
  • vmin: 1/100th of the smallest side
  • vmax: 1/100th of the largest side

Note: IE9 uses vm instead of vmin. It does not support vmax.

##The CSS Viewport Units in Javascript

  • var vw = window.innerWidth / 100
  • var vh = window.innerHeight / 100
  • var vmin = Math.min(vw, vh)
  • var vmax = Math.max(vw, vh)

###Setting your html font-size to a viewport unit and using em values everywhere, results in ridiculously responsive web pages

in CSS:

html {
  font-size: calc( 2vmin + 1.4vmax + 2vh );
}

The same thing in javascript.

var vminsize = 2
var vmaxsize = 1.4
var vhsize   = 2

function setSize() {
  var vw   = window.innerWidth / 100
  var vh   = window.innerHeight / 100
  var vmin = Math.min(vw, vh)
  var vmax = Math.max(vw, vh)
  document.documentElement.style.fontSize =  (( vmin * vminsize ) + ( vmax * vmaxsize ) + ( vh * vhsize ))  + "px";
}

window.addEventListener("resize", setSize);
document.addEventListener("DOMContentLoaded", setSize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment