简体   繁体   English

Bootstrap 3 - 行内的底部对齐列

[英]Bootstrap 3 - Bottom align column within row

I have two Bootstrap columns within a row, thusly: 我连续有两个Bootstrap列,因此:

<div class="row">
    <div class="col-xs-6 mainBox">
        <h1>Heading</h1>
        <h2>Sub Heading</h2>
    </div>
    <div class="col-xs-6 mainBox buttonBox">
        <button class="btn btn-primary">Button</button>
    </div>
</div>

I want the second column to be bottom aligned vertically within the row. 我希望第二列在行内垂直对齐。 How do I achieve this? 我该如何实现这一目标?

Here is a demo fiddle: 这是一个演示小提琴:
http://jsfiddle.net/RationalGeek/6pYhx/ http://jsfiddle.net/RationalGeek/6pYhx/

Try using position: absolute; 尝试使用position: absolute; and setting a bottom of 0 : 并设置0bottom

.row {
    position: relative;
}
.mainBox {
    border: solid thin black;
}    
.buttonBox {
    position: absolute;
    bottom:0;
    right:0;
}

http://jsfiddle.net/6pYhx/3/ http://jsfiddle.net/6pYhx/3/

Although absolute position is a quick fix, a more robust solution that works with many columns would be much better. 虽然绝对位置是一个快速解决方案,但是一个更强大的解决方案适用于许多列会更好。

Here is my solution in this updated fiddle . 这是我在这个更新的小提琴中的解决方案。

[class*='cols-'].row > *{
  float: none;
  position: relative;
  display: inline-block; 
  /* old ie fixes */
  *zoom: 1; 
  *display: inline;
}

.row.cols-bottom > *{
  vertical-align: bottom;
}

.row.cols-middle > *{
  vertical-align: middle;
}

and html: 和HTML:

<div class="row cols-bottom">
    <div class="col-xs-4">
        <h3>Heading</h3>
        <h4>Sub Heading</h4>
    <!-- The lack of space between div tags below does MATTER -->
    </div><div class="col-xs-4">
        <button class="btn btn-primary">Button </button>
    <!-- The lack of space between div tags below does MATTER -->
    </div><div class='col-xs-4'>
        This col should be 3
    </div>
</div>

There are a couple of things to note in my solution. 我的解决方案中有几点需要注意。 The inline-block strategy used allows positioning of the div columns to stay in the flow of the document, while also allowing the use of vertical-align styling. 使用的内联块策略允许将div列定位以保留在文档的流中,同时还允许使用垂直对齐样式。 I've included a style for bottom alignment and middle alignment for you convenience (I tend to use the middle alignment just as often). 我已经为你提供了底部对齐和中间对齐的样式(我倾向于经常使用中间对齐)。

The second thing to note is you must have each of the respective ending-column </div> and starting-column <div> meet with no space in between. 要注意的第二件事是你必须让每个相应的结束列</div>和起始列<div>在两者之间没有空格。 This is because inline-block gives 'space' to any character (including a space character). 这是因为内联块为任何字符(包括空格字符)提供“空间”。 Essentially, it is because a space character has a given font-size, which ends up pushing the respective right most columns underneath the left most columns. 本质上,这是因为空格字符具有给定的字体大小,最终会推动最左侧列下方的相应最右侧列。 There are hacks to overcome this, but they are note cross-browser compatible, so I did not include them. 有黑客要克服这一点,但他们注意到跨浏览器兼容,所以我没有包括他们。 Therefore, my solution is hack-free, and works with multiple columns. 因此,我的解决方案是无黑客的,并且可以使用多个列。 Enjoy! 请享用!

Building on Mike's solution, here's one that does not care about spaces between divs. 基于迈克的解决方案,这里有一个不关心div之间空间的解决方案。 Instead of inline it uses display:table and display:cell. 它不使用内联,而是使用display:table和display:cell。

[class*='cols-'] {
   display:table;
}
[class*='cols-'] > * {
    float: none;
    position: relative;
    /* old ie fixes */
    *zoom: 1; 
    *display: inline;
    display: table-cell;
}
.cols-bottom > * {
        vertical-align: bottom;
}
.cols-top > * {
        vertical-align: top;
}

//extra bonus if needed
.p-top {
    vertical-align: top;
}

plunker updated: http://jsfiddle.net/6pYhx/325/ plunker更新: http//jsfiddle.net/6pYhx/325/

I know I'm posting on a very old thread, but I've been searching for solutions to the same issue. 我知道我在一个非常老的帖子上发帖,但我一直在寻找同样问题的解决方案。 I think a simple solution may be to use display: flex; 认为一个简单的解决方案可能是使用display: flex; on the row, and margin-top: auto; 在行上, margin-top: auto; on the column you want pushed down. 在你要推下的列上。 Here's a demo fiddle based on the OP. 这是一个基于OP的演示小提琴

*Please note* - this is altering the row class which could have unintended side effects. *请注意* - 这会改变可能产生意外副作用的row类。 You may want to implement it in a new class and only apply where needed. 您可能希望在新类中实现它,并仅在需要时应用。

Building on solutions from Mike and JPH, this allows for col-12 behavior to be retained. 基于Mike和JPH的解决方案,这允许保留col-12行为。 It isn't perfect and a bit verbose in plain CSS, but works. 它在纯CSS中并不完美,有点冗长,但有效。

/* Vertical Align Columns */
[class*='cols-'] {
   display:table;
}
.cols-bottom > * {
    vertical-align: bottom;
}
.cols-middle > * {
    vertical-align: middle;
}
.cols-top > * {
    vertical-align: top;
}

/* Resets for col-12 classes */
@media (min-width: 1200px) {
    [class*='cols-'] > *:not(.col-lg-12) {
        float: none;
        position: relative;
        /* old ie fixes */
        *zoom: 1; 
        *display: inline;
        display: table-cell;
    }
}
@media (min-width: 992px) and (max-width: 1199px) {
    [class*='cols-'] > *:not(.col-lg-12):not(.col-md-12) {
        float: none;
        position: relative;
        /* old ie fixes */
        *zoom: 1; 
        *display: inline;
        display: table-cell;
    }
}
@media (min-width: 768px) and (max-width: 991px) {
    [class*='cols-'] > *:not(.col-lg-12):not(.col-md-12):not(.col-sm-12) {
        float: none;
        position: relative;
        /* old ie fixes */
        *zoom: 1; 
        *display: inline;
        display: table-cell;
    }
}
@media (max-width: 767px) {
    [class*='cols-'] > *:not(.col-lg-12):not(.col-md-12):not(.col-sm-12):not(.col-xs-12) {
        float: none;
        position: relative;
        /* old ie fixes */
        *zoom: 1; 
        *display: inline;
        display: table-cell;
    }
}

Here is another working example 这是另一个工作示例
http://jsfiddle.net/RationalGeek/6pYhx/ http://jsfiddle.net/RationalGeek/6pYhx/

<div class="row row-eq-height"></div>


The Css: Css:

.row-eq-height {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display:         flex;
}
.mainBox {
   border: solid thin black;
}

.buttonBox {
    vertical-align:center;
    margin-top: auto;
   margin-bottom: auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM