Bootstrap 3 grid with different height in each item - is it solvable using only CSS?

前端 未结 8 1491
小蘑菇
小蘑菇 2020-12-23 21:20

I\'m trying to make a grid of thumbnails, where each thumbnail has an image and a label. It works fine if all the labels have the same length, because then all the thumbnail

相关标签:
8条回答
  • 2020-12-23 22:15

    You can also do a CSS only clearfix like this. Simply add auto-clear to the row.

    @media (min-width:1200px){
        .auto-clear .col-lg-1:nth-child(12n+1){clear:left;}
        .auto-clear .col-lg-2:nth-child(6n+1){clear:left;}
        .auto-clear .col-lg-3:nth-child(4n+1){clear:left;}
        .auto-clear .col-lg-4:nth-child(3n+1){clear:left;}
        .auto-clear .col-lg-6:nth-child(odd){clear:left;}
    }
    @media (min-width:992px) and (max-width:1199px){
        .auto-clear .col-md-1:nth-child(12n+1){clear:left;}
        .auto-clear .col-md-2:nth-child(6n+1){clear:left;}
        .auto-clear .col-md-3:nth-child(4n+1){clear:left;}
        .auto-clear .col-md-4:nth-child(3n+1){clear:left;}
        .auto-clear .col-md-6:nth-child(odd){clear:left;}
    }
    @media (min-width:768px) and (max-width:991px){
        .auto-clear .col-sm-1:nth-child(12n+1){clear:left;}
        .auto-clear .col-sm-2:nth-child(6n+1){clear:left;}
        .auto-clear .col-sm-3:nth-child(4n+1){clear:left;}
        .auto-clear .col-sm-4:nth-child(3n+1){clear:left;}
        .auto-clear .col-sm-6:nth-child(odd){clear:left;}
    }
    @media (max-width:767px){
        .auto-clear .col-xs-1:nth-child(12n+1){clear:left;}
        .auto-clear .col-xs-2:nth-child(6n+1){clear:left;}
        .auto-clear .col-xs-3:nth-child(4n+1){clear:left;}
        .auto-clear .col-xs-4:nth-child(3n+1){clear:left;}
        .auto-clear .col-xs-6:nth-child(odd){clear:left;}
    }
    

    http://www.codeply.com/go/lUbs1JgXUd

    0 讨论(0)
  • 2020-12-23 22:19

    I think it's better to use some few row of js. With CSS you could only have a new "row" using clearfix class (as answered before), but each div will have different height. If you wish to set the same height to each dynamic div, I think you could do it only by js.

      $(document).ready(function() {
        var maxHeight = 0;          
        $(".equalize").each(function(){
          if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
        });         
        $(".equalize").height(maxHeight);
      }); 
    

    Here is a working demo based on your code.

    All what you have to do is giving the same class to each main div inside the col--, and than run a each() function in js that give to them the same height (the max-one).

    Your HTML structure should looks like that:

    <div class="container">
      <div class="col-md-4">
        <div class="thumbnail equalize">
    
          <!-- your HTML content -->
    
        </div> <!-- /.thumbnail equalize -->
      </div> <!-- /.col-md-4 -->
    </div> <!-- /.container -->
    
    0 讨论(0)
提交回复
热议问题