How to fix vh(viewport unit) css in mobile Safari?

泪湿孤枕 提交于 2019-11-28 08:21:47

I came across this fix today: https://github.com/rodneyrehm/viewport-units-buggyfill It checks every element for a vh/vw unit and turns it into px. Well worth checking out I think.

You can use jQuery in order to fix that.

$('container').height($(window).height()/2);

This works on every browser. Hope it helps.

Praneeth Nidarshan

I also had this issue, here is my solution.

This is equals to height:100vh;

document.getElementById("your-element").style.height = window.innerHeight + 'px';

You can also use below code with jQuery,

$('your-element').height(window.innerHeight + 'px');

Checked with safari :)

I used this CSS only hax today and it worked. iOS 8.2 iPhone Safari :

html, body {
    ...
    width: -webkit-calc(100% - 0px);
    ...
}

Thought: Using calc seems to get Safari to convert units to px itself. Now my page is correctly full-bleed on Chrome and Safari on iOS.

If you need a fullscreen div on mobile browsers, try using wrapper with fixed position and height set to 100%. Then set 100% height to your popup. You can adjust the position of the wrapper and popup in a way you like with top, left, right, bottom properties, as well as height and width.

These eliminates the problem with mobile browsers address bar autohiding in my case where I had to create a fullscreen popup on mobile chrome.

I found this library very useful: https://github.com/Hiswe/vh-check

It will calculate the offset value for the correct vh, and put it in a css variable so you can use it in the css:

.main {
  height: calc(100vh - var(--vh-offset, 0px));
}

My version is "querySelector" because: if I use class of element "getelementbyid" not worked. And this way js is most universal if jQuery libraries are not connected. document.querySelector("your-element").style.height = window.innerHeight + 'px';

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!