0

I have to pull data from a database into columns, but the total columns add up to more than 12. The columns have different heights. This messes up the grid. Does anyone know how to solve this?

<div class="container">
  <div class="row">   
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
   <div class="col-md-3"> ... </div>
  </div>
 </div>
Dan
  • 8,983
  • 5
  • 37
  • 71

3 Answers3

1

Option 1 is to add a standard height to all of your <div>. So you could create a class like .div-200px.

Change your <div class="col-md-3"> to <div class="div-200px col-md-3">

Edit your CSS:

 .div-200px {
     height: 200px; 
}

Option 2 is to create a foreach loop and add a new <div class="row"> for every 4 products.

<div class="container">
    <?php $count = 0;
    foreach($data as $item){
        if(is_int($count/4)){ ?>
            <div class="row">
        <?php } ?>
            <div class="col-md-3"> <?=$item?> </div>
        if($count>0 AND is_int($count/4)){ ?>
            </div>
        <?php } 
        $count++;
    } ?>
</div>

That's not the prettiest thing but it should work.

Dan
  • 8,983
  • 5
  • 37
  • 71
0

There is another option with CSS, add a "item" class to your col-md-3 divs and give it the following css:

.item:nth-child(4n+1){
    clear:left
}
NicoFerna
  • 571
  • 2
  • 10
-1

I think you should not solve this with bootstrap grid system at all. Just use <table> tag and stop caring about a number of columns. Responsiveness shouldn't suffer

Roman Kolpak
  • 1,842
  • 20
  • 22
  • This really depends on what the content is. A table may or may not have the styling OP is looking for. It is a good suggestion though it's not necessarily an answer to this question. – Dan Aug 02 '14 at 16:40