Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 100vh not constant in mobile browser

I have a very odd issue... in every browser and mobile version I encountered this behavior:

  • all the browsers have a top menu when you load the page (showing the address bar for example) which slide up when you start scrolling the page.
  • 100vh sometimes is calculated only on the visible part of a viewport, so when the browser bar slide up 100vh increases (in terms of pixels)
  • all layout re-paint and re-adjust since the dimensions have changed
  • a bad jumpy effect for user experience

How can avoid this problem? When I first heard of viewport-height I was excited and I thought I could use it for fixed height blocks instead of using javascript, but now I think the only way to do that is in fact javascript with some resize event...

you can see the problem at: sample site

Can anyone help me with / suggest a CSS solution?


simple test code:

/* maybe i can track the issue whe it occours... */  $(function(){    var resized = -1;    $(window).resize(function(){      $('#currenth').val( $('.vhbox').eq(1).height() );      if (++resized) $('#currenth').css('background:#00c');    })    .resize();  })
*{ margin:0; padding:0; }    /*    this is the box which should keep constant the height...    min-height to allow content to be taller than viewport if too much text  */  .vhbox{    min-height:100vh;    position:relative;  }    .vhbox .t{    display:table;    position:relative;    width:100%;    height:100vh;  }    .vhbox .c{    height:100%;    display:table-cell;    vertical-align:middle;    text-align:center;  }
<div class="vhbox" style="background-color:#c00">    <div class="t"><div class="c">    this div height should be 100% of viewport and keep this height when scrolling page      <br>      <!-- this input highlight if resize event is fired -->      <input type="text" id="currenth">    </div></div>  </div>    <div class="vhbox" style="background-color:#0c0">    <div class="t"><div class="c">    this div height should be 100% of viewport and keep this height when scrolling page    </div></div>  </div>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
like image 739
Nereo Costacurta Avatar asked May 09 '16 09:05

Nereo Costacurta


People also ask

Why does 100vh not work on mobile?

On some mobile browsers, most commonly Chrome and Safari on iOS, 100vh actually refers to outerHeight. This means the lower toolbar on the browser will not be taken into account, cutting off the last couple of rems of your design.

Is 100% same as 100vh?

Which one is a better option? 100% is 100% width/height of its parent width/height. And 100vh is not means 100% unless its parent is 100vh height.

How do I use 100vh CSS?

Sure! Applying min-height: 100vh to the body element should do the trick. Here, 100vh means that the initial body height will take 100% of the viewport height, whereas the use of min-height instead of height will let the body element grow even more if necessary.

Can I use 100vh?

100vh is broken in a subtle but fundamental way on mobile browsers that makes it nearly useless. It's best to avoid 100vh and instead rely on javascript to set heights for a full viewport experience.


2 Answers

Unfortunately this is intentional…

This is a well know issue (at least in safari mobile), which is intentional, as it prevents other problems. Benjamin Poulain replied to a webkit bug:

This is completely intentional. It took quite a bit of work on our part to achieve this effect. :)

The base problem is this: the visible area changes dynamically as you scroll. If we update the CSS viewport height accordingly, we need to update the layout during the scroll. Not only that looks like shit, but doing that at 60 FPS is practically impossible in most pages (60 FPS is the baseline framerate on iOS).

It is hard to show you the “looks like shit” part, but imagine as you scroll, the contents moves and what you want on screen is continuously shifting.

Dynamically updating the height was not working, we had a few choices: drop viewport units on iOS, match the document size like before iOS 8, use the small view size, use the large view size.

From the data we had, using the larger view size was the best compromise. Most website using viewport units were looking great most of the time.

Nicolas Hoizey has researched this quite a bit: https://nicolas-hoizey.com/2015/02/viewport-height-is-taller-than-the-visible-part-of-the-document-in-some-mobile-browsers.html

No fix planned

At this point, there is not much you can do except refrain from using viewport height on mobile devices. Chrome changed to this as well in 2016:

  • https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/BK0oHURgmJ4
  • https://developers.google.com/web/updates/2016/12/url-bar-resizing
like image 145
nils Avatar answered Sep 28 '22 16:09

nils


You can try min-height: -webkit-fill-available; in your css instead of 100vh. It should be solved

like image 24
Saurabh Jain Avatar answered Sep 28 '22 15:09

Saurabh Jain