user3338209
user3338209

Reputation: 73

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>

Upvotes: 7

Views: 6317

Answers (3)

Chris K.
Chris K.

Reputation: 1078

This is actually possible with pure CSS:

.container {
    column-count: 3;
}

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

Upvotes: 3

user3135691
user3135691

Reputation: 183

You could use something like this: (lets say 6 grid columns) #subcontainer > div { float:left; display:inline; margin: 0 20px 0 0; } #subcontainer > div:nth-of-type(6n+6) { margin-right: 0; }

    #ids or .classes with height and width {
    ...
    }

That way you are always targeting the 6th child of #subcontainer independent from classes or ids for the right border but center divs will always have the same margin

Upvotes: 0

MichaelM
MichaelM

Reputation: 1099

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;
}

Upvotes: 2

Related Questions