html1
2
3
4
<div class="parent">
<div class="child"></div>
</div>
css
水平居中
行内元素
1 | .parent { |
块级元素
1 | <!-- margin: auto --> |
垂直居中
行内元素
1 | <!-- 行内元素(单行文字垂直居中):设置 line-height = height --> |
块级元素
绝对定位
兼容性好,但是需要知道绝对定位元素的高度1
2
3
4
5
6
7
8
9
10
11
12.parent {
position: relative;
}
.child {
width: 80px;
height: 80px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -40px;
margin-left: -40px;
}
绝对定位 + transform
不需要知道高度,但是IE9以下不兼容1
2
3
4
5
6
7
8
9
10.parent {
position: relative;
}
.child {
width: 80px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
绝对定位 + margin: auto;
不需要提前知道尺寸,兼容性好1
2
3
4
5
6
7
8
9
10
11.parent {
position: relative;
}
.child {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
display: table-cell
1 | .parent { |
或者1
2
3
4
5
6
7
8
9.parent {
height: 300px;
display: table-cell;
vertical-align: middle;
}
.child {
vertical-align: middle;
background: blue;
}
display: flex
部分浏览器要兼容性处理1
2
3
4
5
6
7
8.parent {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
align-items: center; /*垂直居中*/
justify-content: center; /*水平居中*/
}
calc() + padding
需要做兼容性处理,需要知道尺寸1
2
3
4
5
6
7
8
9
10
11
12
13
14
15.parent {
width: 300px;
height: 300px;
border: 1px solid red;
}
.child {
width: 100px;
height: 100px;
background: blue;
padding: -webkit-calc((100% - 100px) / 2);
padding: -moz-calc((100% - 100px) / 2);
padding: -ms-calc((100% - 100px) / 2);
padding: calc((100% - 100px) / 2);
background-clip: content-box;
}