I have a CSS textbook, and there is an example to explain the problem of overlapping margins:
Effect:
<div style="margin:10px; background:red;"> <p style="margin:20px; background:yellow;">aaaaaaa</p></div>
1. It is the height of div, which is equal to the height of p
2. If you can’t understand it this way, use Borders or padding to limit the scope of child elements can eliminate overlap.
Your book’s explanation of content height and margin overlay is biased.
Margin overlay, w3c's description is:
For block-level elements (display:block), the upper and lower borders of unfloated vertically adjacent elements will be compressed.
An easy-to-understand example is: there are two elements, upper and lower. The lower border of the upper element is 5px, and the upper border of the lower element is 20px. Then the actual distance between the two elements is 20px (the larger of the two border values) value).
This is a situation where adjacent elements also include parent-child elements, just like in your example.
At this time, it actually has nothing to do with the height of the child element, but because the parent element has no padding or border, so the margin of the child element will go outside the content area of the parent element.
I don’t know what book you are reading. Just give me this example. I suggest you read it in another book.
Learned, very good. . . .
1. The height of the red part is the height of the div content area, and it is also the height of the p content area. Because you have not set the height attribute of the div, by default the height of the div will be reduced to just enough to accommodate the child elements. ;
2. Even if you explicitly set the width of the div, if you do not set a vertical border or padding for the div or p, then even if the div has enough height to accommodate the entire height of the p (including margin etc.), margin overlap will still occur, because the upper margin of p coincides with the upper margin of div. Don’t ask why, the CSS2.1 standard stipulates this: without vertical borders or padding, block-level boxes under regular flow will have overlapping margins, brother boxes, parent-child boxes, and even themselves can have margins. coincide.
For this knowledge, you can go to W3school to learn it.
The internet at home was disconnected yesterday, thank you for your help! ! !