I try to add margin-bottom on a div inside parent but it show as parent's margin-top. Why?
HTML:
<div id="wrapper">
<div id="content">
<div id="box"></div>
</div>
</div>
CSS:
#wrapper{
border: 1px solid #F68004;
}
#content{
background-color: #0075CF;
height: 100px;
}
#box{
margin-bottom: 50px;
}
What I expected:
What I get:
Update:
I can fix this but i have no idea why that code not work? can someone explain?
Update 2:
It look like most answer say that it because of margin collapse and my question duplicate with this. But please note that i set margin-bottom but NOT margin-top. I also read about collapsing margins and i can not found any rule say that margin-bottom can become margin-top. Can anyone point me out?
Top and bottom margins do not affect inline elements because inline elements flow with content on the page. You can set left and right margins/padding on an inline element but not top or bottom because it would disrupt the flow of content.
Adjusting the Margin Size of an HTML Element With CSS You can remove this margin by setting the top and left margin to zero. Like the padding and border, the sizes of specific sides of the margin can be set using margin-left , margin-right , margin-top , and margin-bottom .
The margin-bottom property is used to specify the width of the bottom area of the element's margin box. That is, it specifies the area of space required at the bottom of the element, outside any defined border. Every element on a web page is rectangular.
The margin-bottom CSS property sets the margin area on the bottom of an element. A positive value places it farther from its neighbors, while a negative value places it closer.
Give margin-bottom: 50px;
to #content
instead of #box
Because you have given height to #content
div and here margin collapse.
Here it is explain with example
Updated Fiddle
The reason why the margin appears to be on top is due to margin collapsing:
Parent and first/last child
If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block with the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.
If you add add a transparent border to your parent div (#content
) then your margin will behave:
#wrapper{
border: 1px solid #F68004;
}
#content{
border:1px solid transparent;
background-color: #0075CF;
height: 100px;
}
#box{
margin-bottom: 50px;
height:10px; background-color:red;
}
<div id="wrapper">
<div id="content">
<div id="box"></div>
</div>
</div>
If you want the white space at the bottom like in your expected image, just add padding-bottom:50px
to #wrapper
Update
Why the margin-bottom
is causing margin-top
: As the collapsing margin is moving outside your parent div, it becomes margin bottom of the element outside the parent (which is the top border of #wrapper
) - which pushes your #content
div down (making it look like margin-top)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With