在开发中经常遇到这个问题,即让某个元素的内容在水平和垂直方向上都居中,内容不仅限于文字,可能是图片或其他元素
居中是一个非常基础但又是非常重要的应用场景,实现居中的方法存在很多,可以将这些方法分成两个大类:
- 居中元素(子元素)的宽高已知
- 居中元素宽高未知
实现方式
实现元素水平垂直居中的方式:
- 利用定位 + margin:auto
- 利用定位 + margin: 负值
- 利用定位 + transform
- table 布局
- flex 布局
- grid 布局
利用定位+margin:auto
先上代码:
1 <style>
2 .father {
3 margin: 100px auto;
4 width: 500px;
5 height: 300px;
6 border: 1px solid #0a3b98;
7 position: relative;
8 }
9
10 .son {
11 width: 100px;
12 height: 40px;
13 background: #f0a238;
14 position: absolute;
15 top: 0;
16 left: 0;
17 right: 0;
18 bottom: 0;
19 margin: auto;
20 }
21 </style>
22 <div class="father">
23 <div class="son"></div>
24 </div>
父级设置为相对定位,子级绝对定位 ,并且四个定位属性的值都设置了0,那么这时候如果子级没有设置宽高,则会被拉开到和父级一样宽高
这里子元素设置了宽高,所以宽高会按照我们的设置来显示,但是实际上子级的虚拟占位已经撑满了整个父级,这时候再给它一个margin:auto它就可以上下左右都居中了
利用定位+margin:负值
绝大多数情况下,设置父元素为相对定位, 子元素移动自身50%实现水平垂直居中
1 <style>
2 .father {
3 position: relative;
4 margin: 100px auto;
5 width: 200px;
6 height: 200px;
7 background: skyblue;
8 }
9 .son {
10 position: absolute;
11 top: 50%;
12 left: 50%;
13 margin-left:-50px;
14 margin-top:-50px;
15 width: 100px;
16 height: 100px;
17 background: red;
18 }
19 </style>
20 <div class="father">
21 <div class="son"></div>
22 </div>
整个实现思路如下图所示:
- 初始位置为方块1的位置
- 当设置 left、top 为50%的时候,内部子元素为方块2的位置
- 设置 margin 为负数时,使内部子元素到方块3的位置,即中间位置
这种方案不要求父元素的高度,也就是即使父元素的高度变化了,仍然可以保持在父元素的垂直居中位置,水平方向上是一样的操作
但是该方案需要知道子元素自身的宽高,但是我们可以通过下面transform属性进行移动
利用定位+transform
实现代码如下:
1 <style>
2 .father {
3 position: relative;
4 margin: 100px auto;
5 width: 200px;
6 height: 200px;
7 background: skyblue;
8 }
9
10 .son {
11 position: absolute;
12 top: 50%;
13 left: 50%;
14 transform: translate(-50%, -50%);
15 width: 100px;
16 height: 100px;
17 background: red;
18 }
19 </style>
20 <div class="father">
21 <div class="son"></div>
22 </div>
translate(-50%, -50%) 将会将元素位移自己宽度和高度的-50%
这种方法其实和最上面被否定掉的 margin 负值用法一样,可以说是 margin 负值的替代方案,并不需要知道自身元素的宽高
table布局
设置父元素为display:table-cell,子元素设置 display: inline-block。利用vertical和text-align可以让所有的行内块级元素水平垂直居中
1 <style>
2 .father {
3 display: table-cell;
4 width: 200px;
5 height: 200px;
6 background: skyblue;
7 vertical-align: middle;
8 text-align: center;
9 }
10 .son {
11 display: inline-block;
12 width: 100px;
13 height: 100px;
14 background: red;
15 }
16 </style>
17 <div class="father">
18 <div class="son"></div>
19 </div>
flex弹性布局
还是看看实现的整体代码:
1 <style>
2 .father {
3 display: flex;
4 justify-content: center;
5 align-items: center;
6 width: 200px;
7 height: 200px;
8 background: skyblue;
9 }
10 .son {
11 width: 100px;
12 height: 100px;
13 background: red;
14 }
15 </style>
16 <div class="father">
17 <div class="son"></div>
18 </div>
css3 中了 flex 布局,可以非常简单实现垂直水平居中
这里可以简单看看 flex 布局的关键属性作用:
- display: flex 时,表示该容器内部的元素将按照 flex 进行布局
- align-items: center 表示这些元素将相对于本容器水平居中
- justify-content: center 也是同样的道理垂直居中
grid网格布局
1 <style>
2 .father {
3 display: grid;
4 align-items:center;
5 justify-content: center;
6 width: 200px;
7 height: 200px;
8 background: skyblue;
9
10 }
11 .son {
12 width: 10px;
13 height: 10px;
14 border: 1px solid red
15 }
16 </style>
17 <div class="father">
18 <div class="son"></div>
19 </div>
这里看到,
gird网格布局和flex弹性布局都简单粗暴
小结
上述方法中,不知道元素宽高大小仍能实现水平垂直居中的方法有:
- 利用定位 + margin:auto
- 利用定位 + transform
- 利用定位 + margin:负值
- flex 布局
- grid 布局
总结
根据元素标签的性质,可以分为:
- 内联元素居中布局
- 块级元素居中布局
内联元素居中布局
水平居中
- 行内元素可设置:text-align: center
- flex 布局设置父元素:display: flex ; justify-content: center
垂直居中
- 单行文本父元素确认高度:height === line-height
- 多行文本父元素确认高度:disaply: table-cell; vertical-align: middle
块级元素居中布局
水平居中
- 定宽: margin: 0 auto
- 绝对定位 + left:50% + margin: 负自身一半
垂直居中
- position: absolute 设置 left、top、margin-left、margin-top(定高)
- display: table-cell
- transform: translate(x, y)
- flex (不定高,不定宽)
- grid(不定高,不定宽),兼容性相对比较差