html - Ask about margin-top
高洛峰
高洛峰 2017-05-19 10:47:56
0
3
1893
<p >
    <h1 style="margin-top:100px">aa</h1>
</p>

I have a question that I don’t understand.
In 1.html, p wraps an h1, and h1 has a margin-top. Why does the margin-top of h1 burst outside of p instead of implementing margins inside p?
2. How to implement it in p. Thank you.

Code Demo
Link Description

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
小葫芦

This question has been asked many times, let me give you a reference
MDN-collapsing margins
Collapsing margins

PHPzhong

According to jasonintju’s answer, I will sort it out:
Because the margin-top of my child element is larger than the parent element, I will use the margin-top of the child element directly

Block-level parent element and its first/last child element
If the block-level parent element does not have the four attributes of top border, top padding, inline content, and clear float (for top border and top padding, you can also Say, when the top margin and top padding width are 0), then the top margin of this block-level element and its first child element can be said to be "close to each other." At this time, the upper margins of the block-level parent element and its first child element will merge. In other words, the margins displayed by the parent element at this time will directly become the parent element and its first child element. The larger of the margin-top of the child element.
Similarly, if the margin-bottom of the block-level parent element and the margin-bottom of its last child element are not separated by the border, padding, inline content, height, min-height, and max-height of the parent element, then Margin merging will occur.

Quote: https://developer.mozilla.org...

仅有的幸福

Just separate p and h1 and set padding or border on p

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template