admin管理员组

文章数量:1327750

How do you disable horizontal scrolling on a webpage?

I understand that this question has been asked many times before on stackoverflow (here, for example).

The most mon answer says use CSS to set overflow-x: hidden; or max-width:100% for the html/body elements. However, these seem to hide the scrollbar but still allow the user to scroll with middle clicks, trackpad swiping, and touchscreen swiping. I'm looking for a solution that allows NO horizontal scrolling of any form.

The next most mon answer says don't make your content wider than the screen. Maybe this is a good answer, but in general it's not very helpful and in my particular situation I don't know how to make my content fit.

Are there better methods for preventing horizontal scrolling?

To give you an idea of the problem that's motivating my question, take a look at /. So that you can see the problem better, I have highlighted the offending svg element in gray. When you click the green 'Bet The Bill' button, the svg rotates. If your window is small, the corners of the gray rectangle sometimes end up pointing off the screen and make horizontal scrolling possible.

I've tested this issue on the current versions of Chrome, Android Chrome, Firefox, and IE11. Only IE11 gives the behavior I want, with no horizontal scrolling.

Edit: Thanks to your helpful answers, I now have a solution. I'm going to implement it soon, but unfortunately that means my link above, originally meant to illustrate the problem, will no longer illustrate the problem. Sorry to all future visitors! (Perhaps in hindsight I should have made a fiddle. Although who knows how long that will even last...)

Edit2: Beware, the javascript solution below does not necessarily work on mobile browsers (in my version of Android Chrome there is significant jitter).

Edit3: Aha! My friend told me that overflow: hidden; will indeed work, but it needs to applied to the parent div and not the body or html or another ancestor. This looks like the best solution!

How do you disable horizontal scrolling on a webpage?

I understand that this question has been asked many times before on stackoverflow (here, for example).

The most mon answer says use CSS to set overflow-x: hidden; or max-width:100% for the html/body elements. However, these seem to hide the scrollbar but still allow the user to scroll with middle clicks, trackpad swiping, and touchscreen swiping. I'm looking for a solution that allows NO horizontal scrolling of any form.

The next most mon answer says don't make your content wider than the screen. Maybe this is a good answer, but in general it's not very helpful and in my particular situation I don't know how to make my content fit.

Are there better methods for preventing horizontal scrolling?

To give you an idea of the problem that's motivating my question, take a look at http://www.tedsanders./BetTheBill/. So that you can see the problem better, I have highlighted the offending svg element in gray. When you click the green 'Bet The Bill' button, the svg rotates. If your window is small, the corners of the gray rectangle sometimes end up pointing off the screen and make horizontal scrolling possible.

I've tested this issue on the current versions of Chrome, Android Chrome, Firefox, and IE11. Only IE11 gives the behavior I want, with no horizontal scrolling.

Edit: Thanks to your helpful answers, I now have a solution. I'm going to implement it soon, but unfortunately that means my link above, originally meant to illustrate the problem, will no longer illustrate the problem. Sorry to all future visitors! (Perhaps in hindsight I should have made a fiddle. Although who knows how long that will even last...)

Edit2: Beware, the javascript solution below does not necessarily work on mobile browsers (in my version of Android Chrome there is significant jitter).

Edit3: Aha! My friend told me that overflow: hidden; will indeed work, but it needs to applied to the parent div and not the body or html or another ancestor. This looks like the best solution!

Share Improve this question edited May 23, 2017 at 11:59 CommunityBot 11 silver badge asked Jul 7, 2014 at 4:21 TedTed 491 silver badge7 bronze badges 4
  • have a question here why you using specific height and width for SVG ? – Kheema Pandey Commented Jul 7, 2014 at 4:30
  • Actually, I am not using a specific height and width for the SVG element. I set the SVG width and height attributes to equal document.getElementById('pie-chart').offsetWidth. If you resize the window below about 600 pixels and refresh, you'll see that the SVG changes size. (In the future, I plan to have the SVG automatically resize on window resizes, but I haven't gotten to it yet.) – Ted Commented Jul 7, 2014 at 5:00
  • And what happens when you resize your browser and refresh it again, still you see a scrollbar? I am asking in FF v.30 I cannot see any scrolling.. You problem would be solved as soon as you'll make your SVG responsive. – Kheema Pandey Commented Jul 7, 2014 at 5:05
  • I have overflow-x: hidden set, so I don't see a horizontal scrollbar. However, I can still scroll horizontally by middle click, trackpad, or touchscreen. Could you clarify how I could make my svg responsive? The problem is that the svg rotates, and the corners of its rectangle stick of the screen. I don't want its contents to shrink though. – Ted Commented Jul 7, 2014 at 5:13
Add a ment  | 

3 Answers 3

Reset to default 4

Try this:

html {
    overflow-x: hidden;
    width: 100%;
}

body {
    overflow-x: hidden;
    width: 100%
}

I believe overflow-x: hidden; will only stop the particular element that it is applied to from scrolling, so outer-more elements can still cause the window to scroll. Applying it to html and body should prevent anything which exceeds the width and height of window from causing the window to scroll.

Adding width: 100%; will force the html and body tags to be exactly 100% the width of the window.


But in your example that's not the problem. For some reason the <div class="container"> sometimes displays another set of scrollbars just for the container and the scrollbars appearing and disappearing is what causes the container's movement.

You can fix it by adding to following:

/* overflow: hidden; stops the second set of scrollbars */
/* I increased the width by 300px and added 150px padding on either side. This stopped the grey background from disappearing when the pie chart rotated. */
.container {
    overflow: hidden;
    width: 930px;
    padding-left: 150px;
    padding-right: 150px;
}
    var offset = window.pageXOffset;
    $(window).scroll(function () {
        if(offset != window.pageXOffset)
            window.scrollTo(0, window.pageYOffset);
    });

Also do not forget to hide overflow.

Aha! My friend gave me an answer so I came back here to post it for all of you. overflow: hidden; will indeed work, if it is applied to the parent div and not the body or html or another ancestor. And unlike the javascript solution kindly provided by user3796431, it even works on mobile.

本文标签: javascriptHow do you disable horizontal scrolling on a webpageStack Overflow