40

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?

1

8 Answers 8

32

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();
Sign up to request clarification or add additional context in comments.

2 Comments

A little suggestion. I would declare body as a const. So in my case I did it this way: function resetHeight() { const body = document.getElementById('main-wrap') body.style.height = window.innerHeight + 'px'}, And then I called this function on mounted lifecycle hook. I think it's somewhat about window.onload in vanilla js.
This works great -- but it only registers when I resize my browser or open my dev tools. I am using Chrome....
11

In modern browsers, you can use the dvh unit, which refers to the dynamic viewport. For more information on these new units, see the relevant can I use and web.dev article.

Comments

7

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;

1 Comment

probably because you had a CSS height already, which would override the HTML height. What you did was override that CSS
4

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.

2 Comments

? Please leave a comment after downvoting.
The entire chain from html over body &c. must have height: 100%, while with 100vh you can make any child element that size. Whoever voted you down obviously tried it without the whole chain set to height: 100% and it didn't work.
3

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>

Comments

1

You can try this

max-height: dvh;

or

max-height: -webkit-fill-available;

Comments

1

I've faced the same issue, yet this solution worked for me on both Android and Apple devices

       @include media(d) {
            height: 100vh;
        }

        @include media(m) {
            max-height: fit-content;
        }

Comments

-1

A simple solution worth mentioning...

Continue to set the height of your element to 100vh, then just declare that element's max-height in js.

$('.top-hero-container').css('max-height', (window.innerHeight + "px"));

Now on page load, your element will be no larger than that declared max-height, so will display fine on mobile. It obviously doesn't account for resizing, but the load overhead is less.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.