zoukankan      html  css  js  c++  java
  • 子元素应该margin-top影响父元素的解决办法

    在子元素设置margin-top,有时会带着父元素一起移动。

    原因:

      Outer Div [margin: 0 auto]

      Inner Div [margin-top: 10px]

      根据CSS2.1盒模型规范,两层Div结构,Outer Div属性为“margin:0 auto”,本该紧贴外框顶部的,如果没有Inner Div,或者没有Inner Div的“margin-top”属性,一切如预期。但是当Inner Div设置了“margin-top:10px”之后,它的父元素Outer Div也被撑出来一个本不该有的“margin-top:10px”效果。

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

      “嵌套”的盒元素也算“毗邻”,也会导致公用border。

    知道原因了,解决办法有三种:

      1,若子DIV块中使用margin-top,则在父DIV块中添加:overflow:hidden;

      2,Outer Div 加上 padding/border

      3,Outer Div / Inner Div 设置为 float/position:absolute(CSS2.1规定浮动元素和绝对定位元素不参与Margin折叠)。 

      我采用第二个办法,只要让两个div之间有空隙就可以了。那么在父div设置padding:0.5px;即可

  • 相关阅读:
    查windows系统开关机记录
    HDU-6278-Jsut$h$-index(主席树)
    POJ-2104-Kth Number(主席树)
    HDU-6546-Function(贪心)
    POJ-1523-SPF(求割点)
    POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)
    POJ-2552-The Bottom of a Graph 强连通分量
    POJ-1659-Frogs' Neighborhood
    POJ-1904-King‘s Quest
    POJ-1236-Network of Schools
  • 原文地址:https://www.cnblogs.com/zhangyabin---acm/p/5132499.html
Copyright © 2011-2022 走看看