2

In CSS stylesheet, I often see this rule:

html, body {
  margin  : 0px;
  padding : 0px;
}

Why to put a zero margin on <html> element? Why not just on the <body> element?

Related question : Does the <html> element have a default margin or padding in any browser, since normalize.css doesn't reset it?

Community
  • 1
  • 1
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240

6 Answers6

1

Because browsers have their own default styles for margin and padding if no styles are set on these elements. Resetting those elements to 0 will consistently keep them the same.

Paul Redmond
  • 3,276
  • 4
  • 32
  • 52
1

According to W3s default stylesheet (http://www.w3.org/TR/CSS2/sample.html) the html tag should not have any margin or padding and there for it should not be necessary to reset the margin or padding on the html tag.

But I guess there could be some browser out there that does not follow those guidelines and therefore needs a reset on the html tag.

I am guessing people leave that reset for the html tag because of old habit.

Filip
  • 2,514
  • 17
  • 28
0

If you are reseting your default body margin which is 5 or 7px according to different browsers then make it right. Reset the body, and sometimes (like the PSP browser) even the html has a default margin which means that body will not be enough. But nowadays the main reason is that when you want a full reset you just act for the sake of pure clearance in your code, and if you only reset the body people will say hey but why the html is not cleaned, so just clean the html so now you are totally sure you have a margin of 0 and any coder that sees your code will say ok body and html are at 0 so its at 0 and hurra its god damn 0 :)

Valentin Mercier
  • 5,256
  • 3
  • 26
  • 50
  • Are you sure that PSP browser put a non zero `` margin? Because normalize.css would not fix that. – Ortomala Lokni Jul 01 '14 at 10:22
  • The PSP browser is by far not standard conformant. For having developed on it i can tell that it is very difficult to understand its logic. Yes the html has a default margin, and for instance the
    tag does not even work
    – Valentin Mercier Jul 01 '14 at 10:30
0

By removing the default styles set by the browser you get a "clean start" for implementing your styles. Actually most of the people reset "all" default styles set by the browser, but I guess you know that. :) I always use a full reset- to make sure I wont have to fight with the default styles later.

Normalize css http://necolas.github.io/normalize.css/

Reset css (I use this one) http://meyerweb.com/eric/tools/css/reset/

  • normalize.css doesn't reset `` margin and padding. Why? – Ortomala Lokni Jul 01 '14 at 10:35
  • That's why I posted both links. The difference between the two is that reset css just puts everything to zero and normalize tries to improve consistency in rendering between different browsers. Many people see reset css as a bit too much, but I feel that I do a lot of extra stuff manually when using normalize. Both tools serve a similar but different purpose and the choice is up to you. :) – vazicao Jul 01 '14 at 10:46
-1

You could display or style any HTML element in CSS. It is theoretically possible to display the HTML element itself as well.

Another reason is inheritance. Many values get inherited or can be inherited manually. If you set a global margin to the HTML element, you could just inherit that value e.g. on the BODY element.

Example to display the TITLE element contents:

head, title {
    display: block;
}

http://jsfiddle.net/feeela/YmamL/

But all in all you should not use a reset.css anymore. Use a normalize stylesheet that only set those values that differ from browser to browser. The most famous normalize.css currently is probably that of Nicolas Gallagher. This one also does not reset a margin or padding for the HTML element.

feeela
  • 29,399
  • 7
  • 59
  • 71
  • 1
    Not all HTML elements can be displayed. Could ` – Ortomala Lokni Jul 01 '14 at 09:55
-1

every browser have a default value for margin if we do not, and sometimes when we are coding in html we do not use <html> and <body> those css declaration is like a catch :)

wrecklez
  • 343
  • 2
  • 4