CSS CSS Layout Basics Getting Started with CSS Layout Why Vertical Margins Collapse

normalize.css v6.0.0 doesn't set body to margin 0, the method suggested to resolve Margin Collapsing doesn't work

Hello all,

I downloaded the project files and develop in Sublime Text. While the Normalize.css used in the course was earlier version, the one I used was latest version, and surprisingly it doesn't set body to margin: 0. That's why there is still gap even after I applied two methods (1. set h1 to margin-top: 0, 2. set main-header to padding: 1em) suggested in the course. I tried to find what was problem and compared two versions of Normalize.css and found out it is because the latest one doesn't set body to margin: 0. I wonder what's the use to have the latest version of Normalize.css if it is not better than the previous version?

nico dev
nico dev
20,360 Points

Good finding, KT, thanks for sharing it!

You may want to read this article. It seems not only you are worried about this. It has been updated just a few days ago.

In that case, I am not sure if you have already learned about the DOM and Dev Tools, but this is what you can do in such cases (and similar ones):

Open Dev Tools (CTRL + SHIFT + I - in Win, Chrome).

Once you're in the Elements tab, inspect each one by clicking on say, body, h1, header, etc. You'll notice that when you click on body, like you said, there's a margin that's going off the header and everything you've coded there.

Why? Most probably, because of what you mentioned (the Normalize does not include it, don't know about the reason) and because of the user agent stylesheet (in other words, the default styling of the browser), which in this case is assigning a default margin to the body.

I guess the simpler way to solve it is 'defaulting' the (body) margin to 0 in your style.css. I tried it on my end and it worked wonders.

Let me know if it worked fine for you and/or if I wasn't clear on smth, or you have any other doubt.

NOTE: I know this may seem obvious, but if having resets like this on your normalize feels more comfortable (and organized!) for you, rather than in the style.css, you can go ahead and edit the normalize and add the body margin bit there.

Keep it up!