跳到主要内容

水平垂直居中

长念
长念阅读约 1 分钟2 年前发布

大体来说,下面 5 种方式都可以实现(细分的话可以算 8 种)。 示例 HTML 结构如下:

<div class="parent">
<div class="child"></div>
</div>

绝对定位 + 平移变换

transform: translate(x, y)x y 为百分比时,是相对自身的长度和宽度计算的。

.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

flex 布局

.parent {
display: flex; /* 使用 display: grid; 效果一致 */
justify-content: center;
align-items: center;
height: 100vh;
}

flex + margin

.parent {
display: flex; /* 使用 display: grid; 效果一致 */
height: 100vh;
}
.child {
margin: auto;
}

grid 布局

.parent {
display: grid;
height: 100vh;
}
.child {
align-self: center;
justify-self: center; /* 也可以换成 margin: auto; */
}

table-cell

.parent {
display: table-cell;
text-align: center; /* 子元素必须是 inline 类型元素 */
vertical-align: middle;
height: 100vh;
width: 100vw;
}
.child {
display: inline-block;
}