iPhone Safari Viewport Scaling Bug 121
Those who create responsive design for iPhone may be aware of the viewport scaling bug in iPhone Safari. The bug occurs when you set the viewport width to device-width and rotate the phone to landscape view. To see this in action, view the bug demo page with your iPhone and rotate the phone from portrait to landscape view (you should see the page being scaled up). This is a known bug for a long time. Today I'm going to share some tips on how to fix this bug.
Problem (view demo)
View the demo page with an iPhone and rotate the phone from portrait to landscape.

Solution 1: Quick Meta Tag Fix (view demo)
A quick fix to this issue is by setting maximum-scale=1 in the viewport meta tag. This fixes the problem, but the downside is that the page will no longer be zoomable with the zoom gesture.
<meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1">
Solution 2: Javascript (view demo)
For usability concerns, setting maximum-scale is not an ideal solution because user can not zoom the page. Fortunately, there is a Javascript fix which was discovered by @mathias, @cheeaun and @jdalton. Add the following Javascript in the <head> to fix the viewport scaling bug on orientation change. With this Javascript fix, the page remains zoomable.
<script type="text/javascript">
(function(doc) {
var addEvent = 'addEventListener',
type = 'gesturestart',
qsa = 'querySelectorAll',
scales = [1, 1],
meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];
function fix() {
meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
doc.removeEventListener(type, fix, true);
}
if ((meta = meta[meta.length - 1]) && addEvent in doc) {
fix();
scales = [.25, 1.6];
doc[addEvent](type, fix, true);
}
}(document));
</script>
Comments?
Do you know more solutions to fix this viewport scaling bug? The Javascript fix is particularly useful for coding responsive web design. Please share in the comments.
nice tip it well help me thanks
Thanks for the tips, very helpful. I will study it.
thank yo
Thank you very much for a nice share. I will definitely implement it today
this is definitely some major bug. apple should really work on this one.
I don’t like the viewport solution, and the js-fix isn’t any better.
setting hard maximums and minimums on scale may result in unusable websites.
especially if zooming is essential to your website.
I’m astonished this still hasn’t been fixed in iOS5.
To be honest, my view is simply to disallow scaling as opposed to using hacks. If your responsive layout is done correctly, i.e, fonts are set well and nothing is too small, there shouldn’t really be a need for a user to zoom-in. Native apps are fixed – users are used to this, so you could argue why should a considered responsive website/web app be any different?
Of course, there will always be exceptions.
not working for me either…
This works for me, but after I add it to home screen as app (without displaying URL), it stops scaling on to Landscape !!
I absolutely hate when this happens on my site. I need to fix this asap. thanks for a short and sweet post.
If I remember correctly, -webkit-text-size-adjust: none should prevent that behavior
After house of weeding through posts, trying supposed fixes (including this one), creating my own fixes… this was the thing that fixed my issue. Many thanks. I owe you a beer.
http://www.1hunnit.com | #1 Trusted for Webhosting SSL Certificates Reseller and MORE!!
under $2.75 Webhosting (GOOD FOR WORDPRESS!!)
Insightful post here. Very effective tutorial for iphone bug problem solution.
This is really a great post. I like the tutorial very much. This will helfull for beginners :) Thanks for the post
Any idea on how to modify this script to make it work when the initial scale has to be 0.5 instead of 1.0? — Please, any help is always welcome.
Where do i write all this code? Thanks sharing :)
Thanks, thats new to me!
great article, very useful information for beginners
This is very useful for me.Can you share with us something more like this ??? Thanks.
Nice useful article.Thank you for sharing.
Neat solution, thanks!
Boo ya! Finally, a definitely solution to this symptom. I dig it.
I couldn’t get the JS to work, but the meta tag was perfect. Thanks for sharing the info. Great site BTW. I was actually just here a few days ago studying your technique for a fixed column in a responsive layout. Thanks again.