Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically created bootstrap columns with different heights, float to left

Consider an unknown number of divs being created dynamically and styled using the bootstrap grid system. In this example, I'm using col-sm-4 so after every third block, we move to a new row. The blocks (divs) can be different heights, which is determined by the content within.

This is where I run into the layout problem. When moving to a new row, I want the fourth block to float to the left. This only happens when the left most div in the row above is also the shortest. I have pictures to illustrate.

Real Life:

enter image description here

The Dream:

enter image description here

The "correct" way to do this would be to wrap every three in a row class I beleive, but I'm not sure how to do this with dynamic content (could probably hack it) or if there's an easy css solution.

HTML:

<div class="row">
  <div class="col-sm-12">
    <div class="col-sm-4 block">
      <div class="inner-block"></div>
    </div>
    <div class="col-sm-4 block">
      <div class="inner-block"></div>
    </div>
    <div class="col-sm-4 block">
      <div class="inner-block" style="height:150px"></div>
    </div>
    <div class="col-sm-4 block">
      <div class="inner-block"></div>
    </div>
  </div>
</div>

CSS:

.block {
  padding: 5px;
}

.inner-block {
  height: 200px;
  background-color: blue;
}

Plunker Example (expand preview to proper size)

like image 695
aw04 Avatar asked Aug 13 '14 15:08

aw04


People also ask

What is normally used to clear floats to correct problems that occur with uneven column heights?

. clearfix : Normally used to clear floats, adding this class to any column will make it shift to a new row automatically, to help you correct problems that occur with uneven column heights.

How do I change the position of a column in Bootstrap?

We can easily change the order of built-in grid columns with push and pull column classes. The Push and Pull Classes: The push class will move columns to the right while the pull class will move columns to the left.

What is Col Md offset in Bootstrap?

col-md-offset-* to leave a particular number of virtual Bootstrap columns to the left of any column (kind of like invisible place holders).

What are responsive columns in Bootstrap?

Bootstrap's grid system is responsive, and the columns will re-arrange depending on the screen size: On a big screen it might look better with the content organized in three columns, but on a small screen it would be better if the content items were stacked on top of each other.


1 Answers

If your system is unable to add first/last classes on every nth div, then you can use the nth-child css pseudo selector.

@media (min-width: 768px) {// For medium displays and above
  .col-sm-4:nth-child(3n+1) { // We target every 3rd div but offset the count by 1 so that that 1st, 4th 7th etc divs are cleared
    clear:both; // Clear the float
  }
}
like image 143
Piers Avatar answered Oct 26 '22 09:10

Piers