我在一个具有固定宽度的 div 中有几个 HTML 元素。内部元素的宽度之和大于容器的宽度。如何使内部元素显示为内联(并显示水平滚动)而不是在达到父元素的宽度后中断?
我可以添加一个包装器并为其分配一个最小宽度,但我想要的东西会在内容改变时改变它的大小。
<div id="container">
<div class="contents" id="one"></div>
<div class="contents" id="two"></div>
<div class="contents" id="three"></div>
<div class="contents" id="four"></div>
</div>
#container {
width: 100px;
background-color: #CCC;
overflow: auto;
height: 100px;
}
.contents {
width: 35px;
height: 60px;
float: left;
}
#one {
background-color:#ABC;
}
#two {
background-color:#333;
}
#three {
background-color:#888;
}
#four {
background-color:#AAA;
}
http://jsfiddle.net/elohr/G5YZ6/2/
谢谢!
最佳答案
使用display:inline-block
代替float:left
,并在容器中添加white-space:nowrap;
。如果需要,将 white-space:normal
添加到内容元素。 Demo
关于html - DIV 水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16205151/