跳至主要內容

如何让一个盒子垂直且水平居中 ?

Weipeng Qiao小于 1 分钟

使用定位(固定或绝对定位)

  • 方案一
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
  • 方案二:
1eft: 50%;
top: 50%;
margin-left: 负的宽度一半;
margin-top: 负的高度一半;

注意:该定位的元素必须设置宽高!

  • 方案三
top: 50%;
left: 50%;
/* 两个参数之间一定要加逗号 */
transform: translate(-50%, -50%);

flex

display: flex;
justify-content: center;
align-items: center;
上次编辑于:
贡献者: qiaoweipeng