简体   繁体   English

阻止浮动的div包装到新行

[英]Stopping floated divs from wrapping to a new line

I have a container that contains a number of left-floated divs, something like: 我有一个包含许多左浮动div的容器,类似于:

<div id="container">
    <div class="panel"></div>
    <div class="panel"></div>
    <div class="panel"></div>
</div>

<style>
    .panel {float:left; width:200px;}

</style>

In my app, the user can add more and more panels to the container dynamically, and I'd like them to always stack up on the right, and for a horiz scroll bar to appear when the user has exceeded the available space. 在我的应用程序中,用户可以动态地向容器添加越来越多的面板,我希望它们总是在右侧堆叠,并且当用户超出可用空间时,可以显示水平滚动条。

jsfiddle here: http://jsfiddle.net/yzTFC/6/ jsfiddle: http//jsfiddle.net/yzTFC/6/

Floated elements are dependent upon the width of their container and do no affect the size of the container. 浮动元素取决于其容器的宽度,不会影响容器的大小。 If it is possible, do not use floated elements in this case. 如果可能,请不要在这种情况下使用浮动元素。 I would use display:inline-block with a white-space:nowrap on the parent. 我会使用display:inline-block with a white-space:nowrap on parent。

http://jsfiddle.net/yzTFC/45/ http://jsfiddle.net/yzTFC/45/

Another method would be to give an insanely large width on the container but I wouldn't advise this as it is seems sloppy. 另一种方法是在容器上给出一个非常大的宽度,但我不建议这样做,因为它看起来很草率。

http://jsfiddle.net/yzTFC/52/ http://jsfiddle.net/yzTFC/52/

Here is a good article on how floats work: http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/ 这是一篇关于浮点数如何工作的好文章: http//coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/

Try this: 试试这个:

HTML HTML

<div id="parent-container">
    <div id="container">
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
    </div>
</div>​

CSS CSS

.panel {float:left; width:200px; background-color:red; height:100px; border:solid 1px black;}

#container { width: 9999px; }

#parent-container {
    overflow: auto;
}​

Demo: http://jsfiddle.net/yzTFC/50/ 演示: http //jsfiddle.net/yzTFC/50/

My answer is similar to Ayman. 我的回答与艾曼相似。 I have also added another container. 我还添加了另一个容器。

HTML HTML

<div id="wrapper">
    <div id="panel-container">
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
        <div class="panel"></div>
    </div>
</div>​

CSS CSS

.panel {float: left; width:200px; background-color:red; height:100px; border:solid 1px black;}
#wrapper {width: 606px; overflow-x: scroll; overflow-y: hidden; }
#panel-container {width: 1212px;}

Where #panel-container CSS width is a multiple of the .panels within it, so it would take some scripting to achieve this. 其中#plate-container CSS width是其中.panels的倍数,因此需要一些脚本才能实现这一点。 Most likely some javascript. 最有可能是一些javascript。

The #wrapper width was arbitrary, I just decided to show three panels at a time in the viewport. #wrapper宽度是任意的,我只是决定在视口中一次显示三个面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM