您可以将行放在列内,而不是将列放在行内。请参见https://jsfiddle.net/mendesjuan/89t1g002/1/
<div class="row">
<div class="col-md-6">
<div class="panel">Content </div>
<div class="panel">Content </div>
</div>
<div class="col-md-6">
<div class="panel">Content</div>
<div class="panel">Content</div>
</div>
</div>
根据您当前的HTML DOM结构,无法实现您想要的效果。但是,如果稍微修改一下DOM,就可以实现所需的结果。我在这里制作了一个示例。
请查看旧代码和新代码:
HTML:
<div class="container">
<div class="row">
<h4>
OLD
</h4>
<div id="div1" class="cols col-xs-6">
<h2>
Div 1
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
<div id="div2" class="cols col-xs-6">
<h2>
Div 2
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
<div id="div3" class="cols col-xs-6">
<h2>
Div 3
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
<div id="div4" class="cols col-xs-6">
<h2>
Div 4
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
</div>
</div>
<div class="sep">
</div>
<div class="container">
<div class="row">
<h4>
NEW
</h4>
<div class="cols col-xs-6">
<h2>
Div 1
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
<div class="cols col-xs-6">
<h2>
Div 2
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
<div class="cols col-xs-6">
<h2>
Div 3
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
<div class="cols col-xs-6">
<h2>
Div 4
</h2>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
</div>
</div>
CSS:
.cols {
border: 1px solid red;
}
.sep{
border: 2px solid;
margin: 20px 0;
}
使用 Twitter Bootstrap 的网格系统无法实现此功能。该网格使用 CSS 浮动,右上浮动的元素将不允许第二行的列与第一行的列在同一行内对齐。您想要的可以使用提供了一个系统,使元素填充空白空间的 JavaScript 库来完成,无论大小和数量如何。
我猜您可以使用 masonry grid system 来满足需求。
另一个(可能简单)的选项是嵌套您的列和行。请参见网格嵌套。您可以使用两个col-sm-6
作为主列,并在每个列中放置任意数量的新行,其中包含所需数量的col-sm-12
列。
请参见示例片段。
body {
padding-top: 50px;
}
code {
border-radius: 3px
}
.bs-callout+.bs-callout {
margin-top: -5px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<section class="col-sm-6">
<div class="row">
<section class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Lorem Ipsum is simply dummy text of <b>ONE</b>
</div>
<div class="panel-body">
<p>Lorem Ipsum is simply dummy text of the printing and typesetti.</p>
</div>
<table class="table table-striped">
<tbody>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>List files matching a pattern</td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Lorem Ipsum is simply dummy text of <b>TWO</b>
</div>
<div class="panel-body">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</div>
<table class="table table-striped">
<tbody>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>List files matching a pattern</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>List files matching a pattern</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Lorem Ipsum is simply dummy text of <b>THREE</b>
</div>
<div class="panel-body">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard .</p>
</div>
<table class="table table-striped">
<tbody>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
</section>
<section class="col-sm-6">
<div class="row">
<section class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Lorem Ipsum is simply dummy text of <b>FOUR</b>
</div>
<div class="panel-body">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</div>
<table class="table table-striped">
<tbody>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Lorem Ipsum is simply dummy text of <b>FIVE</b>
</div>
<div class="panel-body">
<p>Lorem Ipsum is simply dummy tex.</p>
</div>
<table class="table table-striped">
<tbody>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Lorem Ipsum is simply dummy text of <b>SIX</b>
</div>
<div class="panel-body">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</div>
<table class="table table-striped">
<tbody>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
<tr>
<td><code>To switch directories, type</code>
</td>
<td>To switch directories, type</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
</section>
</div>
</div>