【问题标题】:How to align vertically an element to the bottom within a bootstrap column?如何在引导列中将元素垂直对齐到底部?
【发布时间】:2014-06-25 09:41:12
【问题描述】:

我正在使用 twitter-bootstrap 框架和bootstrap touchspin plugin。我想在具有可变高度的列.col-md-6 的底部对齐触摸旋转。

我尝试添加以下内容(如this solution):

.row {
    position: relative;
}

.bootstrap-touchspin {
    position: absolute;
    bottom:0;
    right:0;
}

但没有任何改变。 (see jsfiddle)

为什么它不起作用?你有其他方法吗?

这是一个 jsfiddle,您可以尝试:http://jsfiddle.net/R5n9j/1/


实际输出

期望的输出


HTML

<div class="container">
    <div class="row">
        <div class="col-md-7">
            <div id="form-guardar-medidas">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#tab1" id="add-medida-medir">Tab1</a>

                    </li>
                    <li><a href="#tab2">Tab2</a>

                    </li>
                </ul>
                <div id='content' class="tab-content">
                    <div class="tab-pane active fade in" id="tab1">
                        <div class="row">
                            <div class="col-md-6">
                                <img class="img-responsive" src="http://placehold.it/200x200">
                            </div>
                            <div class="col-md-6">
                                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam blandit mattis pretium. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p>
                                <input id="input-cm" type="text" class="input-lg" name="input-cm">
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="tab2">Hello tab2</div>
                </div>
            </div>
        </div>
    </div>
</div>

CSS

body{
    padding: 20px;
}

.tab-content {
    border: 1px solid #ccc;
    padding: 15px;
}

JS

$(document).ready(function () {
    $("input[name='input-cm']").TouchSpin();

    $('.nav-tabs a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    });

});

【问题讨论】:

    标签: html css twitter-bootstrap vertical-alignment


    【解决方案1】:

    我的解决办法是:

    我已经为每一列添加了col1col2 ids,并用js设置了相同的高度。

    col1h = $("#col1").height();
    col2h = $("#col2").height();
    
    if (col1h > col2h) {
        def_height = col1h;
    } else {
        def_height = col2h;
    }
    
    $("#col1,#col2").height(def_height);
    

    并将left: 15px; 添加到.bootstrap-touchspin

    结果http://jsfiddle.net/R5n9j/16/embedded/result

    【讨论】:

    • 第二列比第一列高的情况怎么办?为什么......需要10px? (应该是 15)在父级 .col-md-6 上有一个 15px 的左填充,如 DevTools/Firebug 所示(这是排水沟的一半)
    • 谢谢! :) 看看新的解决方案。
    【解决方案2】:

    通过切换到 CSS 表格布局,您可以免费拥有相同高度的列,但不能相对定位“(视觉)单元格”(至少在 Firefox 中),因此您必须在'(视觉)表格"

    EDIT2:.bootstrap-touchspin 也显示为表格,但在 Chrome 中失败。在这个组件周围添加一个绝对定位的 div(而不是组件本身)解决了这个问题。

    演示:http://jsfiddle.net/R5n9j/9/
    兼容性为 IE8+(由于/感谢 display: tabletable-cell

    HTML

    <div class="table pos-reference">
        <div class="cell">
            <img class="img-responsive" src="http://placehold.it/200x200">
        </div>
        <div class="cell has-touchspin">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam blandit mattis pretium. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p>
            <div class="pos-abs">
                <input id="input-cm" type="text" class="input-lg" name="input-cm">
            </div>
        </div>
    </div>
    

    CSS

    .table {
        display: table;
        table-layout: fixed;
    }
    .pos-reference {
        position: relative;
    }
    .cell {
        display: table-cell;
        vertical-align: top;
    }
    .has-touchspin {
        height: 100%;
        padding-bottom: 50px; /* Avoids superposition of content and absolutely positioned touchspin */
    }
    .pos-abs {
        position: absolute;
        bottom: 0;
        border: 1px solid #F00;
    }
    

    编辑:解释为什么您尝试定位 .input-group 失败:TWBS .col-AZ-NN 已经相对定位,因此定位的最接近 .input-group 的祖先不是您设置的,而是 .col-md-6。请参阅下面的 Firebug 屏幕截图。否则,你的尝试是一个很好的尝试;)

    【讨论】:

    • 感谢您的回答,但(至少)在 chrome 中,标签内容的填充底部不被尊重(比其他边大),并且 touchspin 控件从标签内容中突出向右。
    • +1。这也适用于野生动物园,绝对是一种更好的方法。
    • 啊,非常抱歉。尚未在 Chrome 中进行测试。 touchspin 也使用display: table,在这种情况下,Firefox 和 Chrome 之间的宽度计算似乎非常不同。通过将 touchspin 封装在绝对定位的元素中(而不是组件本身),可以解决问题。在 IE8 中似乎也可以
    • 嘿,如果我删除 margin-bottom: 20px; 会返回我正在寻找的结果。谢谢。 :)
    【解决方案3】:

    最简单的方法是使用flex:

    .tab-pane .row{
        display:flex;
    }
    

    如果您注意到选项卡内的两个 col-md-6 容器的高度不相等,则使用 flex 时会将相等的高度应用于其子容器。

    DEMO

    限制:

    Safari 浏览器支持flex 属性。

    【讨论】:

    • 谢谢,但很遗憾。恐怕我需要一个好的浏览器支持。
    • @JohnDoe 环顾四周,似乎display: -webkit-box; 可以作为弹性替代品。你可以在你的 safari 浏览器中测试这个link,但同样,flex 不是widely supported
    • 嘿,你给我个主意,看这个:jsfiddle.net/R5n9j/12/embedded/result
    • @JohnDoe 啊,我看到您使用了 javascript 方法。效果也很好。
    • 更明确地说,IE8 和 IE9 都不支持 flex。根据 StatCounter 的数据,这是我国市场份额的 6%(尽管由于 MS 不再正式支持 XP,IE8 可能会下降)
    猜你喜欢
    • 1970-01-01
    • 2014-03-31
    • 2014-10-27
    • 1970-01-01
    • 2016-07-13
    • 2021-11-03
    • 1970-01-01
    • 2011-09-01
    • 2013-03-23
    相关资源
    最近更新 更多