Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS properties to both the "html" and "body"

Tags:

css

I've seen this quite a few times now. When people want to assign a CSS property to the whole window/document, they sometimes do

html, body {
   myCSSProperty: someValue;
}

For example, see the answer I accepted here, or see this article.

I am wondering if assigning CSS properties to both html and body is to overcome browser bugs, required for all browsers, a purely psychological thing, a common misconception or misunderstanding of the tags html and body, or something else.

I would be glad if someone could demystify the situation for me, separating the cases where CSS properties need to be assigned to html or body, or both, with specific examples and explanations.

like image 542
Randomblue Avatar asked Nov 14 '22 15:11

Randomblue


1 Answers

This article has great information about the <html> and <body> tags in terms of CSS. The short of it is this (taken from the top of the article):

  • The html and body elements are distinct block-level entities, in a parent/child relationship.
  • The html element's height and width are controlled by the browser window.
  • It is the html element which has (by default) overflow:auto, causing scrollbars to appear when needed.
  • The body element is (by default) position:static, which means that positioned children of it are positioned relative to the html element's coordinate system.
  • In almost all modern browsers, the built-in offset from the edge of the page is applied through a margin on the body element, not padding on the html element.
like image 66
Michael Irigoyen Avatar answered Dec 25 '22 03:12

Michael Irigoyen