方案一:设置父元素的border-top值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
background: pink;
}
.div1{
background: blue;
height: 30px;
width: 30px;
border-top: 10px solid white;
}
.div2{
background: black;
height: 20px;
width: 20px;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d95a99b3690068e5f1884bc04e3cc554.jpeg)
方案二:设置子元素mrgin-top值并令父元素更改为BFC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
background: pink;
}
.div1{
background: blue;
height: 30px;
width: 30px;
overflow: hidden;父元素overflow≠visible
}
.div2{
background: black;
height: 20px;
width: 20px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/af8a534c2a559d0e35fda24a9b03122f.jpeg)
方案三:设置父元素padding-top
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
background: pink;
}
.div1{
background: blue;
height: 30px;
width: 30px;
padding-top: 10px;
box-sizing: border-box;
}
.div2{
background: black;
height: 20px;
width: 20px;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>