水平垂直居中
方法1 绝对定位加margin:auto
<div class="box">
<div class="child"></div>
</div>
.box{
width: 100vw;
height: 100vh;
background-color: antiquewhite;
position: relative;
}
.child{
width: 200px;
height: 100px;
background-color: gold;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
方法2 绝对定位配合负margin
.box{
width: 100vw;
height: 100vh;
background-color: antiquewhite;
position: relative;
}
.child{
width: 200px;
height: 100px;
background-color: gold;
position: absolute;
top:50%;
left:50%;
margin: -50px -100px;
}
方法3 绝对定位配合transform
.box{
width: 100vw;
height: 100vh;
background-color: antiquewhite;
position: relative;
}
.child{
width: 200px;
height: 100px;
background-color: gold;
position: absolute;
top:50%;
left:50%;
transform: translate(-50%,-50%)
}
方法4 flex布局
.box{
width: 100vw;
height: 100vh;
background-color: antiquewhite;
display: flex;
justify-content: center;
align-items: center;
}
.child{
width: 200px;
height: 100px;
background-color: gold;
}