zoukankan      html  css  js  c++  java
  • CSS 之 嵌套 margin-top 处理

      如下代码:

        <div style=" 1000px; height:700px; margin:auto;">
            <div style=" 1000px; height:49px; margin-top:281px; ">
                <span style="display:inline-block; 121px; height:49px; margin-left:230px;">
                    <a href="#">
                        <img name="r2_c2" src="shc.png" width="121" height="49" border="0" id="btn1" alt="" />
                    </a>
                </span>        
            </div>
        </div>

      会发现,内层div的margin-top显示在了外层div上。

      这个“问题”……它是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分隔。

      解决方案:

      给 Outer Div 加上 padding/border,或者

      1.给父层添加:overflow:hidden;
      2.给父层添加:padding 或 border除none以外的属性
      3.给父层添加:padding-top代替margin-top的效果。
      4.给 外层 Div / 内层 Div 设置为 float/position:absolute(CSS2.1规定浮动元素和绝对定位元素不参与Margin折叠)。
  • 相关阅读:
    QBoxLayout中setSpacing(int)和addSpacing(int)的区别
    BZOJ1017 树形DP
    树形DP 2415HDU
    树形DP基础题 HDU1520
    最小生成树个数 并查集压缩路径
    伸展树基本概念基本题目
    BZOJ1010单调性DP优化
    BZOJ1009 矩阵快速幂+DP+KMP
    字典树(数组实现)
    poj 1611 The Suspects(并查集输出集合个数)
  • 原文地址:https://www.cnblogs.com/xinaixia/p/5563506.html
Copyright © 2011-2022 走看看