HTML使用绝对定位进行垂直居中的方法:
1.父元素为body
position:absolute;
top:50%;
margin-top:-自己高度的一半;
2.当有其他父元素时使用“子绝父相来实现定位”
子绝父相的意思是 子元素为绝对定位 父元素为相对定位
<style>
.or{
width: 400px;
height: 400px;
background-color: orangered;
position: relative;
}
.pu{
width: 100px;
height: 100px;
background-color: blueviolet;
position: absolute;
left: 50%;
top: 50% ;
margin-top: -50px;
margin-left: -50px;
}
</style>
<body>
<div class="or">
<div class="pu"></div>
</div>
</body>