With the following, there is a about 20px height gap at the beginning, I know h1 has default margin, and it seems div decides to respect that margin, I’m not sure the reason, I remeber the CSS part of the learn web devlopment does explain margin collposing when two adjacent siblings touchs, but what about parent and child? Another stange thing is, if I add a border to the div, then the gap disappers, it seems it decides no longer to respect the margin of h1, I don’t understand the reason.
Thanks for the reply. I have a maybe related quesiton about background-color in the box mode.
background-color applys to the content and padding box, right? So if both height of content and padding box are 0, the background-color will not show, but this is not true in the following case, since body is empty so it’s 0 height, but the background color fills the whole window, why?
for background it’s not always apply to content and padding there are property that can make it apply for only content also i forget the property name right now but i will try to check it
but without that property you are right it apply to both
the body has width and height default to initial if you inspect your code you wll see the width and height
just a general rule if you did not use a property with value that does not mean that this property does not have default value
for example if you did not specify the font family then the browser will pick the user setting and if not exist then it will apply it’s default
for background it’s not always apply to content and padding there are property that can make it apply for only content also i forget the property name right now but i will try to check it
but without that property you are right it apply to both
the body has width and height default to initial if you inspect your code you wll see the width and height
it says the body has 1422x0, no content, no padding, 1px border, 8px margin, it’s expected since the body is empty, it seems the body element is unusual, the background of the body always fulfills the window, ignoring border and margin.