The following code works properly
<!DOCTYPE html>
<html>
<head>
<title>Working</title>
<style type="text/css">
html {
height: 100%;
}
body {
height: 100%; /* !!!!! difference in here */
}
div#main {
min-height: 100%;
background-color: red;
}
</style>
</head>
<body>
<div id="main"></div>
</body>
while a code using min-height instead of height for body prevents #main from occupying space
<!DOCTYPE html>
<html>
<head>
<title>Not working</title>
<style type="text/css">
html {
height: 100%;
}
body {
min-height: 100%; /* !!!!! difference in here */
}
div#main {
min-height: 100%;
background-color: red;
}
</style>
</head>
<body>
<div id="main"></div>
</body>
Weird thing is when doing an inspect in chrome, in both cases body properly takes up 100% space. In min-height case however the child element min-height: 100%; translates to 0. Why does it happen and is there any workaround? I would like the min-height for my web page for background to work properly and expand as needed.
As to “Why does it happen?”
The
min-heightproperty only works if an explicit height is set. The spec reads for a percentage:The key point in that is that “If the height of the containing block is not
specified explicitly (i.e., it depends on content height).” When
bodyis set withmin-height, it is still depending on content height for calculation, only it does not allow it to fall below the specified size (in your case, 100% ofhtmlheight which is explicitly set). However, becausebodyis still depending on content to set its height, thendiv#maincannot calculatemin-heightbecausebodydoes not have an explicit height set.Is a “Workaround” Needed?
It seems that setting
bodytoheight: 100%achieves all you want. Whether content is long or content is short.Response to Comment on
htmlchange ofbackground-colorI suspect the reason your fiddle with
htmlhavingbackground-colorset changes things is because of the notations in this spec (quoted below). By setting that onhtmlit changes the canvas rendering itself: