一、效果
二、代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.father {
width: 800px;
height: 600px;
background-color: pink;
position: relative;
margin: 0 auto;
}
.son {
width: 100px;
height: 60px;
background-color: purple;
position: absolute;
/* margin: 0 auto; 绝对定位的盒子无法再用margin实现居中效果*/
left: 50%;
/* 先走父盒子的一半 */
margin-left: -50px;
/* 再走自己外边距负一半值就可以了 */
top: 50%;
margin-top: -30px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>