【发布时间】: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