内部div不尊重边缘顶部相对于父div [英] Inner div not respecting margin-top relative to parent div

查看:102
本文介绍了内部div不尊重边缘顶部相对于父div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查这个非常简单的html结构。在另一个div中,内部(绿色)div具有 margin-top:100px ,外部(红色)div没有margin-top。外部div是内部div(100px)的两倍(200px)。

那么你期望结果如何?如果你和我一样,你会得到与你期望得到的相反的结果。



我的问题不是如何防止这种情况。我的问题是,为什么在所有浏览器中这种方式一致?我的意思是,这显然必须是它的工作方式,但对我而言,这是非常直观的。 jsdata-hide =false>

.red {background-color :#a00; height:200px;}。green {background-color:#0a0; margin-top:100px; height:100px;}

< div class =red > < div class =green>< / div>< / div>

http://jsfiddle.net/connexo/7txnoj7m/

解决方案

其正常渲染方式称为边缘折叠。更多你可以阅读这里


Check this extremely simple html structure. One div inside another, the inner (green) div has margin-top: 100px, the outer (red) div has no margin-top. The outer div is twice (200px) as high as the inner div (100px).

So what would you expect the result to look like? If you're like me, you get the opposite of what you'd naturally expect to get.

My question is NOT how to prevent this. My question is WHY ist this happening this way consistently across all browsers? I mean, this obviously must be the way it's inteded to work, but to me it's strongly counter-intuitive.

.red {
    background-color: #a00;
    height: 200px;
}

.green {
    background-color: #0a0;
    margin-top: 100px;
    height: 100px;
}

<div class="red">
    <div class="green"></div>
</div>

http://jsfiddle.net/connexo/7txnoj7m/

解决方案

Its normal way of rendering and it is called margin collapsing. More you can read here

这篇关于内部div不尊重边缘顶部相对于父div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