How to display a horizontal scroll bar

I have a #items div that wraps an entire .item chain. I want to display elements next to each other, and if they are larger than the page width, display a horizontal scroll bar.

 <div id="somediv"></div> <div id="items"> <div class="item"> Item content </div> </div> <div id="someotherdiv"></div> 

I tried something like this, but it does not work

 #items{ overflow: auto; width:100%; height:200px; /* this is the height of each item*/ } .item{ float:left; } 

I thought it was a way to do this, but I can't get it to work, so I am open to corrections and other ways.

+6
html css
source share
4 answers

You're on the right track, but you'll need an extra shell to make it work ...

 <div id="scrollable"> <div id="items"> <div class="item"> Item content </div> </div> </div> 

and then your CSS:

  #scrollable { overflow: auto; width:100%; height:200px; } #items { width: 3000px; /* itemWidth x itemCount */ } .item{ float:left; } 
+5
source share

This previous question may help: CSS div element - how to show only horizontal scrollbars?

So, instead of the current css, change it to:

 #items{ overflow-x: scroll; overflow-y: auto; width:100%; height:200px } .item{ float:left; } 

Try and tune if necessary.

+2
source share

This question is related to fooobar.com/questions/258898 / ... , where animuson explains that floating elements cannot be measured.

 #items{ overflow: auto; white-space: nowrap; width:100%; height:200px } .item{ display: inline-block; } 

And the same jsfiddle

+1
source share

I would prefer that you do not provide a width for the # elements. In my case, the .item number was dynamic, and summing them up was not in my preference.

 #items{ overflow: auto; white-space:nowrap; } .item { display:inline; } 
0
source share

All Articles