【问题标题】:How to remove vertical space between divs in Bootstrap 3 grid layout?如何在 Bootstrap 3 网格布局中删除 div 之间的垂直空间?
【发布时间】:2014-06-03 01:19:13
【问题描述】:

我的 Bootstrap 3 网格布局呈现如下:

但是,我不想要第一个 div(黄色)和第三个 div(红色)之间的空间。

这是我的代码:

<div class='row'>   
<div class="col-xs-12 col-sm-6" style='background-color:yellow;height:100px'>1st div</div>
<div class="col-xs-12 col-sm-6" style='background-color:blue;height:200px'>2nd div</div>
<div class="col-xs-12 col-sm-6" style='background-color:red;height:100px'>3rd div</div>
</div>

有什么想法吗?

更新:

我想我找到了解决办法..

<div class='row'>
<div class="col-xs-12 col-sm-6" style='background-color:yellow;height:200px'>1st Div</div>
<div class="col-xs-12 col-sm-6" style='float:right;background-color:blue;height:600px'>2nd Div</div><br/><div class="clearfix visible-xs"></div>
<div class="col-xs-12 col-sm-6"  style='background-color:red;height:2000px'>3rd Div</div>

【问题讨论】:

  • 您可以执行this 之类的操作,但在小屏幕上无法正确排序
  • 我需要在小屏幕上保留订单
  • 我不认为只能使用本机引导类来完成。而是创建新的。
  • 我不知道为什么这个问题被否决了,响应式模板中的顺序很重要。

标签: css twitter-bootstrap-3 grid-layout


【解决方案1】:

老问题,但是……

您的解决方案看起来不错 - 实际上您只需要蓝色 div 上的 float: right;,但它在媒体查询中效果更好,这样如果您使用 992px 以上的其他格式,div 将保持正确的顺序:

html:

<div class='row'>   
    <div class="col-xs-12 col-sm-6 col-md-4 yellow">1st div</div>
    <div class="col-xs-12 col-sm-6 col-md-4 blue">2nd div</div>
    <div class="col-xs-12 col-sm-6 col-md-4 red">3rd div</div>
</div>

css:

.yellow {
    background-color:yellow;
    height:100px;
}
.blue {
    background-color:blue;
    height:200px;
}
.red {
    background-color:red;
    height:100px;
}

@media (min-width: 768px) and (max-width: 991px) {
    .blue {
        float: right;
    }
}

http://jsfiddle.net/memeLab/M4P6g/1/

【讨论】:

  • 我遇到的这个“问题”可能是最简单的解决方法。
【解决方案2】:

你可以用 Jquery 做到这一点

$(document).ready(function() {
    var x=$('.row');
    $.each(x, function() {
        var max=0;
        $.each($(this).find('div[class^="col-"]'), function() {
            if($(this).height() > max)
                max=$(this).height();
        });
        $.each($(this).find('div[class^="col-"]'), function() {
            $(this).height(max);
        });
    });
});

【讨论】:

  • 永远不要为此使用 jQuery。 CSS就够了