一行 Grid 解决水平垂直居中,不需要知道元素尺寸。
<div class="stage"><div class="box">centered</div></div>.stage {
display: grid;
place-items: center;
min-height: 150px;
width: 100%;
background: #f6f8fc;
border-radius: 12px;
}
.box {
width: 120px; height: 64px;
display: grid; place-items: center;
background: #2b57ff; color: #fff;
border-radius: 10px;
font: 600 13px/1 ui-monospace, monospace;
}复制后可直接使用;如果要放进已有项目,注意把类名改成你项目里的命名,避免冲突。