您的位置:首页 > Web前端 > CSS

css的外边距合并(如何实现不合并)

2016-03-20 13:54 489 查看


  如何实现外边框不合并呢?我总结了几个知乎大神和老师上课说的办法:

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

说的很全面了,可以参考






----------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  css 合并 html web