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.
I know this is a really late comment but I do believe there is a third way. I went through the comments really quick so I might have missed this but you can fix this in CSS.
There is a WebKit specific property called ‘-webkit-text-size-adjust’ that can be set to ‘none’ to prevent scaling in landscape. It also prevents scaling of text when you pinch to zoom but I’m guessing that you’d have the meta user scalable attribute set to false anyway.
Nice weblog right here! Also your site a lot up very fast! What web host are you using? Can I am getting your affiliate link for your host? I desire my site loaded up as quickly as yours lol
Same here! What host are you using? Any CDN being used?
Really its is a great and helpful tutorial… keep going… never put a stop to your experiments…
hai thanks for the code
Thank you for sharing thats exactly what I was looking for.
Really impressive.
yeah i see this bug as well !
I also had that problem only in Safari Mobile on the iPhone5 (I didn’t check on different iPhones). Website worked well on android Samsung Galaxy S2 (FireFox mobile, Chrome mobile) in both landscape, portrait. My viewport was . It worked good on my Galaxy s2, but on the iPhone after flipping the phone to landscape view and then back to portrait the website was zoomed in.
I fixed this with the viewport. This time I used and it worked well on iPhone and android Samsung Galaxy s2.
old viewport was
meta name=”viewport” content=”width=device-width, initial-scale=1.0, maximum-scale=1.0″.
I fixed this with the viewport meta name=”viewport” content=”width=device-width”
comments disable brackets
Thanks for this informative post and the code as well,i usually see this bug.
I have a page where user-scalable is set to “no” ( zoom disabled ) .
A div and an iframe under the div is inserted dynamically and also height of the iframe is set dynamically based on the content. Here I notice that although the page zoom remains unchanged the text inside the iframe is zoomed-in in landscape.
Wow!,,,yes, its amazing work,,,Thank you so much!,,,,,
You can learned a lot from this website and with this topic, the codes really work and you did a great job. Thanks for sharing you knowledge in coding. Cool
Thanks! This solved it. I thought it was a glitch in my media queries, but no. And I appreciate that scaling remains available. Good script.
Hi, I know this post is kinda old, but I bumped into it, and I thought I’d share what I use, with no bugs that I can see…
a) set a min with do the body tag
body {
min-width: 1000px;
}
b) set the same with to the viewport meta tag
Users should now be able to:
- see the design as you have created it
- zoom the content
- change the device landscape to portrait and vice versa with no issues
the meta code (sorry):
meta name=”viewport” content=”width=1000px”
Thanks for sharing such wonderful article with us,its really very helpful for everyone. Keep on posting.