css 居中
方案一: display: flex
.parent {
display: flex;
justify-content: center;
align-items: center;
width: 500px;
height: 500px;
background-color: aqua;
}
.child {
width: 100px;
height: 100px;
background-color: blue;
}方案二: position + margin
利用绝对定位 + margin:auto // 优点: 不需要提前知道尺寸, 兼容性好
.parent {
position: relative;
background-color: aqua;
width: 500px;
height: 500px;
}
.child {
position: absolute;
width: 100px;
height: 100px;
background-color: blue;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}方案三: position + margin-top
方案四: position + 利用transform
方案五: table
方案六: grid
最后更新于
这有帮助吗?