1.元素垂直水平居中
(1)采用定位
<div class="parent">
<div class="child"></div>
</div>
.parent{
position: relative;
background-color: pink;
width: 500px;
height: 500px;
}
.child{
position: absolute;
background-color: blue;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
(2)利用margin:auto
.parent{
position: relative;
background-color: pink;
width: 500px;
height: 500px;
}
.child{
position: absolute;
background-color: blue;
width: 100px;
height: 100px;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(3)利用弹性布局
.parent{
position: relative;
<