css – 保证金堆叠在一起?

我很早就注意到,当两个块元素彼此相邻时,它们的边缘相互叠加.像这样的东西:

两个< div>都有保证金:1em,但当margin1的margin-bottom与margin2的margin-top碰撞时,两个保证金都相互叠加.见这里:http://jsfiddle.net/39XmC/

我所期待的是这样的:

两个< div> s实际上在每个边距上给出空格并且不在彼此的边缘上堆叠.

我知道这可以通过floating修复或溢出元素.我的问题:

>为什么会在理论上发生这种情况?边际不应该堆叠吗?
>这是浏览器的认行为吗?因为我记得在一个没有这种行为的项目上工作.

最佳答案
这种行为是正常的,它被称为崩溃边缘..

引自W3C

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.

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效