3

Given a div with a child element that have a top margin, why the margin is not inside the parent div but outside ?

HTML

<div><h1>Titre<h1></div>

CSS

h1 { margin: 20px; }
div { background-color: #DDD; }

cf http://jsfiddle.net/adt515ww/

Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
  • 1
    possible duplicate of [Margin on child element moves parent element](http://stackoverflow.com/questions/1762539/margin-on-child-element-moves-parent-element) – Weafs.py Jan 15 '15 at 22:21

1 Answers1

11

That's due to collapsing margins. To fix it add overflow:auto to your div:

div {
    background-color:#DDD;
    overflow:auto;
}

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272