Bootstrap网格系统:如何在垂直方向上浮动列

6
我正在使用Bootstrap网格布局做一个项目,想知道是否可以让列垂直浮动。目前我找不到一种方法能够实现这一点。我正在寻找一种解决方案,可以通过自定义CSS或使用另一个网格来解决这个问题。但是,我更喜欢只使用CSS来设置网格,而不使用JavaScript。
以下是示例的图片和链接:

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


这是一个常见的问题。CSS目前还不能做到这一点,即使使用flexbox也不行。CSS列是一个选项,但它不会按照您指定的顺序排列。 - Paulie_D
2个回答

1
你需要一个瀑布流布局来实现这个。使用masonry库和你的bootstrap代码。你可以参考这个sitepoint post 来做这个。
此外,这里有一个在github上使用bootstrap的masonry模板

是的,这也是我找到的唯一解决方案。我曾试图用纯CSS来解决它,但可能不可能:(感谢模板链接,非常有帮助! - Toby

0
你是否在寻找类似这样的东西?使用column属性。

#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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接