One common reason for encountering this issue is often due to neglecting to set height: 100%
for both html and body elements.
It's important to remember that percentage is a relative unit that requires a parent element with a declared absolute height/width in order to work properly. Additionally, if the parent element has height/width specified in percentage, that calculated value will be used as the basis for determining the percentage for its children.
When setting the height as 100%
, be mindful of the box-sizing
property, as adding margins and paddings to the element may cause it to exceed the 100% height of its parent.
For example, if your markup looks like this...
<html>
<body>
<div class="my-element"></div>
</body>
</html>
If you still prefer to use percentage as a length value, you can do so by:
html,
body,
.my-element {
height: 100%;
}
Alternatively, consider using the vh
length unit for a better approach:
.my-element {
height: 100vh;
}
Learn more about the vh
length unit here.
For information on browser compatibility, visit http://caniuse.com/#feat=viewport-units.