Skip to content

Instantly share code, notes, and snippets.

@Linrstudio
Created October 20, 2015 06:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Linrstudio/7236158c6c8d8103b9fb to your computer and use it in GitHub Desktop.
Save Linrstudio/7236158c6c8d8103b9fb to your computer and use it in GitHub Desktop.
solutions for window.innerWidth / innerHeight issue in iOS9

iOS9 returns double the value for window.innerWidth & window.innerHeight
The versions that are concerned are: 9.0.0, 9.0.1, 9.0.2

A few people got mad on twitter:

window.innerWidth in iOS 9 Safari returns double the number it did in iOS 8? Is this real life? tell me no — @rachsmithtweets
iOS9 Safari has the most insane bug where window.innerWidth / innerHeight is *sometimes* twice as large as it should be. ughhhh. !? — @mattdesl
iOS9 innerWidth/innerHeight is having a lot of fun these days — @ayamflow

1) Use shrink-to-fit meta-tag

We don't have much infos about this new meta-tag, but it fix the 'scale of 1' issue for content larger than the viewport. See this tweet for more details:

@awfulben @kmuncie @hortont424 shrink-to-fit=no — @smfr

An appropriate example would look like this:

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1, shrink-to-fit=no">
In my case, it did fixed the window.innerHeight/Width issue. (tested on 9.0.2)

2) Use clientWidth/Height

Instead of using window.innerHeight/Width, use document.documentElement.clientHeight/Width
Since a lot of modules/librairies use it, it's not a very good solution.

More infos could be found here, and here.
If you have any suggestions, feel free to comment!

@zcmgyu
Copy link

zcmgyu commented Nov 6, 2021

Safari SUCKS

@titansoftime
Copy link

Why did I have to find this obscure github page to find a solution to ios getting window.innerWidth/Height wrong on window resize when a page is added to home screen (works fine in regular browser)?!

Safari sucks.

@wibawasuyadnya
Copy link

whoever build safari, you guys moron lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment