Making div contents scroll horizontally and not vertically

loriensleafs picture loriensleafs · Mar 23, 2012 · Viewed 17.2k times · Source

I have a div with some content, a bunch of thumbs, I'd like them to always be horizontal and any overflow gets scrolled horizontally. I'd like the div to take up 100 percent width, keeping the thumbs a group centered, meaning when the page is wider then the group they stay centered and not stuck to the left. I have a jsfiddle and can't seem to figure out why it's not working, it always pushes the overflow into a second row instead of allowing the overflow feature to take over.

http://jsfiddle.net/z5nEQ/1/ that's the fiddle and the code in that is:

css:

#wrapper{
  width:100%;
  height:90px;
  border:1px solid red;
}

.box{
  width:50px;
  height:100px;
  border:1px solid black;
  float:left;
}

#container{
  width:100%;
  height:200px;
  float:left;
  overflow-x:scroll;
}

html:

<div align="center" id="wrapper">
  <div id="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>   
</div>​

any thoughts on this? Thanks for any help ​

Answer

sandeep picture sandeep · Mar 23, 2012

Write like this:

.box{
    width:50px;
    height:100px;
    border:1px solid black;
    display:inline-block;
    *dsplay:inline;/* For IE7*/
    *zoom:1;/* For IE7*/
    white-space:normal;
}
#container{
    width:100%;
    height:200px;
    float:left;
    overflow-x:scroll;
    white-space:nowrap;
}

Check this http://jsfiddle.net/z5nEQ/3/