1

I see lots of answers for hiding the scrollbar by using

overflow-y: scroll;

and pushing the scrollbar away. But how would you achieve this for horizontal scrolling?

Jordan
  • 227
  • 5
  • 20
  • Have you tried using 'overflow-x: scroll' and pushing it away? – JiFus Jul 29 '14 at 12:05
  • http://stackoverflow.com/questions/16670931/hide-scroll-bar-but-still-being-able-to-scroll – M1K1O Jul 29 '14 at 12:05
  • Don't think this is achievable with CSS. Any trick you'll find, will still easily break / will only work under certain conditions in certain browsers on certain operating systems, if every browser / system setting is set as assumed. You might want to try your luck on one of the Javascript solutions instead, they aren't perfect either but are at least a little more reliable. – gpgekko Jul 29 '14 at 12:06
  • While I can't provide an answer, you should never have horizontal scrolling, it is considered very bad practice and makes your site unfriendly to use. – AndrewB Jul 29 '14 at 12:06
  • can you recommend one gpgekko? – Jordan Jul 29 '14 at 12:06
  • @Jordan This one has a native JS answer and a link to a jQuery dupe: http://stackoverflow.com/questions/3293650/hide-scrollbar-while-still-able-to-scroll-with-mouse-keyboard (some are about width but should work for height too) – gpgekko Jul 29 '14 at 12:08

2 Answers2

3

For webkit-based browsers (apple safari, google chrome, opera) you can get rid of scrollbar with simple CSS:

.scrollable-container-class::-webkit-scrollbar {
    height: 0;
    width: 0;
}

It's easiest, but not cross browser way. Do not forget about (still lame) IE and Firefox. If you can live with empty padding at the top of your container, you can place your scrollable container into another container with overflow:hidden; scrollable container should have position:relative and be shifted down by 17px (usual browser's scrollbar height/width). Example is here.

If you use this way, you should know that zooming page may make your scrollbar visible. Also different browsers/OS may use different scrollbar height/width. For example, Apple Mac scrollbars are located over content, so to hide scrollbars on Mac scrollable container should have padding at bottom.

So, the best way is to combine these two methods: there is no need to do anything for webkit-based browsers as we can do it with simple CSS - IE and FF ignores unknown CSS selectors. For IE and FF we will wrap content into wrapper and shift wrapper up to compensate content shifting (to leave content on it's place). Example is here. Next thing is calculating scrollbar height with JS and set top offset for wrapper and content (not with CSS, but with JS as it's dynamic). I'm using next function to get browser scrollbar height/width:

function getBrowserScrollSize(){

    var css = {
        "border":  "none",
        "box-sizing": "content-box",
        "height":  "200px",
        "margin":  "0",
        "padding": "0",
        "width":   "200px"
    };

    var inner = $("<div>").css($.extend({}, css));
    var outer = $("<div>").css($.extend({
        "left":       "-1000px",
        "overflow":   "scroll",
        "position":   "absolute",
        "top":        "-1000px"
    }, css)).append(inner).appendTo("body")
    .scrollLeft(1000)
    .scrollTop(1000);

    var scrollSize = {
        "height": (outer.offset().top - inner.offset().top) || 0,
        "width": (outer.offset().left - inner.offset().left) || 0
    };

    outer.remove();
    return scrollSize;
}

The last thing that is left - zooming browser. To fix it you should bind handler for window resize event and check if scrollbar size is changed - if it's changed - browser was zoomed in/out and you should update top offset for you content and wrapper.

Please note that FF works under Mac and uses OS scrollbar (which is over content), and FF does not support hiding/styling scrollbar with CSS. I have few ideas how to hide it, but didn't try them yet. I've used these concepts of hiding scrollbar in jQuery Scrollbar plugin.

Gromo
  • 1,579
  • 1
  • 12
  • 14
-5

change Y to X Y = vertical X = horizontal

overflow-x: hidden;
mmeasor
  • 419
  • 3
  • 18