懵懂听风雨 2019-07-01
css盒子模型又称框模型 (Box Model) ,包含了元素内容(content)、内边距(padding)、边框(border)、外边距(margin)几个要素。
图中element元素是实际内容,也就是元素框,紧挨着元素框外部的是内边距padding,其次是边框(border),然后最外层是外边距(margin),整个构成了盒子模型。通常我们设置的背景显示区域,就是内容、内边距、边框这一块范围。而外边距margin是透明的,不会遮挡周边的其他元素。那么:
两个上下方向相邻的元素框垂直相遇时,外边距会合并,合并后的外边距的高度等于两个发生合并的外边距中较高的那个边距值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
.top{
width:400px;
background: #0ff;
height:100px;
margin:30px 30px;
}
.bottom{
width:400px;
height:100px;
margin:50px 30px;
background: #ddd;
}
</style>
</head>
<body>
<section class="top">
<h1>上</h1>
margin-bottom:30px;
</section>
<section class="bottom">
<h1>下</h1>
margin-top:50px;
</section>
</body>
</html>
需要注意的是:只有普通文档流中块框的垂直外边距才会发生外边距合并。
BFC其英文拼写为 Block Formatting Context 直译为“块级格式化上下文”
上面边距合并例子调整:
<section class="top">
<h1>上</h1>
margin-bottom:30px;
</section>
<!-- 给下面这个块添加一个父元素,在父元素上创建bfc -->
<div style="overflow:hidden">
<section class="bottom">
<h1>下</h1>
margin-top:50px;
</section>
</div>
关于bfc的应用的案例这里就不在一一举出了,大家去网上找一些其他的文章看一下。
box-sizing : content-box|border-box|inherit
总宽度=margin+border+padding+width;总高度=margin+border+padding+height总宽度=margin+width; 总高度=margin+height偷两张图贴起来~

