DEV Community

Cover image for Don't use 100vh for mobile responsive
⚑ Nirazan Basnet ⚑
⚑ Nirazan Basnet ⚑

Posted on

Don't use 100vh for mobile responsive

Generally, we use height:100vh for fullscreen layout which is easy hack and convenient way to get better design.

Example



.content {
   height: 100vh;
}


Enter fullscreen mode Exit fullscreen mode

But when we test our design on actual device, we encounter several issues:

  • Mostly Chrome and Firefox browsers on mobile have got a UI (address bar etc) at the top.
  • On Safari it get's more tricky address bar is at the bottom.
  • Different browsers have different sized viewports
  • Mobile devices calc browser viewport as (top bar + document + bottom bar) = 100vh
  • Whole document is filled to the page using 100vh

Problems

  • On Chrome

Chrome 100vh issue

Scrollbar issues has been detected. Bad user flow and difficult to navigate the content.

Note: I have also tested this issues on safari which makes more bad user flow


Solutions

Detect the height of the app by using JS

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



const documentHeight = () => {
 const doc = document.documentElement
 doc.style.setProperty('--doc-height', `${window.innerHeight}px`)
}
window.addEventListener(β€˜resize’, documentHeight)
documentHeight()


Enter fullscreen mode Exit fullscreen mode

Using, CSS Variable



:root {
 --doc-height: 100%;
}

html,
body {
 padding: 0;
 margin: 0;
 height: 100vh; /* fallback for Js load */
 height: var(--doc-height);
}


Enter fullscreen mode Exit fullscreen mode

Here, documentHeight function sets new style property var('--doc-height') and includes current window height.


Final Results

Chrome Browser

CSS 100vh viewport

Note: There is no any vertical extra scrollbar is appearing now also no issues on Safari too. The bottom address bar of safari is always on the bottom which makes good user flow to the website


Conclusion
πŸ‘πŸ‘ By coming this far I hope you can solve the mobile devices viewport issues. So, I suggest you give it a try on your project and enjoy it!

Feel free to share your thoughts and opinions and leave me a comment if you have any problems or questions.

Till then,
Keep on Hacking, Cheers

Latest comments (45)

Collapse
 
greggcbs profile image
GreggHume

For anyone using react, i wouldnt waste time with any css solutions. Use this react hook and be done with it:
usehooks-ts.com/react-hook/use-win...

Collapse
 
piarey profile image
piarey

!!!!! This solution still has serious problems. !!!!!

1.
(iOS 15.2)
(when the content length is less than the viewport height)
"When the swipe down refresh function is exposed" ===>>> This action triggers the "Resize" event.

In other words, if you keep trying to swipe down, the continuously calculated height value will decrease, eventually creating a blank space at the bottom.

2.
What if the length of the content is between height: 100% and 100vh???
(iOS15.0.2) 100vh - A blank space is created at the bottom as much as the length of the content.

==========

Even so, are you still going to treat this situation as normal?
iOS is the re-advent of IE.

Collapse
 
balchen profile image
balchen

I'm experiencing the exact same problem, and came across this post (among many others on the same subject). While it does fix the issue for Android and iOS devices, it breaks the page for desktop (Chrome and Firefox, at least).

The scrollbar I don't want now appears on desktop.

If I subtract 1px from innerHeight in the code, there's no scroll bar on desktop. It's a hack. Don't know why 1px makes the difference in this case.

Collapse
 
lico profile image
SeongKuk Han

Thank you! It worked for me!

Collapse
 
nirazanbasnet profile image
⚑ Nirazan Basnet ⚑

Glad this helped you

Collapse
 
kelseyjj profile image
Kelsey Jones

Glad I came across this.

Collapse
 
nirazanbasnet profile image
⚑ Nirazan Basnet ⚑

Thanks πŸ‘

Collapse
 
ambriel profile image
Ambriel Goshi

This is helpful.

Collapse
 
nirazanbasnet profile image
⚑ Nirazan Basnet ⚑

Thanks πŸ‘

Collapse
 
rasmrq profile image
Pace Dev

So thanks uuu

Collapse
 
ibenchellali profile image
IDRESS BENCHELLALI

Thank you for this great article!

Collapse
 
nirazanbasnet profile image
⚑ Nirazan Basnet ⚑

Your welcome :)

Collapse
 
nosovandrew profile image
Andrew Nosov

Cool solution, it helped me, thank you! In addition I've improved this approach with some debounce mechanism (setTimeout) that will limit count of function executions.

let timeoutId = null;
const documentHeight = () => {
  clearTimeout(timeoutId); // avoid execution of previous timeouts
  timeoutId = setTimeout(() => {
   const doc = document.documentElement;
   doc.style.setProperty('--doc-height', `${window.innerHeight}px`)
  }, 200);
};
window.addEventListener(β€˜resize’, documentHeight);
documentHeight();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
greggcbs profile image
GreggHume

Sometimes debouncing cost more then running the code itself. Just as an FYI.

Collapse
 
nirazanbasnet profile image
⚑ Nirazan Basnet ⚑

Tankyou !! Appreciated :)

Some comments have been hidden by the post's author - find out more