Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid horizontal scroll on mobile web with responsive web design?

Tags:

I've an adaptable web application. To avoid horizontal scroll I've written the following CSS code:

html {     overflow-x: hidden; } 

This works fine on desktop, but not on mobile. If I display the app on a mobile device, the app adapts fine. The problem is when I do scroll from right to left. The page moves a bit. I want the page to not move horizontally.

Edit:

The page doesn't scroll on all mobile devices. I've tried it on two devices with the same version of Android and it only scrolls on one of the devices.

like image 228
vicenrele Avatar asked Feb 26 '13 10:02

vicenrele


People also ask

How do I stop horizontal scrolling on a responsive website?

If you want to prevent this you have to check your body elements which is larger than the body. It happens when you take a div with some "padding" or "margin" outside ". container" class (twitter bootstrap). So remove all padding and margin outside the container class.

How do I stop my web page from scrolling horizontally on my phone?

The horizontal scrollbar issue on mobile devices can be fixed by setting overflow to hidden. You can do this by using the Elementor Section settings or by adding CSS code.

Why is there horizontal scroll on mobile?

It is designed to allow space for secondary information, that doesn't hog page space. For example when displaying images in a photo gallery, horizontal scrolling lets users see a small sample of the content and gives them the option to quickly 'swipe' or click for more.

Why does my website scroll horizontally?

Horizontal scrollbar appear on your site because div “video banner” has width 100 vw which mean 100% of browser width. Now, since your site has vertical scrolling bar it takes a little bit width and div still trying to be full browser width. For fix it, make “video banner” width = 100%.


2 Answers

Check all the paddings. If you add padding to something with width set to 100% it will go outside the parent.

Shown here http://jsfiddle.net/wzZ3W/

Code

<div id="fillX">     <div id="childXFill">     </div> </div>  #fillX {     background:red;     width:100%;     opacity:0.5; }  #childXFill {     background:blue;     width:100%;     padding:10px;     opacity:0.5; } 

Also check negative left and right margins on elements that span the page. E.g. http://jsfiddle.net/s7zrukj2/2/

Also, use a CSS Reset.


If you wan't to use overflow: hidden you should probably set it on the body element too. Like so

body, html { overflow-x:hidden; } 

Although the fact that something is overflowing indicates an error in your responsive design and you should try and fix it instead to prevent something not being visible to a mobile user.

like image 70
Jonathan Avatar answered Sep 30 '22 11:09

Jonathan


You can put this in your console to outline all block/div. Then you can find the one which is outside of your container.

[].forEach.call($$("*"),function(a){a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)}) 
like image 29
BaptisteGillard Avatar answered Sep 30 '22 11:09

BaptisteGillard