Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner div not respecting margin-top relative to parent div [duplicate]

Tags:

html

css

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/

like image 985
connexo Avatar asked Nov 19 '25 13:11

connexo


1 Answers

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

like image 118
Glorifind Avatar answered Nov 22 '25 04:11

Glorifind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!