内层div的margin-top影响外层div——引出外边距合并Collapsing margins
内层div的margin-top影响外层div——引出外边距合并Collapsing margins
作者:zccst
今天才算是了解边距合并。正如一位前辈所言,每一个CSS的坑,都让你学到不少知识。
<style type="text/css">
body { margin:0; padding:0;}
div { margin:0; padding:0;}
.a { width:500px; margin:0px auto 30px; background:#eee;}
.d1 { height:100px; width:500px; margin-top:30px; margin-bottom:30px; background-color:#f40;}
.d2 { height:100px; width:500px; margin-top:30px; margin-bottom:30px; background-color:blue;}
</style>
<div class="a">
<div class="d1"></div>
<div class="d2"></div>
</div>
<style type="text/css">
.b { /*height:200px; background:#ddd;*/ margin-top:50px; margin-bottom:50px;}
</style>
<div class="b"></div>
/*
出现原因是符合垂直边距合并的条件。
(No.6)The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.
顶部发生合并,也就是本文的原因。注意发生条件是:外部div没有border,没有padding,而且与第一个子div之间没有任何间隔
消除办法:
一、字面直观解决办法
1,给外面div加padding-top:1px可以解决。
对于margin-top分三种情况:
(1)两个margin-top都是正数。margin-top:1px不行,原因是合并取其大。
(2)一正一负。如margin-top:-30px;两者互相抵消。离顶部距离等于30-(40/30/20),如果是-40之类的话,顶部一部分被隐藏了
(3)两个都是负值。
2,给外面div加border:1px solied transparent;
另外,如果在内层div的上面有内容的话,也是正常的。由于这是不可能的,所以就没有单列一条。
二、非字面解决办法
3,为父元素添加float:left可以。但是仅设置子元素float:left不行。
(No.1)Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
[4],为父元素添加overflow:hidden;样式即可(完美)。overflow可以的原因是生成了新的块元素。
(No.2)Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
5,为父元素添加position:abosulte可以,relative不行。子元素设置position任何值都不行。
(No.3)Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).
6,为子元素加display:inline-block也可以。
(No.4)Margins of inline-block boxes do not collapse (not even with their in-flow children).
疑问:float:left和position:absolute时上部有很大的空隙
底部也会发生类似的合并。
(No.7)The bottom margin of an in-flow block box with a 'height' of 'auto' and a 'min-height' of zero collapses with its last in-flow block-level child's bottom margin if the box has no bottom padding and no bottom border and the child's bottom margin does not collapse with a top margin that has clearance.
-------------------- 两个兄弟元素之间的margin合并情况 -----------------------------
(No.5)The bottom margin of an in-flow block-level element always collapses with the top margin of its next in-flow block-level sibling, unless that sibling has clearance.(两个<div></div>其它内容<div></div>)
-------------------- 一个内部元素自身与in-flow子孙的margin合并的情况 -----------------------------
(No.8)
A box's own margins collapse if the 'min-height' property is zero, and it has neither top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and all of its in-flow children's margins (if any) collapse.
结论:与测试事实不符。
In this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding or border areas or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin. 所有毗邻的两个或更多盒元素的margin将会合并为一个margin共享之。毗邻的定义为:同级或者嵌套的盒元素,并且它们之间没有非空内容、Padding或Border分隔。
简单讲:根据规范,一个盒子如果没有上补白和上边框,那么它的上边距应该和其文档流中的第一个孩子元素的上边距重叠
*/
8.3.1 Collapsing margins
In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.
Adjoining vertical margins collapse, except:
- Margins of the root element's box do not collapse.
- If the top and bottom margins of an element with clearance are adjoining, its margins collapse with the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block.
Horizontal margins never collapse.
Two margins are adjoining if and only if:
- both belong to in-flow block-level boxes that participate in the same block formatting context
- no line boxes, no clearance, no padding and no border separate them (Note that certain zero-height line boxes (see 9.4.2) are ignored for this purpose.)
- both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
- top margin of a box and top margin of its first in-flow child
- bottom margin of box and top margin of its next in-flow following sibling
- bottom margin of a last in-flow child and bottom margin of its parent if the parent has 'auto' computed height
- top and bottom margins of a box that does not establish a new block formatting context and that has zero computed 'min-height', zero or 'auto' computed 'height', and no in-flow children
A collapsed margin is considered adjoining to another margin if any of its component margins is adjoining to that margin.
Note. Adjoining margins can be generated by elements that are not related as siblings or ancestors.
Note the above rules imply that:
- Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
- Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
- Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).
- Margins of inline-block boxes do not collapse (not even with their in-flow children).
- The bottom margin of an in-flow block-level element always collapses with the top margin of its next in-flow block-level sibling, unless that sibling has clearance.
- The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.
- The bottom margin of an in-flow block box with a 'height' of 'auto' and a 'min-height' of zero collapses with its last in-flow block-level child's bottom margin if the box has no bottom padding and no bottom border and the child's bottom margin does not collapse with a top margin that has clearance.
- A box's own margins collapse if the 'min-height' property is zero, and it has neither top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and all of its in-flow children's margins (if any) collapse.
When two or more margins collapse, the resulting margin width is the maximum of the collapsing margins' widths. In the case of negative margins, the maximum of the absolute values of the negative adjoining margins is deducted from the maximum of the positive adjoining margins. If there are no positive margins, the maximum of the absolute values of the adjoining margins is deducted from zero.
If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.
- If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.
- Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the element had a non-zero bottom border.
Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements.
内层div的margin-top影响外层div——引出外边距合并Collapsing margins的更多相关文章
- [CSS][转载]内层div的margin-top影响外层div
参考 内层div的margin-top影响外层div——引出外边距合并 div嵌套导致子区域margin-top失效不起作用的解决方法 我使用的是在外层的div中添加 border: 1px soli ...
- 为什么margin-top不是作用于父元素【margin外边距合并问题】
coding时发现margin-top居然没作用于本元素上,而是作用到了父元素上. 原来是margin外边距合并导致的.以下是网上搬运来的知识: margin外边距合并详解:外边距合并现象在网页布局中 ...
- 关于collapsed margin(外边距合并)
这是前面写postion定位时写到最后面的例子的时候发现的一个问题,于是专门写一篇随笔来解释记录一下,毕竟两个知识点同时写在一篇文章里面有点混乱的感觉.. 上篇随笔position定位遇到的问题在这里 ...
- margin显示怪异,外边距合并问题
很多时候我们使用两个div,内层的div设置文字,需要垂直居中与上层div,但是怎么设置样式都不行,vertical-align:middle也不行. 代码: <div style=" ...
- CSS外边距合并(塌陷/margin越界)
原文 简书原文:https://www.jianshu.com/p/5f18f12cd162 大纲 1.什么是外边距合并?(折叠外边距) 2.外边距带来的影响 3.折叠的结果 4.产生折叠的原因 5. ...
- 关于margin外边距合并的问题
一 .兄弟元素margin外边距合并演示 当两个垂直方向相邻的兄弟元素都为常规流块盒,他们之间垂直方向的外边距不是两者之和,而是取两者中的最大值.这种现象被称为相邻的兄弟元素垂直方向外边距合并. ...
- CSS Margin外边距合并
应该知道这点东西的!!! 可是偏偏记不住! 外边距合并会发生在以下两种情况下: 1 垂直出现的两个拥有外边距的块级元素. div1 { margin-bottom: 20px; } div2 { ma ...
- “margin塌陷” 嵌套盒子外边距合并现象
来源于官方文档对于外边距合并的解释: 注释:只有普通文档流中块框的垂直外边距才会发生外边距合并.行内框.浮动框或绝对定位之间的外边距不会合并. 出现外边距塌陷的三种情况: 1.相邻兄弟元素之间 若两者 ...
- margin 外边距合并问题
一.兄弟元素的外边距合并 效果图如下:(二者之间的间距为100px,不是150px) 二.嵌套元素的外边距合并 对于两个嵌套关系的元素,如果父元素中没有内容或者内容在子元素的后面并且没有上内边距及边框 ...
随机推荐
- LightOJ 1259 Goldbach`s Conjecture 素数打表
题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...
- HUST - 1599 Multiple
input 长度不大于3*10e5的数字串 output 不含前导0的能整除64的字串的个数(0算一个,064不算) 一般数组中找能整除一个数的字串都是用取余来做的 用一个a[64]来存下从1-i位累 ...
- HDU<1372>/bfs
题目连接 简单bfs搜索 #include <set> #include <map> #include <cmath> #include <queue> ...
- ReactiveCocoa / RxSwift 笔记一
原创:转载请注明出处 ReactiveCocoa / RxSwift Native app有很大一部分的时间是在等待事件发生,然后响应事件,比如 1.等待网络请求完成, 2.等待用户的操作, 3.等待 ...
- Android Camera(一)
最近老大交给了一个任务,说是要在本地视频端很够调节摄像头焦距. 碰到了一些问题: 1.手机支不支持摄像头变焦 2.系统自带摄像软件可以变焦,但是自己编写的程序不支持变焦, 这个问题网上也有很多童鞋碰到 ...
- SAX,DOM,JAXP,JDOM,DOM4J比较
dom,sax,jdom,dom4j的技术特点: 1: DOMDOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许 ...
- java 文件字符输入、输出流
Example10_6.java import java.io.*; public class Example10_6 { public static void main(String args[]) ...
- 转:Linux基本命令大全
Linux基本命令大全 新手刚刚接触Linux的时候可能处处感到不便,不过没有关系,接触新的事物都有这样的一个过程,在你用过Linux一段时间后,你就会逐渐了解Linux其实和Windows一样容 ...
- LINUX中磁盘挂载与卸除
一.挂载格式与参数说明: 要将文件系统挂载到我们的 Linux 系统上,就要使用 mount 这个命令啦! 不过,这个命令真的是博大精深-粉难啦!我们学简单一点啊- ^_^ [root@www ~]# ...
- Unity中www的基本应用
Unity的www主要支持HTTP中的GET和POST方式,GET方式会将请求附加到URL后,POST方式则是通过FORM的形式提交. 以下为Unity客户端的信息: using UnityEngin ...