Why should we use 100% height in both html and body tag but not one of them | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Why should we use 100% height in both html and body tag but not one of them

I am trying to make fullscreen background but when i put height: 100% in body, not thing happen. This also happen when i put it in html tag. But when i put it in both html and body, it work!. I dont know why. Can someone explain why ? Thank you so much!

2nd Feb 2022, 3:17 AM
HoƠng Văn Phi
HoƠng Văn Phi - avatar
1 Resposta
+ 1
In order for an element to have a relative height (%) itā€™s parent element also needs to have a height defined. Then, if you set the child element to, lets say, height: 50%; The height will be 50% of itā€™s parent. Since the html tag is the root of the document, you can set it to 100% of the ā€˜screenā€™ height. The following code is valid & will set a background-color on the entire screen: html { height: 100%; width: 100%; background-color: green; } But assuming that the body element will have content, itā€™s ideal to set a background to the body as well.
2nd Feb 2022, 3:27 AM
Moshe Schnitzler
Moshe Schnitzler - avatar