Let's say you have two <p>
elements, one stacked on top of the other, and each with 3rem of top and bottom margin.
With this set up, I expect there to be a total of 6rem of white space between the two <p>
elements (3rem from the top <p>
's bottom margin, plus 3rem from the bottom <p>
's top margin). However, there is only 3rem of white space between the elements.
Why does the margin space collapse between the block elements instead of each block element holding on to its own margin space?
Here is a codepen that illustrates the problem of collapsed margin space between two paragraphs.
The html of the above codepen example is:
<p class="outline-red">This is a paragraph with top & bottom margin and padding set at 3rem.</p>
<p class="outline-yellow">This is a paragraph with top & bottom margin and padding set at 3rem.</p>
The CSS of the above codepen example is:
body {
margin: 0;
padding: 0 3rem;
background-color: #34495e;
font-family: "Helvetica", sans-serif;
text-align: center;
color: white;
}
.outline-red,
.outline-yellow {
display: block;
margin-top: 3rem;
margin-bottom: 3rem;
padding-top: 3rem;
padding-bottom: 3rem;
}
.outline-red { border: 1px solid red; }
.outline-yellow {border: 1px solid yellow; }