这个问题是CSS2.1
的盒模型中规定的内容——Collapsing margins
:
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分隔。
这就是原因了。“嵌套”的盒元素也算“毗邻”,也会 Collapsing Margins。
这个问题的避免方法很多,只要破坏它出现的条件就行。给Outer Div
加上 padding/border
,或者给 Outer Div / Inner Div
设置为 float/position:absolute
(CSS2.1
规定浮动元素和绝对定位元素不参与margin
折叠)。
更多信息,关于不同值的margin
折叠后表现的计算方法等等,可自行查阅。
- 相关链接: