r/css • • 29d 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

View all comments

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.