我先把实现后的代码发一下<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
margin: 0;
padding:0;
}
.div1{
position:relative;
width: 200px;
height: 200px;
background-color: lightblue;
/* margin-left:100px; */
margin:0 auto;
}
.div2{
position: absolute;
width: 100px;
height: 100px;
background-color: pink;
margin-top: 50px;
margin-left:50px;
/* margin:50px; */ /* 给第二个div块添加外边距 */
}
</style>
</head>
<body>
<div class='div1'>
<div class='div2'></div>
</div>
</body>
</html>
这样就能实现你说的理想效果了! |