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
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.
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.
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.
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.
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;
}
You need to add overflow:
#scroller {
position: absolute;
height: 100%;
overflow:scroll;
overflow-y: hidden;
}
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With