居中
水平居中
text-align:center
在父元素中设置text-align:center
实现行内元素水平居中,将子元素的display
设置为inline-block
,使子元素变成行内元素
<style>
.parent{text-align: center;}
.child{display: inline-block;}
</style>
不足:子元素的text-align
继承了父元素的center
,文字也居中显示,所以需要在子元素中设置text-align:left
margin
在本身元素设置margin: 0 auto
实现块级元素水平居中
2.1 将子元素的display
为table
,使子元素成为块级元素,同时table
还具有包裹性,宽度由内容撑开
<style>
.child{
display: table;
margin: 0 auto;
}
</style>
2.2 若子元素定宽,则可以使用绝对定位的盒模型属性,实现居中效果;若不设置宽度时,子元素被拉伸
<style>
.parent{
position: relative;
}
.child{
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
width: 50px;
}
</style>
absolute
通过绝对定位的偏移属性实现水平居中
3.1 配合translate()
位移函数
translate
函数的百分比是相对于自身宽度的,所以left:50%
配合translateX(-50%)
可实现居中效果
relative
数值型的偏移属性是相对于自身的,但百分比却是相对于包含块的。因为子元素已经被设置为 absolute
,所以若使用 relative
,则需要增加一层<div>
结构,使其宽度与子元素宽度相同.
<style>
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%;
transform:translateX(-50%);
}
</style>
3.2 配合relative
relative
数值型的偏移属性是相对于自身的,但百分比却是相对于包含块的。因为子元素已经被设置为absolute
,所以若使用relative
,则需要增加一层<div>
结构,使其宽度与子元素宽度相同
TIP
该方法全兼容,但是增加了 html 结构
<style>
.parent{
position: relative;
}
.childWrap{
position: absolute;
left: 50%;
}
.child{
position: relative;
left: -50%;
}
</style>
3.3 配合负margin
relative
数值型的偏移属性是相对于自身的,但百分比却是相对于包含块的。因为子元素已经被设置为absolute
,所以若使用relative
,则需要增加一层<div>
结构,使其宽度与子元素宽度相同
margin
的百分比是相对于包含块的,所以需要增加一层<div>
结构。由于宽度width
的默认值是auto
,当设置负margin
时,width
也会随着变大。所以此时需要定宽
处理
TIP
虽然全兼容,但需要增加页面结构及定宽处理,所以限制了应用场景
<style>
.parent{
position: relative;
}
.childWrap{
position: absolute;
left: 50%;
}
.child{
width:50px;
margin-left:-50%;
}
</style>
flex
<style>
.parent6{
display: flex;
justify-content: center;
/* 也可以使用margin */
/* margin: 0 auto */
}
</style>
grid
使用栅格布局grid
实现水平居中
<style > .parent7 {
display: grid;
justify-items: center;
/*justify-content:center;*/
}
</style>
/* 或者 child */
.child7 {
justify-self: center;
/*margin: 0 auto;*/
}
垂直居中
line-height
行高line-height
实现单行文本垂直居中
<style>
.test{
line-height: 100px;
background-color: lightblue;
}
</style>
TIP
在不设置高度的情况下,行高撑开高度
vertical-align
设置vertical-align:middle
实现垂直居中
通过为table-cell
元素设置vertical-align:middle
,可使其子元素均实现垂直居中。这和表格里单元格的垂直居中是类似的
TIP
设置为table-cell
的div
不能使用浮动或绝对定位,因为浮动或绝对定位会使元素具有块级元素特性,从而丧失了table-cell
元素具有的垂直对齐的功能。若需要浮动或绝对定位处理,则需要外面再套一层div
<style>
.parent{
display: table-cell;
vertical-align: middle;
}
</style>
特殊场景:子元素是图片
通过设置父元素的行高来代替高度,且设置父元素的font-size
为 0
vertical-align:middle
的解释是元素的中垂点与父元素的基线加1/2
父元素中字母X
的高度对齐。由于字符X
在em
框中并不是垂直居中的,且各个字体的字符X
的高低位置不一致。所以,当字体大小较大时,这种差异就更明显。当font-size
为 0 时,相当于把字符X
的字体大小设置为0
,于是可以实现完全的垂直居中
<style>
.parent{
line-height: 100px;
font-size: 0;
}
.child{
vertical-align: middle;
}
</style>

