14

I need some help with hiding my horizontal scrollbar and still able to scroll. I have used webkit but does not work in IE and firefox. I have seen a lot of help with vertical scrollbar, but does not work with horizontal. Any help?

Update: I have created a JSFiddle to show my problem. I want to hide the horizontal scrollbar and still able to scroll without using

 ::-webkit-scrollbar { 
      display: none; 
   }

http://jsfiddle.net/o1xoh9w8/1/

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Acer Buxton
  • 153
  • 1
  • 1
  • 5
  • Have you googled this? What have you already tried? What code do you have? can you show a concise example of the issue? Please update yorur question with these details. Thanks. – Martin Apr 04 '16 at 14:39
  • Yes I have and seen alot of ways for vertical scrollbar. I have applied same technique for horizontal scrollbar, but not getting desired result. http://stackoverflow.com/questions/16670931/hide-scroll-bar-but-still-being-able-to-scroll for vertical – Acer Buxton Apr 04 '16 at 14:41
  • TBH if you hide a horizontal scrollbar I think many users will not realise that you can scroll horizontally. – Martin Apr 04 '16 at 14:41
  • Have you tried overflow-x: auto; ? – Hawkeye Apr 04 '16 at 14:41
  • CSS uses `overflow-x` and `overflow-y` but you see I don't know if you're aware of this. – Martin Apr 04 '16 at 14:42
  • 1
    overflow-x and overflow-y hides the scrollbar and I cannot scroll either WHILE i want to hide scrollbar and also be able to scroll – Acer Buxton Apr 04 '16 at 14:44
  • can you please provide a JsFiddle of the code you have tried and we can help you debug it. – Bagzli Apr 04 '16 at 14:46
  • 1
    I would recommend the [Prefect Scrollbar](https://noraesae.github.io/perfect-scrollbar/) js plugin. I have used it woth great success. It won't show any scrollbar until a user hovers over the thing that needs scrolled, and then it shows a small unobtrusive scrollbar which is easy to customize or hide entirely – zgood Apr 04 '16 at 14:48
  • I am trying to create an horizontal tab, perfect scrollbar showing the scrollbar will not look ideal. @zgood – Acer Buxton Apr 04 '16 at 15:02
  • @AcerBuxton Perfect Scrollbar has vertical scrollbars also. And you can hide the actually scrollbars if you wish with a simple css display none and the scrolling will still work. Although this may be considered bad UI because a scrollbar is how most users know something is scrollable – zgood Apr 04 '16 at 15:41

3 Answers3

4

Here is how you do it, I have tested this in Chrome, IE, Firefox, Opera, Safari(Windows) and Edge

<h1>You can scroll with mouse wheel</h1>

<div id="box">
    <div id="content">
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
    </div>
</div>

h1{font-weight:bold;font-size:2em;} /* ignore only for header */
/* *********************** */
div#box{
    height:200px;
    width:300px;     
    overflow:hidden;
    border:1px solid black;
    padding: 10px;
}

div#content{
    height:200px;
    width:326px;
    /*
     * Uncomment to see scrollbar
    width:300px;
    */
    overflow:auto;
}

Here is a JSFiddle: http://jsfiddle.net/JoshMesser/VUSuZ/

Credits go to creator of the JsFiddle

EDIT:

For vertical it is just a matter of changing the height. What you are doing is you are just pushing the scroll bar outside of what user can see, so to them its not there, while in reality it is there hidden behind elements. Here is a JS Fiddle based on my last one. You will see I just forced p to be in single line to get horizontal scrolling and then increased the height to hide the scroll-able bar.

http://jsfiddle.net/VUSuZ/575/

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • 3
    This works for vertical scrolling, I am looking for something that works on horizontal scrolling. I have updated my code with JSFiddle. Have a look and any help will be fine – Acer Buxton Apr 04 '16 at 14:56
  • I just took a look at the updated fiddle and the horizontal scroll doesnt seem to be working. maybe things have changed. but is there a way to do it at the moment?? – faraz Apr 05 '19 at 05:55
  • @faraz There are 2 fiddles in my answer. The first fiddle only scrolls Vertically, I have just tested it in Chrome and it works fine, use your mouse scroll to go up and down. The second fiddle only scrolls horizontally, you can hold shift and then use mouse scroll to go left and right. I have also tested this in chrome and it works fine for me. – Bagzli Apr 05 '19 at 12:48
4

I think you do not want to use

::-webkit-scrollbar { 
  display: none; 
}

Because it will hide all the scroll bars. A better way to hide the scroll bar but still enable scrolling in a particular container will be to follow the following example:

HTML

<div class="container">
  <table>
    <tbody>
      <tr>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
      </tr>
    </tbody>
  </table>
</div>

CSS

.container {
    overflow-x: auto;
    white-space: nowrap;
}

.container::-webkit-scrollbar {
    display: none;
}
Hussain Pettiwala
  • 1,406
  • 8
  • 16
3

I used a fixed height approach.

Note: this approach can help only in specific cases.

#container1{
    height: 50px;

    /* Just for presentation. Can be removed */
    border: 1px solid red;

    /* Hides content outside this container */
    overflow: hidden;
}

#container2{
    /* Height is significantly greater than the height of container#1 to hide
       any possible scroll */
    height: 100px;
    overflow: auto;
    white-space: nowrap;
}
<div id="container1">
  <div id="container2">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit, ante laoreet mattis blandit, arcu nisi blandit felis, et molestie justo lacus et sem. Nunc tempor tellus sit amet eleifend tristique. Integer eget condimentum lectus, nec viverra risus. Nullam leo lectus, placerat vitae porta eget, auctor et nisi. Suspendisse feugiat in lacus accumsan tincidunt. Fusce pulvinar accumsan sem sit amet finibus. Curabitur volutpat mi vitae eros mattis congue. In ut sem eu tellus egestas lobortis vitae eu felis. Maecenas sodales, nisl eu bibendum vulputate, neque leo finibus odio, sit amet bibendum libero dolor sed diam. In molestie magna vitae dui vulputate, eu consequat dui ullamcorper. In hac habitasse platea dictumst. Vestibulum pulvinar, mi quis mollis pulvinar, metus justo aliquet arcu, vel venenatis ipsum dolor at sapien. Sed ac odio bibendum, feugiat nibh at, viverra mi. Morbi sem nisi, ultricies non nulla pretium, gravida malesuada neque.
  </div>
</div>

(My case is horizontal scrollable buttons container for mobile screens - the buttons are stuck to the above block (+ margin) and have fixed height)

enter image description here

Community
  • 1
  • 1
Nick
  • 144
  • 1
  • 4