html – CSS margin terror; Margin adds space outside parent element-ThrowExceptions

Exception or error:

My css margins doesn’t behave the way I want or expect them to. I seems like my header margin-top affect the div-tags surrounding it.

This is what I want and expect:
What I want....

…but this is what I end up with:
What I get...


<!doctype html>
<meta charset="utf-8">
<title>Margin test</title>

<style type="text/css">
body {
#page {
#page_container {
    margin:0 20px;
h1 {
    margin:50px 0 0 0;



<div id="page">
    <div id="page_container">
        <header id="branding" role="banner">
                <h1 id="site-title"><span><a href="#" title="Title" rel="home">Title</a></span></h1>
                <h2 id="site-description">Description</h2>

I have exaggerated the margin in this example. Default browser margin on h1-tag is somewhat smaller, and in my case I use Twitter Bootstrap, with Normalizer.css which sets default margin to 10px. Not that important, main point is; I can not, should not, want not change the margin on the h1-tag.

I guess it is similar to my other question; Why does this CSS margin-top style not work?. Question is how do I solve this specific issue?

I have read a few threads on similar problems, but haven’t found any real answers and solutions. I know adding padding:1px; or border:1px; solves the problem. But that only adds new problems, since I do not want a padding nor a border on my div-tags.

There must be a better, best practice, solution? This must be pretty common.

How to solve:

Add overflow:auto to your #page div.

jsFiddle example

And check out collapsing margins while you’re at it.


Add any one of the following rules:

float: left/right;
position: absolute;
display: inline-block;
overflow: auto/scroll/hidden;
clear: left/right/both;

This is caused by collapsing margins. See an article about this behavior here.

According to the article:

The W3C specification defines collapsing margins as follows:

“In this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding, or border areas, or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin.”

This is also true for parent-child elements.

All the answers include one of the possible solutions:

There are other situations where elements do not have their margins collapsed:

  • floated elements
  • absolutely positioned elements
  • inline-block elements
  • elements with overflow set to anything other than visible (They do not collapse margins with their children.)
  • cleared elements (They do not collapse their top margins with their parent block’s bottom margin.)
  • the root element


Problem was the parent not taking into account children for height. Adding display:inline-block; did it for me.

Full CSS

#page {

See Fiddle


Just add border-top: 1px solid transparent; to your #page element.


Two margins are adjoining if and only if:
– no line boxes, no clearance, no padding and no border separate them


The solutions in the other answers didn’t work for me. Transparent borders, inline-block, etc., all caused other problems. Instead, I added the following css to my ancestor element:

  content: "";
  display: inline-block;
  clear: both;

Depending on your situation, this may cause its own problems because it adds extra space after the last child element.

Leave a Reply

Your email address will not be published. Required fields are marked *