简体   繁体   English

如何在引导流体布局中底部对齐网格元素

[英]How do I bottom-align grid elements in bootstrap fluid layout

I have a fluid layout using Twitter's bootstrap, wherein I have a row with two columns.我有一个使用 Twitter 引导程序的流畅布局,其中一行有两列。 The first column has a lot of content, which I want to fill the span normally.第一列内容很多,我想正常填充span。 The second column just has a button and some text, which I want to bottom align relative to the cell in the first column.第二列只有一个按钮和一些文本,我想相对于第一列中的单元格底部对齐。

Here's what I have:这是我所拥有的:

-row-fluid-------------------------------------
+-span6----------+ +-span6----------+
|                | |short content   |
| content        | +----------------+
| that           | 
| is tall        |    
|                |
+----------------+
-----------------------------------------------

Here's what I want:这就是我想要的:

-row-fluid-------------------------------------
+-span6----------+
|                |
| content        |
| that           | 
| is tall        | +-span6----------+    
|                | |short content   |
+----------------+ +----------------+
-----------------------------------------------

I've seen solutions that make the first span an absolute height, and position the second span relative to it, but a solution where I didn't have to specify the absolute height of my divs would be preferred.我见过使第一个跨度成为绝对高度,并将第二个跨度相对于它定位的解决方案,但是我不必指定我的 div 的绝对高度的解决方案将是首选。 I'm also open to a complete rethink of how to achieve the same effect.我也愿意彻底重新思考如何实现同样的效果。 I'm not married to this use of the scaffolding, it just seemed to make the most sense to me.我不喜欢这种脚手架的使用,它对我来说似乎最有意义。

This layout as a fiddle:这种布局就像一个小提琴:

http://jsfiddle.net/ryansturmer/A7buv/3/ http://jsfiddle.net/ryansturmer/A7buv/3/

This is an updated solution for Bootstrap 3 (should work for older versions though) that uses CSS/LESS only:这是 Bootstrap 3 的更新解决方案(虽然应该适用于旧版本),仅使用 CSS/LESS:

http://jsfiddle.net/silb3r/0srp42pb/11/ http://jsfiddle.net/silb3r/0srp42pb/11/

