5

Here's the extremely simple HTML page

<body style="overflow-y: hidden">
  ...
</body>

The expected behavior of this page is: scrolling of a document is prevented because of overflow-y: hidden.

It works as expected (scrolling is prevented) everywhere except Safari.

Live demo: https://spotted-chime.glitch.me/


The question is: how to make Safari behave the same way as other browsers?

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • Funny enough, if instead of `overflow-y: hidden`, `overflow: hidden` will be set on body, Safari prevents vertical scrolling :) – Limon Monte Oct 18 '18 at 05:34
  • You can try slapping "!important" behind overflow-y: hidden; –  Oct 20 '18 at 05:32
  • Apple claim they are supporting [`overflow`](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariCSSRef/Articles/StandardCSSProperties.html#//apple_ref/doc/uid/TP30001266-overflow). I would go in one of their shops and request a new device, which works properly. This one doesn't. Look! According to their own specs, it should. And I'd film the whole thing. – tao Oct 20 '18 at 11:52
  • Changing `ontouchend` with `ontouchmove` will probably help. – tao Oct 20 '18 at 12:10

4 Answers4

2

Just use overflow: hidden and it will work.

Alternatively, you can try using position: fixed on the <body> tag as well.

(NOTE: Using this approach, the body will scroll to the top as by default the top: 0.)

EDIT: For safari mobile devices, you need to use Javascript events. Explained in this answer.

https://stackoverflow.com/a/4770179/2860486

Rahul Gandhi
  • 1,035
  • 1
  • 11
  • 24
  • 2
    `overflow: hidden` won't work for mobile devices (iOS), `position: fixed` on the `` isn't acceptable because of the scrolling issue you mentioned. – Limon Monte Oct 21 '18 at 06:53
0

Position fixed is meant to create a div that stays in the same position on the screen while the content behind it is scrolled down.

fixed

The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none (see the CSS Transforms Spec), in which case that ancestor behaves as the containing block. (Note that there are browser inconsistencies with perspective and filter contributing to containing block formation.) Its final position is determined by the values of top, right, bottom, and left.

This value always creates a new stacking context. In printed documents, the element is placed in the same position on every page.

Have you tried changing it to absolute?

body {
  overflow-y: hidden;
}

#backdrop {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, .2);
  border: 5px dashed black;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Hello!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    

  </head>  
  <body>
    <div id="backdrop">
      
    </div>  
    <div>0%</div>
    <div>1%</div>
    <div>2%</div>
    <div>3%</div>
    <div>4%</div>
    <div>5%</div>
    <div>6%</div>
    <div>7%</div>
    <div>8%</div>
    <div>9%</div>
    <div>10%</div>
    <div>11%</div>
    <div>12%</div>
    <div>13%</div>
    <div>14%</div>
    <div>15%</div>
    <div>16%</div>
    <div>17%</div>
    <div>18%</div>
    <div>19%</div>
    <div>20%</div>
    <div>21%</div>
    <div>22%</div>
    <div>23%</div>
    <div>24%</div>
    <div>25%</div>
    <div>26%</div>
    <div>27%</div>
    <div>28%</div>
    <div>29%</div>
    <div>30%</div>
    <div>31%</div>
    <div>32%</div>
    <div>33%</div>
    <div>34%</div>
    <div>35%</div>
    <div>36%</div>
    <div>37%</div>
    <div>38%</div>
    <div>39%</div>
    <div>40%</div>
    <div>41%</div>
    <div>42%</div>
    <div>43%</div>
    <div>44%</div>
    <div>45%</div>
    <div>46%</div>
    <div>47%</div>
    <div>48%</div>
    <div>49%</div>
    <div>50%</div>
    <div>51%</div>
    <div>52%</div>
    <div>53%</div>
    <div>54%</div>
    <div>55%</div>
    <div>56%</div>
    <div>57%</div>
    <div>58%</div>
    <div>59%</div>
    <div>60%</div>
    <div>61%</div>
    <div>62%</div>
    <div>63%</div>
    <div>64%</div>
    <div>65%</div>
    <div>66%</div>
    <div>67%</div>
    <div>68%</div>
    <div>69%</div>
    <div>70%</div>
    <div>71%</div>
    <div>72%</div>
    <div>73%</div>
    <div>74%</div>
    <div>75%</div>
    <div>76%</div>
    <div>77%</div>
    <div>78%</div>
    <div>79%</div>
    <div>80%</div>
    <div>81%</div>
    <div>82%</div>
    <div>83%</div>
    <div>84%</div>
    <div>85%</div>
    <div>86%</div>
    <div>87%</div>
    <div>88%</div>
    <div>89%</div>
    <div>90%</div>
    <div>91%</div>
    <div>92%</div>
    <div>93%</div>
    <div>94%</div>
    <div>95%</div>
    <div>96%</div>
    <div>97%</div>
    <div>98%</div>
    <div>99%</div>
    <div>100%</div>

    <!-- include the Glitch button to show what the webpage is about and
          to make it easier for folks to view source and remix -->
    <div class="glitchButton" style="position:fixed;top:20px;right:20px;"></div>
    <script src="https://button.glitch.me/button.js"></script>
  </body>
</html>
Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
0

Try this:

  <head>
    <style>
       .forSafari::-webkit-scrollbar { width: 0 !important }
    </style>
   </head>
   <body style="overflow-y: hidden" class="forSafari">
         ...
  </body>
Mohan
  • 334
  • 2
  • 12
-1

just use "position: relative" or fixed it will resolve the problem

pabloRN
  • 866
  • 10
  • 19