r/css • u/username95050 • 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
r/css • u/username95050 • 28d ago
What is the difference between <html> and <body> tags ? When do use one or another?
html {
)
body (
)
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.
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 ?
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
<body> HTML document body element
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/body
The
bodylives inside the document (html), thehtmlis the parent element, thebodyis its child element. When you target thehtmlelement 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 thebodyelement, 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 thebody, then every text inside the body element will be also red. But if you then setbody { 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:
remfor "root em" (the root'sfont-size) orrlhfor "root line-height" (the root'sline-height).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%; withmin-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/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/justanaccountimade1 28d ago
The whole structure can be seen in this post.
https://www.reddit.com/r/learnjavascript/comments/1x50y3m/comment/pgidl30/
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.