Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seamlessly align block divs of different sizes?

I want to create a div box 'grid' so to speak.

Currently my div boxes are not aligning correctly. div boxes of different height cause a large vertical space in between some divs. The div boxes are also going more to the right side.

I want the div boxes to keep the same margin from each other regardless of the height of the boxes and for them to line-up from the left > right.

I want the div boxes to align something like this:

Here's the example of what's happening: http://jsfiddle.net/P4S8z/

HTML:

.container {
  position: relative;
  float: left;
  margin: 0;
}

.box {
  position: relative;
  display: block;
  float: left;
  width: 250px;
  margin-left: 1.5em;
  margin-bottom: 0.5em;
  padding: 0 10px 0;
  color: #666;
  background: #fff;
  border: 1px solid #d2d2d2;
  border-radius: 3px;
}

.box h3 {
  position: relative;
  display: block;
  height: 20px;
  line-height: 1.3em;
  width: 260px;
  margin: 0;
  padding: 5px 10px;
  left: -15px;
  top: 8px;
  color: #cfcfcf;
  text-shadow: 0 1px 1px #111;
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  background: #333;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
<div class="container">

  <div class="box" style="height:225px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:160px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:200px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:180px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:150px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:170px;">
    <h3>Blah blah</h3>
  </div>

</div>
like image 387
user3338209 Avatar asked Feb 21 '14 17:02

user3338209


2 Answers

The simplest way I can think of using pure CSS would be to designate columns.

I surrounded each "column" with a div, and floated those divs left.

Here's an updated fiddle: http://jsfiddle.net/Renson/P4S8z/4/

This should keep the margins equal like you wanted

New HTML

<div class="container">
<div class="subcontainer">
    <div class="box" style="height:225px;">
        <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:180px;">
        <h3>Blah blah</h3>
    </div>
</div>
<div class="subcontainer">
    <div class="box" style="height:160px;">
        <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:200px;">
        <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:150px;">
    <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:170px;">
    <h3>Blah blah</h3>
    </div>
</div>

Added to CSS

.subcontainer{
    float: left;
}
like image 135
MichaelM Avatar answered Sep 28 '22 16:09

MichaelM


This is actually possible with pure CSS:

.container {
    column-count: 3;
}

At least in modern browsers: https://caniuse.com/#search=column-count

like image 41
Chris K. Avatar answered Sep 28 '22 16:09

Chris K.