重要的! 垂直中心相对于height父母的
如果您尝试居中的元素的父元素没有定义height, none的垂直居中解决方案将会起作用!
现在,垂直居中...
Bootstrap 5(2021 年更新)
Bootstrap 5 仍然flexbox基于所以垂直居中的工作方式与 Bootstrap 4 相同。例如,align-items-center
, justify-content-center
或可以在弹性盒父级上使用自动边距(row
or d-flex
).
- use
align-items-center
在 Flexbox 行父级上(row
or d-flex
)
- use
justify-content-center
在 Flexbox 列父级上(d-flex flex-column
)
- use
my-auto
在 Flexbox 父级上
Bootstrap 5 中的垂直居中 https://codeply.com/p/0VM5MJ7Had
引导程序4
您可以使用新的Flexbox 和尺寸实用程序 http://v4-alpha.getbootstrap.com/utilities/flexbox/使container
全高和display: flex
。这些选项不需要额外的 CSS(除了容器的高度(即:html、body)必须为 100%).
选项1align-self-center
在弹性盒孩子上
<div class="container d-flex h-100">
<div class="row justify-content-center align-self-center">
I'm vertically centered
</div>
</div>
https://codeply.com/go/fFqaDe5Oey https://codeply.com/go/fFqaDe5Oey
选项2align-items-center
在弹性盒父级上 (.row
is display:flex; flex-direction:row
)
<div class="container h-100">
<div class="row align-items-center h-100">
<div class="col-6 mx-auto">
<div class="jumbotron">
I'm vertically centered
</div>
</div>
</div>
</div>
https://codeply.com/go/BumdFnmLuk https://codeply.com/go/BumdFnmLuk
选项3justify-content-center
在弹性盒父级上 (.card
is display:flex;flex-direction:column
)
<div class="container h-100">
<div class="row align-items-center h-100">
<div class="col-6 mx-auto">
<div class="card h-100 border-primary justify-content-center">
<div>
...card content...
</div>
</div>
</div>
</div>
</div>
https://codeply.com/go/3gySSEe7nd https://codeply.com/go/3gySSEe7nd
有关 Bootstrap 4 垂直居中的更多信息
现在 Bootstrap 4 提供了 flexbox 和其他公用事业 http://v4-alpha.getbootstrap.com/utilities,垂直方向有很多方法
结盟。http://www.codeply.com/go/WG15ZWC4lf http://www.codeply.com/go/WG15ZWC4lf
1 - 使用自动边距的垂直居中:
另一种垂直居中的方法是使用my-auto
。这将使元素在其容器内居中。例如,h-100
使行全高,并且my-auto
将垂直居中col-sm-12
column.
<div class="row h-100">
<div class="col-sm-12 my-auto">
<div class="card card-block w-25">Card</div>
</div>
</div>
使用自动边距垂直居中Demo http://www.codeply.com/go/akn5BYxQwn
my-auto
表示垂直 y 轴上的边距,相当于:
margin-top: auto;
margin-bottom: auto;
2 - 带 Flexbox 的垂直居中:
自 Bootstrap 4 起.row
is now display:flex
你可以简单地使用align-self-center
在任何列上将其垂直居中...
<div class="row">
<div class="col-6 align-self-center">
<div class="card card-block">
Center
</div>
</div>
<div class="col-6">
<div class="card card-inverse card-danger">
Taller
</div>
</div>
</div>
or, use align-items-center
整体上.row
垂直居中对齐所有col-*
在行...
<div class="row align-items-center">
<div class="col-6">
<div class="card card-block">
Center
</div>
</div>
<div class="col-6">
<div class="card card-inverse card-danger">
Taller
</div>
</div>
</div>
垂直中心异高柱Demo http://www.codeply.com/go/GFgGl4YS27
请将此 Q/A 置于中心,但保持相同的高度 https://stackoverflow.com/questions/52913611/align-items-center-while-keeping-the-background-full-height
3 - 使用显示实用程序垂直居中:
Bootstrap 4 有显示工具 https://v4-alpha.getbootstrap.com/utilities/display-property/可以用于display:table
, display:table-cell
, display:inline
等。这些可以与垂直对齐实用程序 https://v4-alpha.getbootstrap.com/utilities/vertical-align/对齐内联、内联块或表格单元格元素。
<div class="row h-50">
<div class="col-sm-12 h-100 d-table">
<div class="card card-block d-table-cell align-middle">
I am centered vertically
</div>
</div>
</div>
使用显示实用程序垂直居中Demo http://www.codeply.com/go/30DYTbq83o
更多示例
垂直居中图像<div> https://www.codeply.com/go/Niu8NCcdqj
.container 中的垂直中心 .row https://www.codeply.com/go/7GgwkQhiro
垂直中心和底部<div> https://stackoverflow.com/questions/49278082/bootstrap-4-text-vertical-align-in-the-center-and-the-end-of-div
父级内部垂直居中的子级 https://www.codeply.com/go/6V2zpJSbQO
垂直居中全屏巨幕机 https://www.codeply.com/go/9HWnyQm2Sf
重要的! 我有提到身高吗?
请记住,垂直居中是相对于父母的身高元素。如果你想在整个页面上居中,在大多数情况下,这应该是你的CSS......
body,html {
height: 100%;
}
Or use min-height: 100vh
(min-vh-100
在 Bootstrap 4.1+ 中)在父级/容器上。如果要将子元素置于父元素的中心。父级必须有一个定义的height.
另请参阅:
Bootstrap 4 中的垂直对齐 https://stackoverflow.com/questions/43313090/vertical-alignment-in-bootstrap-4
Bootstrap 中心垂直和水平对齐 https://stackoverflow.com/questions/42388989/bootstrap-4-center-vertical-and-horizontal-alignment