【问题标题】:Top-Margin Not working Instead Height is increased [duplicate]上边距不起作用,而是增加了高度[重复]
【发布时间】:2014-07-25 12:37:31
【问题描述】:

我试图用id=footertop-margin 分配给div。但它不起作用,而是增加了外部div 的高度。 HTML 文件:

<div id="lowestContainer">
    <div class="middle" id="footer">
        <address id="addressSFCM">
            xxxxx-xxxx-xxxxx-xxxx<br/>
            xx--xx--xx--xx<br/>
            xxx---xxx-x--x-xx-xx<br/>
            Email:xxxxx@hotmail.com
        </address>
    </div>
</div>

css文件:

#lowestContainer{
    background-color: rgba(18,19,20,1);
    height: 300px;
    z-index: 1000;
    box-shadow: 1px 0px 3px rgb(0,0,0,0.3);
}
#footer{
    width:1000px;
    height:100px;
    display:block;
}
#addressSFCM{
    color: rgba(76,76,76,1);
    max-width: 220px;
    height:auto;
    /*margin-top: 30px;*/
    display:inline;
}

请帮我解决这个问题;

【问题讨论】:

    标签: html css


    【解决方案1】:

    另一种方法是将 padding-top 添加到产生相同结果的最低容器中

    See this fiddle

    #lowestContainer{
        padding-top:30px;
    }
    

    【讨论】:

    • 好的,但我需要一个它不工作的原因
    • 据我了解,Margin 不会影响孩子相对于其父母的位置,除非父母有填充(我的解决方案证明了这一点)在此处阅读有关盒子模型的更多信息:w3.org/TR/CSS2/box.html(参见 8.3 .1)
    • 但有时它适用于向左或向右浮动的元素
    【解决方案2】:

    如下所示更新您的页脚类。

    #footer{
    width:1000px;
    height:100px;
    display:inline-block;
    margin-top:100px;
    text-align:center;
    }
    

    DEMO

    【讨论】:

    • 执行此操作后,页脚不会保持在中心
    • 中心??没听懂你说的是哪一个。
    • class=中间属性包含margin-left:auto;margin-right:属性。
    • 更新了我的答案。使用text-align:center
    【解决方案3】:

    overflow: hidden 添加到包装器

    DEMO

    #lowestContainer{
      overflow: hidden;
    }
    #footer{
        margin-top: 50px; // add top margin as per needed
    }
    

    【讨论】:

    • thnxx 它有效!但是为什么能解释一下原因
    • 当您尝试将top margin 应用于内部div 时,它实际上就像将上边距应用于父div#lowestContainer。这就是为什么当您要将边距应用于内部 div 时,主包装器 div 会向下移动。
    • 那么overflow:hidden 是如何提供帮助的
    • 我也看到它有时对子 div 正常工作,为什么会这样
    • 虽然我们知道申请overflow: hidden后,内容是clipped。所以它允许在这里将top margin 应用于内部div。应用overflow:hidden', the top margin` 后应用到inner div' and the main wrapper is clippped`。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2012-07-26
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多