版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33276623/article/details/50936333
如何实现外边框不合并呢?我总结了几个知乎大神和老师上课说的办法:
1.可以只设置下边距或者上边距:
<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>不合并</title>
<style>
*{margin:0;padding: 0;}
.first, .second, .third{height:100px;width:500px;padding: 20px;margin-bottom: 20px;}
.first{ background:#ccc;}
.second{ background:#FCC;}
.third{ background:#9CF;}
</style>
</head>
<body>
<div class="first">first</div>
<div class="second">second</div>
<div class="third">third</div>
</body>
2。
让父级元素触发 BFC,就能使父级 margin 和当前元素的 margin 不重叠。
<!DOCTYPE html PUBLIC >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>不合并</title>
<style>
*{margin:0;padding: 0;}
.first, .second, .third{height:100px;width:500px;padding: 20px;margin: 20px;}
.container{
overflow: hidden;
}
.first{ background:#ccc;}
.second{ background:#FCC;}
.third{ background:#9CF;}
</style>
</head>
<body>
<div class="container">
<div class="first">first</div>
</div>
<div class="container">
<div class="second">second</div>
</div>
<div class="third">third</div>
</body>
可以看到第一和第二之间没有发生合并,而第二第三之间合并了。
3.border的设置:
<!DOCTYPE html PUBLIC >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>不合并</title>
<style>
*{margin:0;padding: 0;}
.first, .second, .third{height:100px;width:500px;padding: 20px;margin: 20px;border: 10px solid #fff;}
.container{
overflow: hidden;
}
.first{ background:#ccc;}
.second{ background:#FCC;}
.third{ background:#9CF;}
</style>
</head>
<body>
<div class="first">first</div>
<div class="second">second</div>
<div class="third">third</div>
</body>
这三方法其实都是让两个元素不再相邻,就可以避免合并的情况出现。
----------------------------------------------------------
2016/3/22修改
今天又不小心看到一篇总结的很好的博文。实在汗颜。
附地址:http://www.hujuntao.com/web/css/css-margin-overlap.html
说的很全面了,可以参考
----------------------------------------------------------------------