margin-top 为什么会影响父元素的 margin-top

 1.原因:

 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分隔。

2.解决方法:

  1、修改父元素的高度,增加padding-top样式模拟(padding-top:1px;常用)
  2、为父元素添加overflow:hidden;样式即可(完美)
  3、为父元素或者子元素声明浮动(float:left;可用)
  4、为父元素添加border(border:1px solid transparent可用)
  5、为父元素或者子元素声明绝对定位

 

posted @ 2016-05-09 11:09  好叶叶孤城  阅读(202)  评论(0编辑  收藏  举报