赞
踩
目录
水平垂直居中是一个非常基础但又是非常重要的应用场景,实现居中的方法存在很多,可以将这些方法分成两个大类:
实现元素水平垂直居中有以下七种方法:
display:table-cell
,子元素设置 display: inline-block
。利用vertical
和text-align
可以让所有的行内块级元素水平垂直居中。- <div class="father">
- <div class="son"></div>
- </div>
- <style>
- .father{
- width:500px;
- height:300px;
- border:1px solid #0a3b98;
- position: relative; //父级设置为相对定位
- }
- .son{
- width:100px;
- height:40px;
- background: #f0a238;
- position: absolute; //设置为子级绝对定位
- top:0;
- left:0;
- right:0;
- bottom:0;
- margin:auto;
- }
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
父级设置为相对定位,子级绝对定位 ,并且四个定位属性的值都设置了0,那么这时候如果子级没有设置宽高,则会被拉开到和父级一样宽高。这里子元素设置了宽高,所以宽高会按照我们的设置来显示,但是实际上子级的虚拟占位已经撑满了整个父级,这时候再给它一个margin:auto
它就可以上下左右都居中了。
绝大多数情况下,设置父元素为相对定位, 子元素移动自身50%实现水平垂直居中
- <div class="father">
- <div class="son"></div>
- </div>
- <style>
- .father{
- width: 500px;
- height: 300px;
- background-color: pink;
- position: relative;
- }
- .son{
- width: 100px;
- height: 100px;
- background-color: skyblue;
- position: absolute;
- left: 50%;
- top: 50%;
- margin-left: -50px;
- margin-top: -50px;
- }
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
这种方案不要求父元素的高度,也就是即使父元素的高度变化了,仍然可以保持在父元素的垂直居中位置,水平方向上是一样的操作。但是该方案需要知道子元素自身的宽高,但是我们可以通过下面transform
属性进行移动。
- <div class="father">
- <div class="son"></div>
- </div>
-
- <style>
- .father{
- width: 500px;
- height: 300px;
- background-color: pink;
- position: relative;
- }
- .son{
- width: 100px;
- height: 100px;
- background-color: skyblue;
- position: absolute;
- left: 50%;
- top: 50%;
- transform: translateX(-50px) translateY(-50px);
- }
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
这种方法其实和最上面被否定掉的margin负值用法一样,可以说是margin
负值的替代方案,并不需要知道自身元素的宽高。
- <div class="father">
- <div class="son"></div>
- </div>
-
- <style>
- .father{
- width: 500px;
- height: 300px;
- background-color: pink;
- text-align: center;
- }
- .father::before{
- content : '';
- display: inline-block;
- vertical-align: middle;
- height: 100%;
- }
- .son{
- width: 100px;
- height: 100px;
- background-color: skyblue;
- vertical-align: middle;
- margin: 0 auto;
- display: inline-block;
- }
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- <div class="father">
- <div class="son"></div>
- </div>
-
- <style>
- .father{
- width: 500px;
- height: 300px;
- background-color: pink;
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .son{
- width: 100px;
- height: 100px;
- background-color: skyblue;
- }
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
css3
中了flex
布局,可以非常简单实现垂直水平居中
这里可以简单看看flex
布局的关键属性作用:
display: flex时,表示该容器内部的元素将按照flex进行布局
align-items: center表示这些元素将相对于本容器水平居中
justify-content: center也是同样的道理垂直居中
设置父元素为display:table-cell
,子元素设置 display: inline-block
。利用vertical
和text-align
可以让所有的行内块级元素水平垂直居中。
- <div class="father">
- <div class="son"></div>
- </div>
-
- <style>
- .father {
- display: table-cell;
- width: 200px;
- height: 200px;
- background: skyblue;
- vertical-align: middle;
- text-align: center;
- }
- .son {
- display: inline-block;
- width: 100px;
- height: 100px;
- background: red;
- }
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
-
- <div class="father">
- <div class="son"></div>
- </div>
-
- <style>
- .father{
- width: 500px;
- height: 300px;
- background-color: pink;
- display: grid;
- justify-content: center;
- align-items: center;
- }
- .son{
- width: 100px;
- height: 100px;
- background-color: skyblue;
- }
- </style>
-
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
上述方法中,不知道元素宽高大小仍能实现水平垂直居中的方法有:
根据元素标签的性质,可以分为:
水平居中
垂直居中
水平居中
垂直居中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。