6

I am using the Bootstrap grid for a project and I was wondering if it would be possible to make the columns float vertically. As of right now I can't seem to find a method that makes this possible. I am looking for a solution to this problem in form of some custom css for the Boostrap grid or using another grid. However I would prefer that the grid is set up using css only, no JavaScript.

There's an image and a link for the example:

Example

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <title>Test</title>
    <style type="text/css">
    body {
        margin-top: 20px;
    }

    .container {
        padding: 0 10px;
    }

    @media (min-width: 768px) {
        .container {
            width: 768px;
        }
    }

    @media (min-width: 992px) {
        .container {
            width: 868px;
        }
    }

    .div1, .div2, .div3, .div4, .div5, .div6 {
        background-color: #999;
        border-radius: 6px;
        width: 100%;
        display: table-cell;
        text-align: center;
        vertical-align: middle;
        font: bold 28px 'Arial'; 
    }

    .div1 {
        height: 120px;
    }

    .div2 {
        height: 260px;
    }

    .div3 {
        height: 140px;
    } 

    .div4 {
        height: 180px;
    }

    .div5 {
        height: 200px;
    } 

    .div6 {
        height: 90px;
    }

    .row {
        background-color: #CCC;
        border-radius: 6px;
        padding: 5px;
        margin: 0;
    }

    [class*="col-"] {
        display: table;
        padding: 5px;
    }
    </style>
</head>
<body>

<div class="container">
    <h2><b>Using Bootstrap</b><br>Each DIV is 6 columns wide with a variable height.<br>They're inside the same row.</h2><br>
    <div class="row">
        <div class="col-xs-6">
            <div class="div1">DIV 1</div>
        </div>
        <div class="col-xs-6">
            <div class="div2">DIV 2</div>
        </div>
        <div class="col-xs-6">
            <div class="div3">DIV 3</div>
        </div>
        <div class="col-xs-6">
            <div class="div4">DIV 4</div>
        </div>
        <div class="col-xs-6">
            <div class="div5">DIV 5</div>
        </div>
        <div class="col-xs-6">
            <div class="div6">DIV 6</div>
        </div>
    </div>
</div>

</body>
</html>

Image

splash
  • 13,037
  • 1
  • 44
  • 67
Toby
  • 110
  • 1
  • 2
  • 8
  • It's a common question. CSS can't do that (yet)...not even with `flexbox`. CSS Columns is an option but it would not have the order you indicate. – Paulie_D Jul 04 '15 at 17:18

2 Answers2

1

You need a masonry layout to do this. Use masonry library with your bootstrap code. You can follow this sitepoint post to do this.

Again here is a masonry template using bootstrap on github.

M B Parvez
  • 808
  • 6
  • 16
  • Yeah, that's also the only solution to my problem that I found. I was looking to solve it with pure css, but that might not be possible :( Thanks for the template link, helps alot! – Toby Jul 05 '15 at 07:17
0

Are you looking for something like this? Using the column-properties.

#wrapper{
  -webkit-column-count: 3;
  -webkit-column-gap: 10px;
  -webkit-column-fill: auto;
  -moz-column-count: 3;
  -moz-column-gap: 10px;
  -moz-column-fill: auto;
  column-count: 3;
  column-gap: 15px;
  column-fill: auto;
}
.box{
  width: 150px;
  height: 220px;
  background-color: #eee;
  border-bottom: 1px solid black;
  -webkit-column-break-inside: avoid;
  -moz-column-break-inside: avoid;
  column-break-inside: avoid;
}
<div id="wrapper">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>
Gustaf Gunér
  • 2,272
  • 4
  • 17
  • 23