Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does margin-bottom show as margin-top?

Tags:

html

css

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:

enter image description here

What I get:

enter image description here

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?

like image 929
Minh Hoang Avatar asked Mar 09 '16 09:03

Minh Hoang


People also ask

Does setting margin-top and margin-bottom have an?

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.

How do I get rid of the top margin in HTML?

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 .

What does bottom margin mean?

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.

How does margin-bottom work?

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.


2 Answers

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

like image 179
ketan Avatar answered Oct 11 '22 14:10

ketan


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)

like image 38
Pete Avatar answered Oct 11 '22 14:10

Pete