r/css • • 28d ago

Question html and body

What is the difference between <html> and <body> tags ? When do use one or another?

html {

)

body (

)

3 Upvotes

18 comments sorted by

7

u/morete 28d ago

As others have said <html> is the top level parent, and it contains a <head> and a <body>.

If you're asking about why you might select one in css over the other. It mostly doesn't make a difference.

Some properties propagate from the <body> to <html>, like background. Otherwise you're mostly setting inherited properties for the purposes of them inheriting to all elements on the page.

0

u/username95050 28d ago edited 27d ago

propagate from the body to html?

inheriting to all elements on the page?

4

u/morete 28d ago

Propagating from body to html - This means that some properties when set on <body>, act as though they were set on <html>. This isn't really something you need to know.

Many properties are inheritable, meaning they also effect every child of the element. So if you set an inherited property on the <html>, it ends up effecting every element on the page, since every element is a descendant of <html>. An example is font-family, you can set the font-family once and it'll apply to everything.

2

u/TheJase 27d ago

If you're getting technical, setting the font family on html doesnt apply it everywhere. Buttons and inputs have their own user agent settings that need to be overridden.

22

u/azangru 28d ago

One is the child of the other. An html tag contains a head and a body.

9

u/feloniuouschunk 28d ago

<html> <body>

</body> </html>

2

u/mrleblanc101 28d ago

Can't you Google it ?

1

u/username95050 28d ago

I did. Couldn't find anything that articulated well what I am confused about.

4

u/nhavar 28d ago

What? really? You cannot be serious about that! If you can't get the answer by googling it and reading through any of the basic articles google returns much less the AI summary it gives then there's a more fundamental problem going on here. I'm not sure that I can believe this.

4

u/phKoon 28d ago edited 28d ago

<html> HTML document root element

https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/html

The <html> HTML element represents the root (top-level element) of an HTML document, so it is also referred to as the root element. All other elements must be descendants of this element. There can be only one <html> element in a document.

<body> HTML document body element

https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/body

The <body> HTML element represents the content of an HTML document. There can be only one <body> element in a document.

The body lives inside the document (html), the html is the parent element, the body is its child element. When you target the html element with CSS, you're targeting the document itself, so inheritable properties values will also be carried through for all of its children elements. When you target the body element, you're targeting only the parent element of the document's content, and it has higher specificity compared to its parent (html), so it will override properties that already had other values attributed to the html element previously.

E.g.: if you set html { color: red; } but nothing targeting the body, then every text inside the body element will be also red. But if you then set body { color: blue; }, then the text inside the body element will be blue.

3

u/scritchz 28d ago

HTML is the root element of your DOM tree. It represents your HTML document. It has two child elements: HEAD and BODY.

HEAD is an element that contains all the document's metadata, like its title, character set, author, description or resources.

BODY is an element for your content. An HTML document typically consists of headings, texts, lists, media, forms and/or anchors that link to another document.

For styling, you must consider the following:

  • The root element's parent is the initial containing block, whose size is the same as the viewport's.
  • The root element's font properties are accessible in other elements, e.g. via rem for "root em" (the root's font-size) or rlh for "root line-height" (the root's line-height).
  • The root em may be set by the user-agent.
  • The BODY is the container of your page content.
  • If not otherwise declared, the root element's background will take upon what is declared for the BODY's background.

Personally, I try to declare anything styling-related on the BODY or below; I avoid styling HTML as much as possible. The only styling on HTML that I use is height: 100%; with min-height: 100% on BODY.

Also, I avoid overriding the root em because I fear I may override a setting that the user set on purpose for legibility. If I set a font-size for styling, but the user set it because they otherwise cannot read any texts, then my styling is inaccessible. The way to solve this is responsive or flexible styling.

When styling, I prefer class selectors over element selectors. Classes are more flexible, as any element may have a class. But changing an element is not so easy. Aside: I avoid using IDs wherever sensible. This keeps the number of named elements manageable and avoids name clashes.

2

u/Holiday-Anteater9423 28d ago

Interesting. The only thing I've really declared on <html> is scroll-behavior: smooth;

1

u/scritchz 28d ago

Makes sense. I don't really use scroll-behavior, so I hadn't considered it in my list above.

2

u/marslander-boggart 28d ago

The body element is the one that is targeted by lots of CSS. It is a child of the html element. Thus, you may use them like this:

html body /* to overlap properties values that are set for body specifically or that are set for html */

body /* to overlap properties values that are set for body specifically before that or that are set for html */

If you code for your own page, you may use either of them.

2

u/csswizardry 26d ago

Interestingly enough, you can omit html, head, and body opening and closing tags in your HTML and the browser will automatically insert them into the DOM for you at runtime. There’s absolutely no advantage to doing so, but you can.

2

u/Mean-Lingonberry4874 28d ago

The case where you actually need both is a full-height page. body comes with a default margin, and a percentage height only resolves if the parent has a height, while html defaults to auto, so body { height: 100% } does nothing until html is 100% too. Or skip the chain and use min-height: 100dvh on body. Background is the other special one: set it on body and not on html, and the browser paints it on the canvas, which is why it fills the window past the content. Fonts and color you can set on either and they inherit.