Twitter引导响应块高度

时间:2014-07-14 21:05:18

标签: jquery css twitter-bootstrap twitter-bootstrap-3

在我的Twitter Bootstrap 3.2应用程序中,我有div块具有重叠宽度。

但如果我删除" row"类,由于内容长度的变化,块高度变得不同,块看起来很奇怪。检查此宽度是否大于992像素:http://jsfiddle.net/mavent/bFcrq/13/

如果我使用" row"我的页面看起来不错:http://jsfiddle.net/mavent/bFcrq/15/ 但我不知道何时使用行类。因为屏幕尺寸不同,所以我不知道我是否可以使用每2个div或3个div的行类。

所以我的问题是,如何让这些积木具有相同的高度,即使小而大的屏幕都有漂亮的外观?

<div class="text-center"> <div class="visible-xs visible-sm">Small screen is ok, There is problem for big screens </div> <div class="well col-lg-4 col-md-4 col-sm-12 col-xs-12"> <a href=""><h2>My pretty title 1</h2><p>My subtitle which can have different number of words</p></a> </div>

编辑:同时在大屏幕中检查此行为:http://jsfiddle.net/bFcrq/26/
即使我使用&#34; row&#34;上课时,有些街区很长,有些则很短 当我使用&#34; alert - *&#34;它看起来非常糟糕。类。

Edit2:虽然我更喜欢CSS解决方案,但基于JQuery的解决方案也适合我。

2 个答案:

答案 0 :(得分:7)

我认为这是没有javascript的最佳选择:http://jsfiddle.net/V2F9E/6/

<div class="container">
    <div class="row">
        <div class="col-lg-4 col-md-6 col-xs-12">
            <h1>1</h1>
            <p>Sub</p>
        </div>
        <div class="col-lg-4 col-md-6 col-xs-12">
            <h1>2</h1>
            <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
        </div>

        <div class="clearfix visible-md-block "></div><!--This clearfix has to be set every 2 div because that's when col-md break to a new line-->

        <div class="col-lg-4  col-md-6 col-xs-12">
            <h1>3</h1>
            <p>Sub</p>
        </div>

        <div class="clearfix visible-lg-block "></div><!--This clearfix has to be set every 3 div because that's that col-lg break to a new line-->

        <div class="col-lg-4  col-md-6 col-xs-12">
            <h1>4</h1>
            <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
        </div>

        <div class="clearfix visible-md-block "></div><!--This clearfix has to be set every 2 div because that's when col-md break to a new line-->

        <div class="col-lg-4  col-md-6 col-xs-12">
            <h1>5</h1>
            <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
        </div>
        <div class="col-lg-4  col-md-6 col-xs-12">
            <h1>6</h1>
            <p>Sub</p>
        </div>

        <div class="clearfix visible-md-block "></div><!--This clearfix has to be set every 2 div because that's when col-md break to a new line-->

         <div class="clearfix visible-lg-block "></div><!--This clearfix has to be set every 3 div because that's that col-lg break to a new line-->

    </div>
</div>

请注意:

<div class="clearfix visible-lg-block">
</div>.

有关网格重置的更多信息:http://getbootstrap.com/css/#grid-responsive-resets

修改

您还可以查看flexbox。也许这会帮助你,它将bootstrap与flexbox结合起来。 http://www.bootply.com/zoupRVbLG4

答案 1 :(得分:3)

我创建了这个基于javascript的解决方案 小提琴:http://jsfiddle.net/mavent/zobhpkvz/7/

// html

<div class="row modify_parent">
    <div class="col-sm-4 alert alert-success modify_child">Some thing happens here..
        <br>Some thing happens here..
        <br>Some thing happens here..
        <br>Some thing happens here..
        <br>Some thing happens here..
        <br>
    </div>
    <div class="col-sm-offset-2 col-sm-4 alert alert-success modify_child">Other thing happens here..
        <br>
    </div>
</div>

// javascript

var sameHeightTop = $(".modify_parent");
if (sameHeightTop.length !== 0) {
    sameHeightTop.each(function () {
        var tallest = 0;
        var sameHeightChildren = $(this).find(".modify_child");
        sameHeightChildren.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        sameHeightChildren.height(tallest);
    });
}