水平垂直居中
大约 2 分钟约 674 字
定位(absolute) + 负margin
.wp {
position: relative;
}
.box {
position: absolute;;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
Demo 演示
<div class="wp">
<div class="box size">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
.box.size{
width: 100px;
height: 100px;
}
/* ======================== */
.wp {
position: relative;
}
.box {
position: absolute;;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
定位(absolute) + margin auto
.wp {
position: relative;
}
.box {
position: absolute;;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
Demo 演示
<div class="wp">
<div class="box size">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
.box.size{
width: 100px;
height: 100px;
}
/* ======================== */
.wp {
position: relative;
}
.box {
position: absolute;;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
定位(absolute) + calc
.wp {
position: relative;
}
.box {
position: absolute;;
top: calc(50% - 50px);
left: calc(50% - 50px);
}
Demo 演示
<div class="wp">
<div class="box size">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
.box.size{
width: 100px;
height: 100px;
}
/* ======================== */
.wp {
position: relative;
}
.box {
position: absolute;;
top: calc(50% - 50px);
left: calc(50% - 50px);
}
定位(absolute) + transform
.wp {
position: relative;
}
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Demo 演示
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
position: relative;
}
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
flex
.wp {
display: flex;
justify-content: center;
align-items: center;
}
Demo 演示
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
display: flex;
justify-content: center;
align-items: center;
}
flex + margin
.wp {
display: flex;
}
.box {
margin: auto;
}
Demo 演示
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
display: flex;
}
.box {
margin: auto;
}
lineheight
.wp {
line-height: 300px;
text-align: center;
font-size: 0px;
}
.box {
font-size: 16px;
display: inline-block;
vertical-align: middle;
line-height: initial;
text-align: left; /* 修正文字 */
}
Demo 演示
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
line-height: 300px;
text-align: center;
font-size: 0px;
}
.box {
font-size: 16px;
display: inline-block;
vertical-align: middle;
line-height: initial;
text-align: left; /* 修正文字 */
}
table
<table>
<tbody>
<tr>
<td class="wp">
<div class="box">123123</div>
</td>
</tr>
</tbody>
</table>
.wp {
text-align: center;
}
.box {
display: inline-block;
}
Demo 演示
<table>
<tbody>
<tr>
<td class="wp">
<div class="box">123123</div>
</td>
</tr>
</tbody>
</table>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
text-align: center;
}
.box {
display: inline-block;
}
css-table
.wp {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.box {
display: inline-block;
}
Demo 演示
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.box {
display: inline-block;
}
grid
.wp {
display: grid;
}
.box {
align-self: center;
justify-self: center;
}
Demo 演示
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
/* ======================== */
.wp {
display: grid;
}
.box {
align-self: center;
justify-self: center;
}