【问题标题】:Vertical alignment of column rows in Bootstrap gridBootstrap网格中列行的垂直对齐
【发布时间】:2016-12-31 06:17:30
【问题描述】:

假设您有一个使用 Twitter Bootstrap 的两列布局,您希望其中的特定行彼此垂直对齐:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css"/>

<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <h2>Column 1</h2>
      <p>Optional content of variable height.</p>
      <p><strong>Align this vertically...</strong></p>
    </div>
    <div class="col-sm-6">
      <h2>Column 2</h2>
      <p><strong>...with this</strong></p>
    </div>
  </div>
</div>

垂直对齐对于表格布局是可行的,但是,Bootstrap 列的大小和响应行为会丢失:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css">

<div class="container">
  <table class="row">
    <thead>
      <tr>
        <th scope="col"><h2>Column 1</h2></th>
        <th scope="col"><h2>Column 2</h2></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><p>Optional content of variable height.</p></td>
      </tr>
      <tr>
        <td><strong>Align this vertically...</strong></td>
        <td><strong>...with this</strong></td>
      </tr>
    </tbody>
  </table>
</div>

另一种选择是拆分行,但是,布局在较小的分辨率上以错误的顺序折叠。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css">

<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <h2>Column 1</h2>
    </div>
    <div class="col-sm-6">
      <h2>Column 2</h2>
    </div>
  </div>
  <div class="row">
    <div class="col-sm-6">
      <p>Optional content of variable height.</p>
    </div>
  </div>
  <div class="row">
    <div class="col-sm-6">
      <strong>Align this vertically...</strong>
    </div>
    <div class="col-sm-6">
      <strong>...with this</strong>
    </div>
  </div>
</div>

如何在保持 Bootstrap 列的行为的同时获得相同的结果?使用表格布局是可行的方式还是死路一条?是否可以不借助 JavaScript 将行定位到计算的偏移量?

编辑:我的目标是让行的顶部与表格布局中的情况一样对齐。

【问题讨论】:

  • Align this vertically... 是可变高度还是固定高度?
  • 你定义了高度吗?也许你可以向我们展示你的 CSS。
  • @MuhammadUsman:要对齐的内容高度可变。
  • @HermLuna:不,我没有。我只使用默认的 Twitter Bootstrap CSS,它包含在 sn-ps 中。
  • @jindrichm 好吧,我假设您使用 bootstrap 3。我认为它没有垂直对齐元素的能力。由于集成了 flexbox,Bootstrap 4 将进行垂直对齐。 Bootstrap 3 更多的是浮动。不幸的是,bootstrap 4 仍处于 alpha 或 beta 阶段。

标签: css twitter-bootstrap vertical-alignment


【解决方案1】:

你可以这样做来获得等高的行:

<style>
 .centered {
   text-align: center;
   font-size: 0;
  }

 .centered .myheight{
   float: none;
   display: inline-block;
   text-align: left;
   font-size: 13px;
   vertical-align: top;
   margin-bottom: 5px; /*add this if you want to give some gap between the rows. */
  }
</style>


<div class="container-fluid">
 <div class="row centered">
    <div class="col-sm-4 myheight">
      Some  content
    </div> 
    <div class="col-sm-4 myheight">
      Some  content ...<br>
      Some more content
    </div> 
    <div class="col-sm-4 myheight">
      Some  content
    </div>  
    <div class="col-sm-4 myheight">
      Some  content
    </div>                                                              
 </div>
</div>

【讨论】:

【解决方案2】:

据我所知,我会测试这些解决方案。

解决方案 1:使用 Javascript(如果需要,可以使用 Jquery)检测左右 div 的高度,并告诉它们具有相同的高度。

您可以将此解决方案应用于第二个内容 div,以使粗体文本上方的空格具有相同的高度。

或者添加..例如在比较两者的高度后,在较小的 div 中根据需要添加 margin-top(带有需要对齐的粗体文本)。

无论如何,如果你有他们两个的身高,你就会有足够的信息来找到方法。这里有多种解决方案,我让您找到适合您的上下文和需求的更好的解决方案。

<div class="container">
  <div class="row">
    <div class="col-sm-6 leftcolumn">
      <h2>Column 1</h2>
      <div class="astallas"><p>Optional content of variable height.</p></div>
      <p><strong>Align this vertically...</strong></p>
    </div>
    <div class="col-sm-6 rightcolumn">
      <h2>Column 2</h2>
      <div class="astallas"></div> // make this empty div have the same height that the left one with variable content
      <p><strong>...with this</strong></p>
    </div>
  </div>
</div>

解决方案 2(但与某些浏览器不兼容):使用 Flexbox http://flexboxfroggy.com/ https://css-tricks.com/snippets/css/a-guide-to-flexbox/

我认为这两种方法都适用于引导程序,并且会尊重响应式需求。

【讨论】:

  • 我不想将行在其容器中垂直居中。相反,我想对齐他们的顶部。您能概括一下 flexbox 解决方案的工作原理吗?
  • 我正在重新考虑我的回复。它现在肯定会回复您的评论。关于 flexbox,你会很快理解访问一些给定的链接。它非常强大,那么它肯定会找到一种方法来使用它
  • 我通过您链接的资源进行了研究(Flexbox Froggy 很棒!),但仍不清楚哪个 flexbox 属性可以帮助我解决我的问题。你能指出我认为有帮助的属性吗?
  • 是的,这些练习非常酷,并且提供了一种很好的学习方式。我会看看,我不经常使用它然后我检查网站,一般都会给出例子。我回来了;)
  • 一个简单的想法:如果我记得你能够制作 2 列具有相同高度,然后告诉其中的 3 个 div 位于顶部、中间和底部。我想就这么简单..?我会检查所需的属性。 codepen.io/team/css-tricks/pen/jqzNZq 这个例子告诉你 Aside1 和 2(你的两列)可以共享一行 > 相同的高度。然后你 align-self: 结束你的粗体文本内容。
猜你喜欢
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2019-02-28
  • 2022-01-10
  • 2016-10-26
  • 1970-01-01
  • 2020-03-11
相关资源
最近更新 更多