【布局】483- 推荐 15 种水平垂直居中布局方案
共 5445字,需浏览 11分钟
· 2020-08-13
https://juejin.im/post/5db706d5f265da4d31073959
我们在日常的开发中,经常会遇到这样一个问题,就是如何实现居中水平垂直居中对齐。并且在面试中也会出现这样的问题,但是我们往往回答的不是很全部,而导致没有得到面试加分。接下来我们通过不同的方式来实现,让我们成功破解这道面试。
1、定宽高
一、绝对定位和负magin值
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
![](https://filescdn.proginn.com/cab95557a68d6ff75f7009503d28850f/c2acf5a68ac0cceb296eacd87b5cc1a0.webp)
二、绝对定位 + transform
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
![](https://filescdn.proginn.com/8059bd99ba776cd4380dc54951dc939b/789d80ff881b4bd5a9543df2705a3a39.webp)
三、绝对定位 + left/right/bottom/top + margin
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
display: inline;
top: 0;
left: 0;
right: 0;
bottom: 0px;
background: yellow;
margin: auto;
height: 100px;
width: 100px;
}
![](https://filescdn.proginn.com/caf8a3aa918dbb777da5a5d7429a0dee/8eae2bfb1080668887d1298080d3b906.webp)
四、flex布局
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.children-box {
background: yellow;
height: 100px;
width: 100px;
}
![](https://filescdn.proginn.com/1c064f4075f17b31dd6601eb288e396b/e523000202a039719e115caea82fc239.webp)
五、grid布局
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: grid;
}
.children-box {
width: 100px;
height: 100px;
background: yellow;
margin: auto;
}
![](https://filescdn.proginn.com/4d8a19e24cdb5948594c1ee13de6a943/e089f108409660038b4fabecd3b020b6.webp)
六、table-cell + vertical-align + inline-block/margin: auto
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.children-box {
width: 100px;
height: 100px;
background: yellow;
display: inline-block;// 可以换成margin: auto;
}
![](https://filescdn.proginn.com/8b8675645f98733de7ce8845c7ffd5e1/2e6fd55820c44f96034f9b8979b6071a.webp)
2、不定宽高
一、绝对定位 + transform
111111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.children-box {
position: absolute;
background: yellow;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
![](https://filescdn.proginn.com/6528220c46466ed220e5d1cf35759417/d89f519104ca9afc39a9b050d5c16ef9.webp)
二、table-cell
111111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.children-box {
background: yellow;
display: inline-block;
}
![](https://filescdn.proginn.com/6221d15479018ec2d4096c6c926f5e40/75fcaf6bd5cbb372f9513ec897e93375.webp)
三、flex布局
11111111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.children-box {
background: yellow;
}
![](https://filescdn.proginn.com/b0229f4eac10b4a086487c08032cad58/e419fc7e9ba71ff027f4fe277c1a1c7a.webp)
四、flex变异布局
11111111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
}
.children-box {
background: yellow;
margin: auto;
}
![](https://filescdn.proginn.com/36a33907795fde01119fc0979b0911d4/5155e25ba62f7fbee1a7b685716545b5.webp)
五、grid + flex布局
11111111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: grid;
}
.children-box {
background: yellow;
align-self: center;
justify-self: center;
}
![](https://filescdn.proginn.com/5796d7a46d9b2c378be0443c374ddcc2/78f1570b6486eeee1cfacd6fc187dc97.webp)
六、gird + margin布局
11111111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
display: grid;
}
.children-box {
background: yellow;
margin: auto;
}
![](https://filescdn.proginn.com/fdb4a247d9ffa46b738174dcd1d428dc/f79faaf51f2d4a8b60d15dd0c7d21064.webp)
七、writing-mode属性布局
11111
.box {
width: 200px;
height: 200px;
border: 1px solid red;
writing-mode: vertical-lr;
text-align: center;
}
.box>.children-box {
writing-mode: horizontal-tb;
display: inline-block;
text-align: center;
width: 100%;
}
.box>.children-box>p {
display: inline-block;
margin: auto;
text-align: left;
background: yellow;
}
![](https://filescdn.proginn.com/4add81b8629e17c0bca76f23221d34be/29a4c5970757d3ebecdc7c5849509fbc.webp)
writing-mode 属性定义了文本在水平或垂直方向上如何排布。兼容性上还有些小瑕疵,但大部分浏览器已经支持。
![](https://filescdn.proginn.com/fb50af485aadeb63c2800f59a12cc0b9/0627d72a51dfbe005750adbfd5a93ced.webp)
3、番外(图片定高|不定高水平垂直居中)
一、table-cell
.box {
height: 200px;
width: 200px;
display: table-cell;
text-align: center;
border: 1px solid #ccc;
vertical-align: middle;
}
![](https://filescdn.proginn.com/0464f37632db4188fabb9031d1767b97/ce4e7b0e1995ad5b98d89e2a1fa386fc.webp)
二、 ::after
.box {
width: 200px;
height: 200px;
border: 1px solid red;
text-align: center;
}
.box::after {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
img {
vertical-align: middle;
}
![](https://filescdn.proginn.com/ecfbdfafc3c4de3ff545d933f4c3805a/a5ef6ad3da9151b781bb677a96c3807c.webp)
三、 ::before
.box {
width: 200px;
height: 200px;
border: 1px solid #ccc;
text-align: center;
font-size: 0;
}
.box::before {
display: inline-block;
vertical-align: middle;
content: '';
height: 100%;
}
img {
vertical-align: middle;
}
![](https://filescdn.proginn.com/3faef0e38bf65330f8545f888f1527ad/c4694e3bf136133d2778b13991631124.webp)
四、总结
这里总结了很多种垂直水平居中的方法,但是肯定不是最完全的,还有很多其他中方式。但是总体的思路可以总结为以下几点。
一、内联元素居中布局
水平居中
行内元素可设置:text-align: center;
flex布局设置父元素:display: flex; justify-content: center;
垂直居中
单行文本父元素确认高度:height === line-height
多行文本父元素确认高度:disaply: table-cell; vertical-align: middle;
二、块级元素居中布局
水平居中
定宽: margin: 0 auto;
不定宽:参考上诉例子中不定宽高例子。
垂直居中
position: absolute设置left、top、margin-left、margin-to(定高);
position: fixed设置margin: auto(定高);
display: table-cell;
transform: translate(x, y);
flex(不定高,不定宽);
grid(不定高,不定宽),兼容性相对比较差;
回复“加群”与大佬们一起交流学习~
![](https://filescdn.proginn.com/efb65efcc171dcc2c4c2721934d8de5d/3d67d2ff67da6741d1f33afd8b0bf27b.webp)