只提供核心代码,多余代码已被省去。
padding
适用于 parent 的高度不确定的情况
.parent {
padding: 20px 0;
}
.child {}
flex
.parent {
display: flex;
justify-content: center;
align-items: center;
}
.child {}
absolute margin auto
.parent {
position: relative;
}
.child {
position: absolute;
margin: auto;
width: 300px;
height: 200px;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
注意:
- child 的宽高都必须是确定值
- 可用
inset: 0代替top: 0; bottom: 0; left: 0; right: 0;
absolute translate
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
grid
.parent {
display: grid;
}
.child {
place-self: center;
}
评论区
评论加载中...