2013-05-02 80 views
4

我有很多可變高度的div。我需要這些div彼此排序,但是當它們到達窗口的末端時 - >創建新的「列」。CSS float下和左

Do not overflow but create new "column"

現在的div四溢,但我需要創建新的「列」。

BTW:

我使用的解決方案:

-webkit-column-gap: 16px; 
-webkit-column-width: 230px; 

但我需要支持其他瀏覽器。

謝謝你的幫助!

+3

[列API是由人的現代化的browers supportert(http://caniuse.com/#feat=multicolumn)。你也可以爲舊瀏覽器使用[polyfill](http://github.com/BetleyWhitehome/CSS3MultiColumn)。 – 2013-05-02 07:21:17

+0

這對於「正常」的CSS是不可能的,afaik不能以這種方式控制垂直溢出。水平,但它可能會。 – Christoph 2013-05-02 08:22:43

回答

2

要與CSS僅做到這一點,

一個選項將使用column-count和容器上的max-height

看到DEMO

,但我不知道瀏覽器的前綴相同。

它有點做你想要的,至少在某種程度上,但你可能會更好的與JavaScript的東西。

編輯:這裏我粘貼CSS:

.container { 
    column-count: 3; 
    -webkit-column-count: 3; 
    -moz-column-count: 3; 
    -ms-column-count: 3; 
    -o-column-count: 3; 

    vertical-align:text-top; 

    max-height:100px; 
} 
+0

嗨馬丁!如果你有興趣,我在[這裏回答這個問題]詳細闡述了答案(http://stackoverflow.com/a/16560470/2113185)(例如,如何使元素不會在列之間換行以及如何以更靈敏的方式使用它)。希望你覺得它有用,如果你想,我可以用更多的例子aswe擴展這個答案。最好!^_ ^ – 2013-05-17 10:36:54

0

嘗試類似這樣: 其中maxHeight與窗口的高度完全相同。

<style type="text/css"> 
.maxHeight { 
    display:inline-table; 
    height:600px; 
    width:50px; 
    float:left; 
    background-color:#09F; 
    margin:3px; 
} 
.box { 
    display:inline-block; 
    height:50px; 
    width:50px; 
    background-color:#33F; 
    margin:2px; 
} 
</style> 


<span class="maxHeight"> 
    <span class="box"></span> 
    <span class="box"></span> 
    <span class="box"></span> 
    <span class="box"></span> 
</span> 

<span class="maxHeight"> 
    <span class="box"></span> 
    <span class="box"></span> 
    <span class="box"></span> 
    <span class="box"></span> 
</span> 

<span class="maxHeight"> 
    <span class="box"></span> 
    <span class="box"></span> 
    <span class="box"></span> 
    <span class="box"></span> 
</span> 

JavaScript來循環這將需要一點注意,但它是很有可能的。