Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Cascading Style Sheets _ html vs body tag for setting margin 0

Posted by: mp3909 May 5 2020, 12:00 PM

So I want to have zero margins on my body so I thought if i do the following it would work, but it didn't.

CODE
html {
    margin: 0;
}



But then i did this and it worked.

CODE
body {
    margin: 0;
}



I'm confused why it did not work when I used the html selector to set margins to zero.

Posted by: Christian J May 5 2020, 02:12 PM

Most browsers use a default margin for BODY, but not for the HTML element.

Here's a list of all(?) HTML elements, though I'm not sure how correct it is. Also browsers may use (or have used) different default styling: https://www.w3schools.com/cssref/css_default_values.asp

There also an official but less readable list for HTML4 elements here: https://www.w3.org/TR/CSS2/sample.html (haven't found an official one for HTML5 yet).

Posted by: pandy May 5 2020, 03:32 PM

QUOTE(Christian J @ May 5 2020, 09:12 PM) *

There also an official but less readable list for HTML4 elements here: https://www.w3.org/TR/CSS2/sample.html (haven't found an official one for HTML5 yet).


And not complete unless older browsers are ignored. Some browsers used to use padding on body, and maybe their offspring still do if in Quirks Mode, I don't know. I don't think any browser has applied styling to HTML though.

Posted by: mp3909 May 9 2020, 06:42 AM

ok, thanks guys. smile.gif

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)