Why isn't my div scrolling horizontally when adding multiple images?

谁说胖子不能爱 提交于 2019-12-06 07:46:30

Will this do? I simplified it somewhat.

CSS (you can remove the borders, they are just so you can see what is happening):

#grid-container {position: relative; width: 300px; height: 400px; overflow: auto; border: 1px red solid;}
#grid {border: 1px blue solid;}
#grid ul {height: 40px; list-style-type: none; white-space: nowrap; padding: 0; margin: 0; border: 1px green solid;}
#grid ul li {display: inline; padding: 0; margin: 0;}
#grid ul li img {height: 50px;}

HTML:

<div id="grid-container">
 <div id="grid">
  <ul>
    <li><img src="testimage.jpg"></li>
    <li><img src="testimage.jpg"></li>
    <li><img src="testimage.jpg"></li>
    <li><img src="testimage.jpg"></li>
    <li><img src="testimage.jpg"></li>
    <li><img src="testimage.jpg"></li>
  </ul>
  <ul>
   <li><img src="testimage.jpg"></li>
   <li><img src="testimage.jpg"></li>
   <li><img src="testimage.jpg"></li>
  </ul>
 </div>
</div>

You need to put a white-space:nowrap; on the UL and LI tags. You also need the LI elements to be display inline rather than floating them.

CSS:

#grid-container   { left:33px; position:relative; width:300px; }
#grid   { width:310px; height:400px; overflow:auto; margin-bottom: 15px; }
#grid-container ul   { width:305px; }
#grid-container li   { 
    display:inline;
   list-style-type:none; 
    padding:5px 15px 5px 15px; 
    height:88px; 
    margin:0;
    text-align:center; 
}

ul, li{
    white-space:nowrap;
}

HTML:

<div id="grid-container">
<div id="grid"> 
<div class="image-row"> 
  <ul> 
    <li> 
      <img id="img1" class="grid-image" src="test.jpg" /> 
    </li>
    <li>
      <img id="img2" class="grid-image" src="test.jpg" /> 
    </li>
    <li>
      <img id="img3" class="grid-image" src="test.jpg" /> 
    </li>
    <li>
      <img id="img4" class="grid-image" src="test.jpg" /> 
    </li>
  </ul>
</div>
<div class="image-row"> 
  <ul> 
    <li> 
      <img id="img5" class="grid-image" src="test.jpg" /> 
    </li>
    <li>
      <img id="img6" class="grid-image" src="test.jpg" /> 
    </li>
  </ul>
</div>

This works in latest versions of IE, FF, Safari and Chrome.

try this:

#grid-container li { 
  display: inline;
  list-style-type:none; 
  padding:5px 15px 5px 15px; 
  height:88px; 
  text-align:center;
  white-space: nowrap;
}

You're already on the right path: Your approach to set "float: left;" on the <li>s, in combination with setting "width: 305px" on the <ul>s should basically work to avoid float dropping.

However, a few things to check:

  1. Are 305px really enough? (It must be at least as large as the combined width of the elements in row 1, including margins, paddings and borders) Try setting it to a much larger value. overflow: auto won't help, since the floats will always wrap instead of causing an overflow. Setting a large enough width works perfectly for me (at least in my own example).

Other things to try:

  • Try it without floating ".image-row".
  • Try setting a width on the images.
  • You have the id "row1" twice in your HTML - that's invalid.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!