如何互锁div?

时间:2013-02-07 10:11:12

标签: css html pinterest interlocked

我想从这里联系一个未定义数量的div(如在Pinterest中):

++++++++ ********
+      + *      *
+      + *      *
+      + ********
+      +         
++++++++ 
~~~~~~~~ --------
~      ~ -      -
~      ~ -      -
~~~~~~~~ -      -
         -      -
         -      -
         -      -
         -      -
         --------

到此:

++++++++ ********
+      + *      *
+      + *      *
+      + ********
+      + --------
++++++++ -      -
~~~~~~~~ -      -
~      ~ -      -
~      ~ -      -
~~~~~~~~ -      -
         -      -
         -      -
         --------

是否可以使用CSS或其他东西?

2 个答案:

答案 0 :(得分:3)

这里已被问过一百万次。

大多数民众使用砌体:

http://masonry.desandro.com/

答案 1 :(得分:1)

由于zessx,我在zessx answer引用了这个答案。

<h2>Without columns</h2>
<div class="box">1 Lorem Ipsum</div>
<div class="box">2 Lorem Ipsum Lorem Ipsum</div>
<div class="box">3 Lorem Ipsum</div>
<p class="clear">&nbsp;</p>
<div class="box">4 Lorem Ipsum Lorem</div>
<div class="box">5 Lorem Ipsum</div>
<div class="box">6 Lorem Ipsum</div>
<p class="clear">&nbsp;</p>
<div class="box">7 Lorem Ipsum</div>
<div class="box">8 Lorem Ipsum</div>
<h2>With columns</h2>
<div class="column">
<div class="box">1 Lorem Ipsum</div>
<div class="box">4 Lorem Ipsum Lorem</div>
<div class="box">7 Lorem Ipsum</div>
</div>
<div class="column">
<div class="box">2 Lorem Ipsum Lorem Ipsum</div>
<div class="box">5 Lorem Ipsum</div>
<div class="box">8 Lorem Ipsum</div>
</div>
<div class="column">
<div class="box">3 Lorem Ipsum</div>
<div class="box">6 Lorem Ipsum</div>
</div>

CSS

.box { 
width: 50px;
min-height: 50px;
background: #ccc;
margin: 3px;
padding: 3px;
float: left;
}
.clear {
clear: both;
height: 0px;
}
.column {
width:60px;
float: left;
}

See Demo