html - Bootstrap row with columns of different height -
i have like:
<div class="row"> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> </div>
now assuming that, content
boxes of different height, same width - how keep same "grid based layout" , yet have boxes line under each other, instead of in perfect lines.
currently twbs place next line of col-md-4
under longest element in previous third row., each row of items aligned clean - while awesome want each item fall directly under last element
this popular bootstrap question, i've updated , expanded answer.
the bootstrap "height problem" occurs because columns use float:left
. when column “floated” it’s taken out of normal flow of document. shifted left or right until touches edge of containing box. so, when have uneven column heights, correct behavior stack them closest side.
note: options below applicable column wrapping scenarios there more 12 col units in single .row
. readers don't understand why there more 12 cols in row, or think solution "use separate rows" please read first!
there few ways fix this.. (updated 2017)
1 - 'clearfix' approach (recommended bootstrap) (requires iteration every x columns). force wrap every x number of columns...
<div class="row"> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="clearfix"></div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="clearfix"></div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> <div class="col-md-4">content</div> </div>
clearfix demo (responsive tiers) - eg. col-sm-6 col-md-4 col-lg-3
there css-only variation of 'clearfix'
css-only clearfix tables
2 - make columns same height (using flexbox):
since issue caused difference in height, can make columns equal height across each row. flexbox best way this, , natively supported in bootstrap 4...
.row.display-flex { display: flex; flex-wrap: wrap; } .row.display-flex > [class*='col-'] { display: flex; flex-direction: column; }
3 - un-float columns use inline-block instead..
again, height problem occurs because columns floated. option set columns display:inline-block
, float:none
. provides more flexibility vertical-alignment. however, solution there must no html whitespace between columns, otherwise inline-block elements have additional space , wrap prematurely.
4 - css3 columns approach (masonry-like css solution)..
this not native bootstrap 3, approach using css multi-columns. 1 downside approach column order top-to-bottom instead of left-to-right. bootstrap 4 include type of solution: bootstrap 4 masonry cards demo.
bootstrap 3 multi-columns demo
5 - masonry javascript/jquery approach
finally, may want use plugin such isotope/masonry:
bootstrap masonry demo
masonry demo 2
Comments
Post a Comment