Calculating Viewport Height on Chrome Android with CSS Calculating Viewport Height on Chrome Android with CSS google-chrome google-chrome

Calculating Viewport Height on Chrome Android with CSS


Here's what I went with:

HTML

<div id="selector"></div>

CSS

#selector {   height: 100vh;}

JQUERY

function calcVH() {    $('#selector').innerHeight( $(this).innerHeight() );}(function($) {   calcVH();  $(window).on('orientationchange', function() {    calcVH();  });})(jQuery);

PURE JS (NO JQUERY)

function calcVH() {  var vH = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);  document.getElementById("selector").setAttribute("style", "height:" + vH + "px;");}calcVH();window.addEventListener('onorientationchange', calcVH, true);

Simply change #selector to whatever your css selector is. If you use the pure js version you need to use an ID unless you change .getElementByID to .getElementsByClassName.

I'm only aware of this being a problem in Mobile Chrome Android, but I'm guessing it's the same for Chrome iOS as well. You could easily add a mobile detect option if you wanted so this only runs when you need it to. Personally I use Detectizr which works well, but to be honest, since it's pretty lightweight as it is, adding something like this is probably not worth it unless you're already using it.

Hopefully this gets fixed soon so a javascript solution isn't necessary. Also, I tried adding the resize event in case the browser width resizes, but after seeing this question I removed it from my answer. If you want to try using those just change the above to:

JQUERY

function calcVH() {    $('#selector').innerHeight( $(this).innerHeight() );}(function($) {   calcVH();  $(window).on('orientationchange resize', function() {    calcVH();  });})(jQuery);$(window).on('resize orientationchange', function() {

PURE JS (NO JQUERY)

function calcVH() {  var vH = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);  document.getElementById("selector").setAttribute("style", "height:" + vH + "px;");}calcVH();window.addEventListener('onorientationchange', calcVH, true);window.addEventListener('resize', calcVH, true);


I tried every suggestion ... but nothing works for me. Either with Chrome or IOS.

But then...I had an Idea!

1. You can leave the standard entry in the head

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

2. Write an JS in the Foot(be sure jQuery knows about your elements) of the site e.g

<script type="text/javascript" charset="utf-8">  (function() {    function size() {      // you can change here what you prefer      if (/android|webos|iphone|ipad|ipod|blackberry|nokia|opera mini|opera mobi|skyfire|maemo|windows phone|palm|iemobile|symbian|symbianos|fennec/i.test(navigator.userAgent.toLowerCase())) {        var theminheight = Math.min(document.documentElement.clientHeight, window.screen.height, window.innerHeight);        //now apply height ... if needed...add html & body ... i need and i use it        $('html body #yourelementofchoise').css('height', theminheight);      }    }    window.addEventListener('resize orientationchange', function() {      size();    }, false);    size();  }());</script>

3. No more Navigationbar of chrome or whatever will disturb your output or what you want to see

I really hope this will help somebody !

Maybe this is only a template of doing it muchmuchmuch greater.