开发者

Stack divs with different heights

开发者 https://www.devze.com 2023-03-08 05:18 出处:网络
I want to stack divs with different heights but 开发者_运维技巧same width within a div container.. from top to bottom going right.

I want to stack divs with different heights but 开发者_运维技巧same width within a div container.. from top to bottom going right.

Problem now is with divs that are short.. gives a ugly gap to the div above.

I've added a small sketch with what i want to do..

Stack divs with different heights

Thanks from norway!


I suppose that you are using jQuery on your site. From the sketch I suggest to take a look at jQuery plugin called Masonry.


CSS:

.column { width:20em; float:left }
.column div { background:red; margin:1em }

HTML:

<div class="column">
    <div></div>
    <div></div>
    <div></div>
</div>
<div class="column">
    <div></div>
    <div></div>
    <div></div>
</div>
<div class="column">
    <div></div>
    <div></div>
    <div></div>
</div>


Have a look at using CSS Columns

Here's the W3C spec, but a slightly easier read might be PPK's write up.


Use three column divs within container div. Each floats left. Add a div at the top and at the bottom that's empty and clears on both sides.

.column { float: left; width: whatever you want it to be; margin-left: whatever you want it to be; }

.clear{ clear: both; height: 0px; }

.column div{ margin-bottom: whatever you want it to be; width: whatever you want it to be; }

       <div id='container'>

<div class='clear'>&nbsp;</div>

  <div class='column'>
<div>blah blah blah</div><div>blah blah blah</div>... etc
  </div>

  <div class='column'>
 <div>blah blah blah</div><div>blah blah blah</div>... etc
   </div>

  <div class='column'>
<div>blah blah blah</div><div>blah blah blah</div>... etc
  </div>

<div class='clear'>&nbsp;</div>

       </div>
0

精彩评论

暂无评论...
验证码 换一张
取 消