So I have a div with a p inside at the top of current document. And I'm setting margin-top
onto the <p>
expecting there could be a margin between the div and p elememt. What makes me misunderstanding is the result that the margin-top
is shown between the body and the parent div. See the example.
But if I set the div.container
with a 1px border, the margin-top
could be brought back between the p element and its parent div.
What's more, if the div.container
is set as flex display instead of default block, the margin-top
could work intuitively as well.
How come? And can I do anything about it?
html, body {
padding: 0;
margin: 0;
background-color: grey;
}
.container {
background-color: red;
display: block; /* flex could also make it work */
margin: 0;
/* The border setting would bring back the margin-top on p */
/* border: 1px solid black; */
}
.paragraph {
margin-top: 100px;
}
<div class="container">
<p class="paragraph"> sadf </p>
</div>