Consider the following HTML/CSS:
<div>
<p>Paragraph #1</p>
<p>Paragraph #2</p>
<p>Paragraph #3</p>
</div>
div {
background-color: gray;
padding: 0;
}
p {
margin-top: 1em;
margin-bottom: 1em;
}
The containing div
has no padding, so the top margin of p
#1 and the bottom of p
#2 are not respected, effectively zero:
But now, if we introduce just a smidge of padding to the containing div
, these margins are suddenly respected:
div {
padding-top: 1px;
padding-bottom: 1px;
}
Question:
- Why does this behavior make sense? Why do the leading/trailing margins depend at all on the padding? Why don't they simply appear all the time, or not at all?
- Is this behavior standard? It seems fairly consistent cross-browser (IE9, Firefox 69.0.3, Chrome 77.0.3865.120).