Diogo Cardoso
Diogo Cardoso

Reputation: 22267

Prevent horizontal scroll on jQuery Mobile

Is there a way to prevent horizontal page scroll on a mobile device preferably with CSS only?

Here's an example: http://jsfiddle.net/YfLst/15/

Update: The following code solves the issue on iOS but the problem remains on Android:

html, body {
    overflow-x: hidden !important;
    position: relative !important;
}

Upvotes: 3

Views: 12931

Answers (3)

Beat Richartz
Beat Richartz

Reputation: 9622

There are different ways to do that:

You could target mobile devices with a special stylesheet

<link rel="stylesheet" media="handheld, only screen and (max-device-width: 320px)" href="phone.css">

And set the body width to 100% and overflow-x: hidden, or even try to position it absolutely

body {
  width: 100%;
  overflow-x: hidden;
  position: absolute;
  top: 0;
  left: 0;
}

in css.

If by "preventing horizontal scrolling" you mean that your viewport (the area displayed on the mobile screen) is too narrow and should be bigger, you should set the viewport width accordingly in your meta tags

    <meta content="width = 999 (for example)" name="viewport">

Upvotes: 3

aharris88
aharris88

Reputation: 3630

With jQuery mobile the order of the css files matters. What I had to do is make sure that I included my theme css before the jQuery Mobile css instead of after it like this:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="width=device-width, user-scalable=no">

    <link rel="stylesheet" type="text/css" href="css/themes/green-theme.css" />
    <link rel="stylesheet" type="text/css" href="css/jquery.mobile.structure.min.css" />

If you look in jQuery Mobile's css, you'll see code to prevent horizontal scrolling:

body.ui-mobile-viewport, div.ui-mobile-viewport {
    overflow-x: hidden;
}

You don't even have to add any classes to your body tag. jQuery mobile does it automatically.

Upvotes: 0

Max Hudson
Max Hudson

Reputation: 10226

Not with CSS, but you can make your document no wider than the screen and you won't have the issue.

Otherwise you will have to use a javascript/jquery solution like so: http://forum.jquery.com/topic/scrollview-make-page-not-scrollable

Upvotes: 0

Related Questions