Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS 100vh is too tall on mobile due to browser UI

Tags:

css

What is the best way to solve this issue. Obviously all browsers on mobile have got a UI (address bar etc) at the top. This adds additional height to the viewport, so my website which is using 100vh is missing a section.

I'd assume different browsers have different sized viewports due to this, I could simply do something like height: calc(100vh - 50px) or what ever the height is, but it won't match up on all mobile browsers right?

like image 261
Martyn Ball Avatar asked Apr 23 '17 19:04

Martyn Ball


People also ask

Does 100vh work on mobile?

Yes, 100vh is 100% of a viewport height (your device), when pure 100% only fill all available parent area (parent block can have e.g. 50px height and it will fill only to this parent height).

How do I change my height to 100vh?

The state-of-the-art way 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.

Is 100vh same as 100%?

100% is 100% width/height of its parent width/height. And 100vh is not means 100% unless its parent is 100vh height.

What is height 100vh in CSS?

height: 100vh; means the height of this element is equal to 100% of the viewport height. example: height: 50vh; If your screen height is 1000px, your element height will be equal to 500px (50% of 1000px). CALC. height: calc(100% - 100px); will calculate the size of the element by using the value of the element.


Video Answer


4 Answers

Usually the 100vh height will account for the adjusted height, with is why you'll sometimes see mobile pages go funky when the browser's address bar slides down.

For browsers that don't account for the sliding bar within the vh unit: The height for the address bars will not be constant across the browsers, so I'd advise against appending -50px.

Try setting the height of the page (using javascript) with the window.innerheight property.

function resetHeight(){
    // reset the body height to that of the inner browser
    document.body.style.height = window.innerHeight + "px";
}
// reset the height whenever the window's resized
window.addEventListener("resize", resetHeight);
// called to initially set the height.
resetHeight();
like image 175
Tobiq Avatar answered Oct 18 '22 21:10

Tobiq


Use height: 100% which gives you the height after reducing the menu bar's height.

You can test the difference between 100vh and 100% by using document.getElementsByTagName('html')[0].scrollHeight on mobile browser.

For me (Chrome on Andriod), 100vh returns a higher value than 100%, which always giving me a vertical scrollbar, even if I haven't added anything in the html body.

like image 3
Rick Avatar answered Oct 18 '22 21:10

Rick


The accepted answer didn't work for me. I had to make two adjustments:

  • use document.body.style.height instead of document.body.height
  • add 'px' to the end of window.innerHeight

    document.body.style.height = ${window.innerHeight}px;

like image 6
Sheridan Avatar answered Oct 18 '22 20:10

Sheridan


If the element is a direct child of body, you can achieve the desired effect with:

html, body {
    height: 100%;
}

#screenheight {
    height: 100%;
    background-color: blue;
}
<div id="screenheight"></div>
<p>Random content after screenheight element.</p>
like image 2
mori Avatar answered Oct 18 '22 21:10

mori