css - 在 Bootstrap3 行中垂直居中内容?

标签 css twitter-bootstrap twitter-bootstrap-3 row vertical-alignment

我有 Bootstrap3 页面,中间某处有一行显示高度。如何使行的内容垂直居中?

我已经尝试过“vertical-align:center”css 属性,但这似乎不起作用('middle' 而不是 'center' 也不起作用)。

这是一个小 live demo ,或作为独立的 HTML 示例:

<!DOCTYPE html><html><head><meta charset='utf-8'>

<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css'>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css'>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js'></script>

<style type='text/css'>
    .row { color:#ff0; }
    .bg1 { background:#007; }
    .bg2 { background:#060; }
    .special { background:#600; height:100vh; vertical-align:center; }
</style>

</head><body>

<div class="container-fluid text-center">

    <div class="row bg1">Top row<br><br></div>

    <div class="row bg2">Another row<br>With some content<br><br></div>

    <div class="row bg1">....Bla bla bla....<br>sadf asdf adsf asdf asdf<br><br></div>

    <div class="row special">
        <h2>Hello</h2>
        This should be vertically centered
    </div>

    <div class="row bg2">And here's yet another row<br><br></div>

    <div class="row bg1">The bottom row<br><br></div>

</div>

</body></html>

最佳答案

.row 的列有负边距,使用没有内部 col-X-X 会给你奇怪的结果。阅读有关如何使用网格系统的文档。

vertical-align:middle有多种方式(非居中)http://css-tricks.com/centering-in-the-unknown/

演示:http://jsfiddle.net/mLopevv8/

自述:为 .row 创建另一个类,删除 .container-fluid,将垂直对齐的中间内容的父级转换为 display:table,子级现在是 display:table-cell。

CSS

.special { background:#600; height:100vh; display:table;width:100%;}
.v-m {display:table-cell;vertical-align:middle}

HTML

<div class="text-center">
   <div class="my-row bg1">Top row<br><br></div>
   <div class="my-row bg2">Another row<br>With some content<br><br></div>
   <div class="my-row bg1">....Bla bla bla....<br>sadf asdf adsf asdf asdf<br><br></div>
   <div class="my-row special">
      <div class="v-m">
         <h1>Hello</h1>
         This should be vertically centered!
      </div>
   </div>
   <div class="my-row bg2">And here's yet another row<br><br></div>
   <div class="my-row bg1">-- The bottom row --<br><br>
   </div>
</div>

关于css - 在 Bootstrap3 行中垂直居中内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26889849/

相关文章:

javascript - 使用 HTML、CSS 和 JavaScript 增加圆弧效果

css - 如何在网站的移动版本中对齐 div 标签?

Jquery UI 可拖动列表项到框

html - Bootstrap 字体很棒的图标在下载的 CSS 中不起作用

CSS 文件仅在某些页面上加载缓慢

html - Bootstrap 表格图像作为边框

html - 使用引用类将 css 应用于目标类

javascript - 验证文本区域输入的字母数字值

HTML/Bootstrap 对齐表格中的变量元素

css - Bootstrap 列忽略内容填充