水平垂直居中
table、相对定位、flex、grid
父
display: table,子display: table-cell; vertical-align: middle(父高度确定)父相对,子绝对,子
top left 50%,transform -50%
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
- (父子高度确定)父相对,子绝对,子
top0 left0 bottom0 right0,margin auto
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
- (父子高度确定)父相对,子绝对,子
margin-top/margin-left是height/width的一半,top/left: 50%
.parent{
height: 600px;
position: relative;
}
.child{
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px;
/* left: 50%; */
/* width: 300px; */
/* margin-left: -150px; */
}
父
flex,justify-content algin-items 都是 center父
flex,子margin: auto父
grid,justify-content algin-items 都是 center父
grid,place-items: center父
grid,子 align-self justify-self 都是 center