Avoid using percentage heights in CSS.
Referring to this answer on Stack Overflow:
In order for a height set in percentage to work, the parent element(*) must have a specific height defined. If the parent's height is left as auto, the block will adjust its height based on its content... but if the content itself has a height specified in percentage relative to the parent, it creates a bit of a dilemma. The browser then defaults to the content height.
Therefore, the parent of the div must have an explicit height property. Even if that height is also in percentage, it simply shifts the problem to the next level.
If you want the div's height to be a percentage of the viewport height, every ancestor of the div, including and , needs to have height: 100%, creating a hierarchy of explicitly defined percentage heights leading down to the div.
This means that the parent element must have a specific height (which is not the case here). This is because the default behavior for the height of a block
element is to match the content's height, while the width
defaults to 100%. That's why setting width: 100%
works.
Therefore, to achieve a similar effect as width:100%
, all parent elements must be set to height: 100%
(including the body
and html
elements).