Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap row elements with variable height

I'm currently developing a website using Twitter Bootstrap.

My case: A single .row element containing several elements .col-XX-XX with variable height (product images with variable image ratio)

The number of columns in my design vary from viewport size to viewport-size, which disables me just seperate the elements by use of creating several .row wrappers.

Each element having variable height, due to the element's individual image. Large blank areas (marked with red) with no elements can appear if a large element is appearing as first in the actual row it is appearing.

Variable element heights in .row

My only solution yet is to set a predefined height of the elements in the list, to ensure that no "jumping" can happen when the next element (on a new row) is displayed.

I've also tried to use a javascript, finding the max value of the elements, and then set all elements to this height, however this is a bad solution hence large varity of image heights will also create large blank areas.

Does anyone have a nice solution to this problem?

like image 892
Jens Stigaard Avatar asked Jun 02 '15 14:06

Jens Stigaard


People also ask

How do I make Bootstrap 4 columns equal height?

You just have to use class="row-eq-height" with your class="row" to get equal height columns for previous bootstrap versions.

What is Col lg 2?

col-lg-2: This class is used when the device size is large or greater than 992px and the maximum width of container is 960px and when you want size equal to 2 columns.


1 Answers

With help from Joseph Casey, I found a solution.

CSS: flexbox

By using the styles explained in this link: http://osvaldas.info/flexbox-based-responsive-equal-height-blocks-with-javascript-fallback

I used the following styles:

CSS

.row{
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;

    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
}
.row .item{
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
}

Which achieved that the elements aligns nicely.

The styles is supported by the most newer browsers. I found a list of the browser support of flexbox here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

A little note: In Google Chrome 43, the first row of elements moves the last element down on the next row (last column on first row is empty), but on all following rows, all columns is filled nicely. I haven't found any solution to this yet.

like image 119
Jens Stigaard Avatar answered Sep 22 '22 12:09

Jens Stigaard