①子绝父相
②设置小div的left和top都为50%,向左、向上平移50%
<style>
.big{
width: 500px;
height: 500px;
border: 3px solid #f00;
position: relative;
}
.small{
width: 100px;
height: 100px;
background-color: green;
border: 2px solid #000;
position: absolute;
left: 50%;
top: 50%;
/* 二选一 */
/* margin: -50px 0 0 -50px; (用margin的方法还必须知道移动多少距离)*/
transform: translate(-50%, -50%);
}
</style>
<div class="big">
<div class="small"></div>
</div>