CSS实现任意的单行(多行)垂直居中
CSS实现垂直居中有很多方法, 各种方法也有各自的使用场景, 我们可以从 6 Methods For Vertical Centering With CSS 中了解一二.
以下就是我学习之后对其的改进, 通过absolute和margin来实现任意的单行(多行)垂直居中
这种方式实现的垂直居中灵活在哪里?
* 不需要知道待垂直居中的容器高度(意味着不需要写死任何高度数值)
* 支持多行垂直居中
* 可做为工具样式来使用, 例如这里的: .vcenter, .vcenter-child
CSS
--------------------
.vcenter {
position: relative;
height: 100%;
}
.vcenter-child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
margin: auto;
display: table;
text-align: center;
}
HTML
--------------------
<!-- 我们需要垂直居中的容器 -->
<div class="container">
<div class="vcenter">
<div class="vcenter-child">vertical-centering, 我要垂直居中</div>
</div>
</div>
想了解更多
--------------------
css-vertical-centering
以下就是我学习之后对其的改进, 通过absolute和margin来实现任意的单行(多行)垂直居中
这种方式实现的垂直居中灵活在哪里?
* 不需要知道待垂直居中的容器高度(意味着不需要写死任何高度数值)
* 支持多行垂直居中
* 可做为工具样式来使用, 例如这里的: .vcenter, .vcenter-child
CSS
--------------------
.vcenter {
position: relative;
height: 100%;
}
.vcenter-child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
margin: auto;
display: table;
text-align: center;
}
HTML
--------------------
<!-- 我们需要垂直居中的容器 -->
<div class="container">
<div class="vcenter">
<div class="vcenter-child">vertical-centering, 我要垂直居中</div>
</div>
</div>
想了解更多
--------------------
css-vertical-centering
热门话题 · · · · · · ( 去话题广场 )
- 解锁我的夏日旅行足迹地图 活动 28.9万次浏览
- 哪一刻你真正感觉到了自己身体的存在? 5136次浏览
- 我给自己写了一个使用说明书 新话题
- 用对话体写日记 1746次浏览
- 我是怎么拼命点亮人生至暗时刻的 1.4万次浏览
- 晒出你的上影节最爱 新话题