CSS 非环绕浮动 div
我需要创建一个包含可变数量(浮动?)div 的单行:容器尺寸是固定的,并且应该在需要时添加水平滚动条,而不是换行。
我尝试了以下方法,但它不起作用:它会换行。
div#sub {
background-image:url("../gfx/CorniceSotto.png");
width:760px;
height:190px;
}
div#sub > div#ranking {
position:relative;
top:42px;
left:7px;
width:722px;
height:125px;
overflow-x:auto;
overflow-y:hidden;
}
div#sub > div#ranking > div.player {
float:left;
width:67px;
height:120px;
margin-left:5px;
background-color:#f3e1bb;
}
我尝试了一些东西(内联、表格单元格等),但它们都失败了。
这可以做到吗?如果是这样,怎么办?
I need to create a single line containing a variable amount of (floating?) divs: the container dimension is fixed, and it is supposed to add an horizontal scrollbar when needed, never wrapping.
I tried the following, but it doesn't work: it wraps instead.
div#sub {
background-image:url("../gfx/CorniceSotto.png");
width:760px;
height:190px;
}
div#sub > div#ranking {
position:relative;
top:42px;
left:7px;
width:722px;
height:125px;
overflow-x:auto;
overflow-y:hidden;
}
div#sub > div#ranking > div.player {
float:left;
width:67px;
height:120px;
margin-left:5px;
background-color:#f3e1bb;
}
I've tried a few things (inline,table-cell,etc.) but they all failed.
Can this be done? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
display: inline-block
而不是float
并为容器指定white-space: nowrap
。这里有一个例子: http://jsfiddle.net/D5hUu/3/
Use
display: inline-block
instead offloat
and give the containerwhite-space: nowrap
.Here an example: http://jsfiddle.net/D5hUu/3/
内联不起作用,表格单元格应该起作用 - 请参阅我在回答类似问题时所做的jsFiddle:
http: //jsfiddle.net/DxZbV/1/
在 <=ie7 中不起作用...
inline won't work, table-cell should work - see this jsFiddle I made in answer to a similar question:
http://jsfiddle.net/DxZbV/1/
won't work in <=ie7 though...
哎呀,我看错了问题。之前的答案已删除。
在你的容器上,
white-space: nowrap
然后在元素上display: inline-block
在这里小提琴: http://jsfiddle.net/HZzrk/1/
whoops, I misread the question. Previous answer removed.
on your container,
white-space: nowrap
and then on the elementsdisplay: inline-block
Fiddle here: http://jsfiddle.net/HZzrk/1/
编辑:结合 DanielB 和我原来的答案。您需要为
sub
和ranking
放置min-width
而不是width
并将元素设置为 < code>inline-block 容器具有white-space: nowrap
。请参阅:http://jsfiddle.net/5wRXw/3/编辑 2:出于您的目的,最好消除
ranking
元素上的overflow
属性。请参阅http://jsfiddle.net/5wRXw/4/Edited: Combined DanielB's and my original answer. You need to put
min-width
instead ofwidth
for bothsub
andranking
and have the elements set toinline-block
with container havingwhite-space: nowrap
. See: http://jsfiddle.net/5wRXw/3/Edit 2: For your purposes, it might be better to eliminate the
overflow
properties all together on theranking
element. See http://jsfiddle.net/5wRXw/4/