Categories
css html margin overflow

CSS margin terror; Margin adds space outside parent element [duplicate]

194

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...

Source:

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

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

</head>

<body>

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

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.

6

  • Did you start with a CSS reset?

    Nov 26, 2012 at 21:46

  • I use Normalizer.css. But that is not the issue. If i remove the h1-tag. There is no problem. Problem is that i want a margin on my h1-tag and that is affecting the surrounding elements.

    Nov 26, 2012 at 21:48

  • 1

    @DavidThomas I’m playing with a demo at the moment if you want to fiddle jsfiddle.net/5RhFq/6

    – Calvin

    Nov 26, 2012 at 21:55

  • 2

    never mind.. j08691 has solved it 🙂 jsfiddle.net/5RhFq/7

    – Calvin

    Nov 26, 2012 at 21:56

  • 1

    The explanation and some possible solutions were already covered in my answer to your previous question…

    – BoltClock

    Nov 28, 2012 at 6:16

257

Add overflow:auto to your #page div.

jsFiddle example

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

5

  • 4

    That works. Found loads of examples here ( seifi.org/css/… ) aswell and some good explaining. No solution is really 100% satisfying. But I guess one just have to live with it, margin must work this way or text formatting would be impossible. 99% of time it works as needed. But every now and then comes the issue when designing a layout. 😛

    Nov 26, 2012 at 22:38

  • 8

    overflow:hidden; also worked for me and worked better for me in my scenario.

    – stuyam

    Dec 21, 2014 at 15:24

  • 1

    you a magincian. this was really weird behavior

    – luky

    Dec 6, 2018 at 16:33

  • 4

    Collapsing margins look incredibly complicated to define (many rules, many exceptions), what’s it use?

    – estani

    Nov 26, 2019 at 11:29

  • 1

    display: flex with flex-direction: column on parent container does the same trick.

    Dec 6, 2020 at 7:32

59

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

1

  • 5

    clear: <any option> does not seem to work, the other solutions work on Google Chrome

    – Ferrybig

    Jan 22, 2019 at 12:54

22

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

Full CSS

#page {
    margin:0;
    background:#FF9;
    display:inline-block;
    width:100%;
}

See Fiddle