I’ve got this simple HTML5 document:
<!doctype html>
<html>
<head>
<style type="text/css">
body { margin: 0; padding: 0; }
.parent1 { height: 50px; background: #00ff00; }
.parent1 > .child1 { height: 50%; width: 100%; background: #ffcc00; }
.parent2 { position: absolute; width: 100%; height: 25%; background: #ff0000; }
</style>
</head>
<body>
<div class="parent1">
<div class="child1">I'm 25px height (50% of 50px)</div>
</div>
<div class="parent2">I'm absolute positioned. My ancestors, body and html, are static... Where I'm getting the 25% from?</div>
</body>
</html>
There’s no height defined for html and body, so they use the default value auto (which means that their height is their content’s height). They aren’t positioned, so they are static.
div.parent1 is 50px height.
div.parent1 has a child element with a 50% height.
div.parent2 is positioned absolute with a height of 25%.
If I’m not mistaken, a height set with percentage only works if it’s parent has a height defined (that’s the case of div.child1).
Because div.parent2 is absolute, it’s height is not computed, so body and html height is 50px. That’s clear, but I don’t understand why div.parent2 25%’s height is working… Where does it take from? Its ancestors body and html are static… Window? Viewport?