CSS
居中
CSS实现垂直水平居中的几种方式:
1. Flexbox
.center-flex {
display: flex;
justify-content: center;
align-items: center;
}2. Grid
.center-grid {
display: grid;
place-items: center;
}3. Position + Transform
.center-transform {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}4. Position + Margins (适用于已知宽高)
.center-margin {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 200px; /* 需指定宽度 */
height: 100px; /* 需指定高度 */
}5. Table布局
.center-table {
display: table-cell;
text-align: center;
vertical-align: middle;
width: 100vw;
height: 100vh;
}
.center-table-child {
display: inline-block;
}6. Calc方法 (适用于已知宽高)
.center-calc {
position: absolute;
top: calc(50% - 50px); /* 减去高度的一半 */
left: calc(50% - 100px); /* 减去宽度的一半 */
width: 200px;
height: 100px;
}7. Flexbox的另一种写法
.center-flex-alt {
display: flex;
}
.center-flex-alt-child {
margin: auto;
}