html - DIV 水平滚动

标签 html css

我在一个具有固定宽度的 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/

相关文章:

javascript - 在添加 DIV 元素之前删除它

html - 调整表格的最后一列以适应内容,但不是第一列

javascript - 由于折旧而转换为 ajax async false 替代方案

javascript - 中心 div(位置 :absolute) vertically and horizontally to screen view

css - 是否可以使用异形 iframe?

html - img 悬停时的不透明度和颜色变化

javascript - 在可编辑模式下添加新的 Div

css - DIV 在 Chrome/Firefox 中居中很好,但在 Safari 中不居中

html - IE6 在这里搞砸了……嘿!

javascript - 蓝色到灰色 CSS 颜色在 javascript 中计算