1、先上代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{padding: 0;margin: 0;}
.wrap{width: 400px;height: 400px;background-color: lightblue;}
.main{width: 100px;height: 100px;background-color: lightgreen;margin-top: 50px;}
</style>
</head>
<body>
<div class="wrap">
<div class="main">
</div>
</div>
</body>
</html>
2、代码效果如下
3、解决方案
a、为父元素设置overflow:hidden
b、为父元素设置border,具体情况设置border:1px solid transparent或者白色
c、为子元素和父元素绝对浮动
d、为子元素或父元素绝对定位
e、子元素设置padding-top,但需要更改父元素的高度,也可以使用box-sizing:border-box来消除影响