我在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/
谢谢!
答案 0 :(得分:23)
使用display:inline-block
代替float:left
,并将white-space:nowrap;
添加到容器中。如果需要,请将white-space:normal
添加到内容元素。 Demo
答案 1 :(得分:3)
试试这个:
float: left
替换为display: inline-block;
white-space: nowrap;
用于容器。CSS:
#container {
width: 100px;
background-color: #CCC;
overflow: auto;
height: 100px;
white-space: nowrap;
}
.contents {
width: 35px;
height: 60px;
display: inline-block;
}
答案 2 :(得分:0)
在根DIV元素中,您始终可以指定溢出是可滚动的:
<div id="container" style="overflow: scroll;">
<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>