【问题标题】:Why a child element with a top margin doesn't stretch the parent container为什么具有上边距的子元素不会拉伸父容器
【发布时间】:2015-01-16 06:23:20
【问题描述】:

给定一个具有上边距的子元素的 div,为什么边距不在父 div 内部而是在外部?

HTML

<div><h1>Titre<h1></div>

CSS

h1 { margin: 20px; }
div { background-color: #DDD; }

cfhttp://jsfiddle.net/adt515ww/

【问题讨论】:

标签: css


【解决方案1】:

这是由于collapsing margins。要修复它,请在您的 div 中添加溢出:自动:

div {
    background-color:#DDD;
    overflow:auto;
}

jsFiddle example

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 2014-07-10
    • 2019-06-22
    • 1970-01-01
    • 2011-01-11
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多