Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a div which will take an "infinite width" (horizontal scroller)?

Tags:

css

I would like to draw a simple horizontal scroller.

My problem is my elements inside my scroller don't take an infinite width, so after using 100% of the width parent, the next elem will be display to a new line. I was thinking an absolute div was taking an infinite width by default but apparently not, how to make it works ?

I specify that the number of elems is dynamic.

Here is a JSFiddle representing my issue

like image 895
Ludo Avatar asked Aug 23 '13 13:08

Ludo


People also ask

How do you make a div scroll horizontally?

Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line. Here the scroll div will be horizontally scrollable.

How do I force a div to full width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do I keep a div to scroll?

Use position fixed. Position fixed makes it so that an element stays at it's specified place even if the user scrolls up or down.

How do I scroll horizontally in CSS?

To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.


3 Answers

This is an easy way to do it:

http://jsfiddle.net/thirtydot/gakqm/10/

#scroller-wrapper {
    width: 100%;
    height: 200px;
    background-color: red;
}

#scroller {
    height: 100%;
    overflow-x: auto;
    white-space: nowrap;
}

.elem {
    height: 100%;
    display: inline-block;
    outline: 1px solid blue;
}
like image 199
thirtydot Avatar answered Oct 29 '22 20:10

thirtydot


You need to add overflow:

#scroller {
    position: absolute;
    height: 100%;
    overflow:scroll;
    overflow-y: hidden;
}
like image 25
SaturnsEye Avatar answered Oct 29 '22 21:10

SaturnsEye


CSS:

#scroller-wrapper {
    height: 200px;
    width: 100%;
    background-color:red;
    position: relative;
    overflow-x: scroll;
}

#scroller {
    position: absolute;
    height: 100%;
    white-space: nowrap;
}

.elem {
    height: 100%;
    padding: 0;
    margin: 0;
    overflow: hidden;
    width: auto;
    display: inline-block;
}

http://jsfiddle.net/gakqm/15/

like image 2
JMParsons Avatar answered Oct 29 '22 21:10

JMParsons