24

I'm trying to configure a viewport for mobile Safari. Using the viewport meta tag, I am trying to ensure that there's no zooming, and that you can't scroll the view horizontally. This is the meta tag I'm using:

<meta id="viewport" name="viewport" content ="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />

On my iPhone when I load the page, it seems to look okay:

screenshot

But I can scroll horizontally, so that it looks like this (this is as far to the right as I can go:

screenshot

When I swing it into landscape view, the page renders as expected, locking the horizontal scroll position.

I'm trying to figure out how to make this page not scroll horizontally at all. Is it possible that I've got some page element pushing the content out? I wouldn't even expect that to be possible with a correct viewport set, but I'm grasping at straws here.

alex
  • 479,566
  • 201
  • 878
  • 984
Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75

5 Answers5

21

Is it possible that I've got some page element pushing the content out?

Yes, that is indeed the case. The viewport setting only defines the visible viewport area but does not deal with turning off sideway panning.

So, in order to avoid this from happening, set an overflow:hidden on the element that contains your content, or else, avoid elements from overflowing.

NB: other mobile browsers also support the viewport meta tag since a while, so you'll want to test in those as well.

andreasbovens
  • 1,447
  • 12
  • 11
15

body { overflow-x: hidden; } also works.

HaL
  • 299
  • 4
  • 6
  • This doesn't appear to work on Android devices. I have a page with `overflow-x` set to hidden and can still scroll with my mouse. – Jordan Reiter Jun 19 '12 at 16:45
  • 6
    The question was for Mobile Safari :) For Android browsers, I've had success with [this answer](http://stackoverflow.com/a/10739042/326073). – HaL Jun 20 '12 at 19:05
  • this also breaks vertical scrolling, making scrolling really slow – Acosta Nov 14 '16 at 14:19
  • This works! I had no overflowing blocks or tags. I narrowed it down to just a ` – Chloe Jun 08 '18 at 21:51
9

Late to the party here, but I just had a similar problem where I had horizontal scrolling across an iPhone 5, the site was effectively showing as double the width, with the right hand half completely empty.

In fact, I just needed to change the viewport meta tag from:

<meta name='viewport' content='width=device-width, maximum-scale=1.0, minimum-scale=1.0' />

to:

<meta name='viewport' content='width=device-width, maximum-scale=1.0, minimum-scale=1.0, initial-scale=1.0' />

Adding the 'initial-scale' locked it down so that it only scrolled vertically as expected.

Donovan Charpin
  • 3,567
  • 22
  • 30
Codecraft
  • 8,291
  • 4
  • 28
  • 45
6

This will prevent any elements pushing content out:

body div {overflow: hidden ;}  @ media queries 
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jose Miguel
  • 61
  • 1
  • 1
4

Try this variant:

html, body{
  overflow-x: hidden;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Serj
  • 41
  • 1