Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this CSS margin-top style not work?

Tags:

html

css

margin

I try to add margin values on a div inside another div. All works fine except the top value, it seems to be ignored. But why?

What I expected:
What I expected with margin:50px 50px 50px 50px;

What I get:
What I get with margin:50px 50px 50px 50px;

Code:

#outer {      	width: 500px;       	height: 200px;       	background: #FFCCCC;      	margin: 50px auto 0 auto;      	display: block;  }  #inner {      	background: #FFCC33;      	margin: 50px 50px 50px 50px;      	padding: 10px;      	display: block;  }
<div id="outer">    <div id="inner">    	Hello world!    </div>  </div>

W3Schools have no explanation to why margin behave this way.

like image 798
jamietelin Avatar asked Mar 01 '12 16:03

jamietelin


People also ask

Why is margin not working in CSS?

This happens because the div has no border, and no padding defined, and the CSS specification states to collapse the two top margins into a single one of the parent. (The same happens with bottom margins.)

How do you make a top margin in CSS?

The <p class="b"> element has a top and bottom margin of 20px. This means that the vertical margin between <p class="a"> and <p class="b"> should be 50px (30px + 20px).

Why margin is not working on anchor tag?

And on the a tag the margin doesn't work because it's an inline element. You may need to change its display property to inline-block or block .


1 Answers

You're actually seeing the top margin of the #inner element collapse into the top edge of the #outer element, leaving only the #outer margin intact (albeit not shown in your images). The top edges of both boxes are flush against each other because their margins are equal.

Here are the relevant points from the W3C spec:

8.3.1 Collapsing margins

In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

Adjoining vertical margins collapse [...]

Two margins are adjoining if and only if:

  • both belong to in-flow block-level boxes that participate in the same block formatting context
  • no line boxes, no clearance, no padding and no border separate them
  • both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
    • top margin of a box and top margin of its first in-flow child

You can do any of the following to prevent the margin from collapsing:

  • Float either of your div elements
  • Make either of your div elements inline blocks
  • Set overflow of #outer to auto (or any value other than visible)

The reason the above options prevent the margin from collapsing is because:

  • Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
  • Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
  • Margins of inline-block boxes do not collapse (not even with their in-flow children).

The left and right margins behave as you expect because:

Horizontal margins never collapse.

like image 177
BoltClock Avatar answered Oct 18 '22 22:10

BoltClock