.box1 {
width: 100px;
height: 100px;
background-color: aqua;
}
/* 1. 绝对定位水平居中 */
.box1 {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
/* 2. 水平垂直居中(不知道盒子大小) */
.box1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* 3.居中(知道盒子大小) */
.box1 {
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
/* 4.flex居中 */
.box1 {
display: flex;
align-items: center;
justify-content: center;
}
盒子居中的几种方式
最新推荐文章于 2023-03-19 19:49:44 发布