You set the font-size to 0 on the row (otherwise you'll end up with a pesky space between columns), then remove the column floats, set display to inline-block , re-set their font-size, and then vertical-align can be set to anything you need.您将行上的字体大小设置为 0(否则最终会在列之间出现讨厌的空间),然后删除列浮动,将显示设置为inline-block ,重新设置它们的字体大小,然后vertical-align可以设置为任何你需要的。

No jQuery required.不需要 jQuery。

Please note: for Bootstrap 4+ users, please consider Christophe's solution (Bootstrap 4 introduced flexbox, which provides for a more elegant CSS-only solution).请注意:对于 Bootstrap 4+ 用户,请考虑Christophe 的解决方案(Bootstrap 4 引入了 flexbox,它提供了更优雅的纯 CSS 解决方案)。 The following will work for earlier versions of Bootstrap...以下将适用于早期版本的 Bootstrap ...


See http://jsfiddle.net/jhfrench/bAHfj/ for a working solution.有关工作解决方案,请参阅http://jsfiddle.net/jhfrench/bAHfj/

//for each element that is classed as 'pull-down', set its margin-top to the difference between its own height and the height of its parent
$('.pull-down').each(function() {
  var $this = $(this);
  $this.css('margin-top', $this.parent().height() - $this.height())
});

On the plus side:从积极的一面:

  • in the spirit of Bootstrap's existing helper classes , I named the class pull-down .本着Bootstrap 现有帮助类的精神,我将该类命名为pull-down
  • only the element that is getting "pulled down" needs to be classed, so...只有被“拉下”的元素需要分类,所以......
  • ...it's reusable for different element types (div, span, section, p, etc) ...它可重复用于不同的元素类型(div、span、section、p 等)
  • it's fairly-well supported (all the major browsers support margin-top)它得到了很好的支持(所有主要浏览器都支持 margin-top)

Now the bad news:现在是坏消息:

  • it requires jQuery它需要 jQuery
  • it's not, as-written, responsive (sorry)它不是,如所写的那样,响应(抱歉)

You can use flex:你可以使用弹性:

@media (min-width: 768px) {
  .row-fluid {
    display: flex;
    align-items: flex-end;
  }
}

You need to add some style for span6 , smthg like that:您需要为span6添加一些样式,像这样:

.row-fluid .span6 {
  display: table-cell;
  vertical-align: bottom;
  float: none;
}

and this is your fiddle: http://jsfiddle.net/sgB3T/这是你的小提琴:http: //jsfiddle.net/sgB3T/

Here's also an angularjs directive to implement this functionality这里还有一个 angularjs 指令来实现这个功能

    pullDown: function() {
      return {
        restrict: 'A',
        link: function ($scope, iElement, iAttrs) {
          var $parent = iElement.parent();
          var $parentHeight = $parent.height();
          var height = iElement.height();

          iElement.css('margin-top', $parentHeight - height);
        }
      };
    }

Just set the parent to display:flex;只需将父级设置为display:flex; and the child to margin-top:auto .和孩子margin-top:auto This will place the child content at the bottom of the parent element, assuming the parent element has a height greater than the child element.这会将子内容放在父元素的底部,假设父元素的高度大于子元素。

There is no need to try and calculate a value for margin-top when you have a height on your parent element or another element greater than your child element of interest within your parent element.当父元素或其他元素的高度大于父元素中感兴趣的子元素时,无需尝试计算margin-top的值。

This is based on cfx's solution, but rather than setting the font size to zero in the parent container to remove the inter-column spaces added because of the display: inline-block and having to reset them, I simply added这是基于 cfx 的解决方案,而不是在父容器中将字体大小设置为零以删除由于显示而添加的列间空格:inline-block 并且必须重置它们,我只是添加了

 .row.row-align-bottom > div { float: none; display: inline-block; vertical-align: bottom; margin-right: -0.25em; }

to the column divs to compensate.到列 div 进行补偿。

Based on the other answers here is an even more responsive version.基于这里的其他答案是一个响应速度更快的版本。 I made changes from Ivan's version to support viewports <768px wide and to better support slow window resizes.我对 Ivan 的版本进行了更改,以支持 <768px 宽的视口并更好地支持缓慢的窗口调整大小。

!function ($) { //ensure $ always references jQuery
    $(function () { //when dom has finished loading
        //make top text appear aligned to bottom: http://stackoverflow.com/questions/13841387/how-do-i-bottom-align-grid-elements-in-bootstrap-fluid-layout
        function fixHeader() {
            //for each element that is classed as 'pull-down'
            //reset margin-top for all pull down items
            $('.pull-down').each(function () {
                $(this).css('margin-top', 0);
            });

            //set its margin-top to the difference between its own height and the height of its parent
            $('.pull-down').each(function () {
                if ($(window).innerWidth() >= 768) {
                    $(this).css('margin-top', $(this).parent().height() - $(this).height());
                }
            });
        }

        $(window).resize(function () {
            fixHeader();
        });

        fixHeader();
    });
}(window.jQuery);

Well, I didn't like any of those answers, my solution of the same problem was to add this: <div>&nbsp;</div> .好吧,我不喜欢这些答案中的任何一个,我对同一问题的解决方案是添加: <div>&nbsp;</div> So in your scheme it would look like this (more or less), no style changes were necessary in my case:所以在你的方案中它看起来像这样(或多或少),在我的情况下没有必要改变样式:

-row-fluid-------------------------------------
+-span6----------+ +----span6----------+
|                | | +---div---+       |
| content        | | | & nbsp; |       |
| that           | | +---------+       |
| is tall        | | +-----div--------+|   
|                | | |short content   ||
|                | | +----------------+|
+----------------+ +-------------------+
-----------------------------------------------
.align-bottom {
    position: absolute;
    bottom: 10px;
    right: 10px;
}

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

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