absolute
通过绝对定位实现垂直居中
3.1 配合translate()
位移函数, translate
函数的百分比是相对于自身高度的,所以top:50%
配合translateY(-50%)
可实现居中效果
TIP
子元素的高度已知,translate()
函数也可替换为margin-top: 负的高度值
<style>
.parent{
position:relative;
}
.child{
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>
3.2 若子元素定高,结合绝对定位的盒模型属性,实现居中效果
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
height: 50px;
}
</style>
WARNING
在水平居中对齐中,元素外层套一层div
并设置absolute
,元素设置负margin-left
或者relative
的负left
属性,可以实现水平居中的效果。但由于margin
是相对于包含块宽度的,这样margin-top:-50%
得到的是宽度而不是高度的-50%
,所以不可行; 对于relative
的百分比取值而言,在包含块高度为 auto 的情况下,chrome、safari 和 IE8+浏览器都不支持设置元素的百分比 top 值,所以也不可行
flex
使用弹性盒模型 flex 实现垂直居中
4.1 在伸缩容器上设置侧轴对齐方式align-items: center
<style>
.parent{
display: flex;
align-items: center;
}
</style>
4.2 在伸缩项目上设置margin: auto 0
<style>
.parent{
display: flex;
}
.child{
margin: auto 0;
}
</style>
grid
使用栅格布局grid
实现垂直居中
5.1 在网格容器上设置align-items
或align-content
<style>
.parent{
display:grid;
align-items:center;
/*align-content:center;*/
}
</style>
5.2 在网格项目中设置align-self
或者margin: auto 0
<style>
.parent{
display:grid;
}
.child{
align-self:center;
/*margin: auto 0;*/
}
</style>
水平&垂直居中
- 水平对齐 + 行高
<style>
.test{
text-align: center;
line-height: 100px;
}
</style>
- 水平 + 垂直对齐
2.1 父元素设置text-align
和vertical-align
,并将父元素设置为table-cell
元素,子元素设置为inline-block
元素
<style>
.parent{
display: table-cell;
text-align: center;
vertical-align: middle;
}
.child{
display: inline-block;
}
</style>
2.2 若子元素是图像,可不使用table-cell
,而是其父元素用行高替代高度,且字体大小设为 0。子元素本身设置vertical-align:middle
<style>
.parent{
text-align: center;
line-height: 100px;
font-size: 0;
}
.child{
vertical-align: middle;
}
</style>

margin
+ 垂直对齐
要想在父元素中设置vertical-align
,须设置为table-cell
元素;要想让margin:0 auto
实现水平居中的块元素内容撑开宽度,须设置为table
元素。而table
元素是可以嵌套在tabel-cell
元素里面的,就像一个单元格里可以嵌套一个表格
<style>
.parent{
display:table-cell;
vertical-align: middle;
}
.child{
display: table;
margin: 0 auto;
}
</style>
absolute
4.1 利用绝对定位元素的盒模型特性,在偏移属性为确定值的基础上,设置margin:auto
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 50px;
width: 80px;
margin: auto;
}
</style>
4.2 利用绝对定位元素的偏移属性和translate()
函数的自身偏移达到水平垂直居中的效果
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
4.3 在子元素宽高已知的情况下,可以配合margin
负值达到水平垂直居中效果
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 60px;
margin-left: -40px;
margin-top: -30px;
}
</style>
flex
5.1 在伸缩容器上使用主轴对齐 justify-content 和侧轴对齐 align-items
.parent {
display: flex;
justify-content: center;
align-items: center;
}
5.2 在伸缩项目上使用 margin:auto
<style>
.parent{
display: flex;
}
.child{
margin: auto;
}
</style>
grid
6.1 在网格容器上设置justify-items
、align-items
或justify-content
、align-content
<style>
.parent{
display: grid;
align-items: center;
justify-items: center;
/* align-content: center; */
/* justify-content: center; */
}
</style>
6.2 在网格项目中设置justify-self
、align-self
或者margin: auto
<style>
.parent{
display: grid;
}
.child{
align-self: center;
justify-self: center;
/* margin: auto; */
}
</style>