There are some good answers here but also some misunderstandings.
I support the following settings for modern browsers when styling for dimensions in web pages:
html {
height: 100%; /* fallback for IE and older browsers */
height: 100vh;
}
body {
height: auto; /* required to allow content to expand vertically without overflow */
width: auto;
min-height: 100%; /* fallback for IE and older browsers */
min-height: 100vh;
margin: 0;
padding: 0;
}
For starters, the new viewport units ("vh") are redundant and not necessary as long as you have set html
to a height of 100%
. The reason is the body
derives its values from the html
parent. You can still use "vh" units in body
to bypass the parent and get its property dimensions directly from the viewport. But its optional if html
has 100%
height.
Notice I've set body
to height:auto
. You do NOT want to set body
height
and width
to 100%
, or specific values as that limits content to the viewport's dimensions and there will be overflow. height:auto
is your best friend in CSS! Using overflow:auto
properties are not needed if you set height:auto
on the body
. That tells the page to let content expand height to any dimension necessary, even that beyond the viewport's height, if it needs to. It will not break out of the body
dimensions. And it does allow scrolling as needed. auto
also allows you to have margins and still support pages that fill the viewport using min-height
. I believe height:auto
is the default on body in most UA browser style sheets, anyway.
Adding min-height:100%
then gives you the default height you want body
to have and then allows the page dimensions to fill the viewport without content breaking out of the body. This works only because html
has derived its height:100%
based on the viewport.
The two CRITICAL pieces here are not the units, like %
or vh
, but making sure the root element, or html
, is set to 100% of the viewport height. Second, its important that body have a min-height:100%
or min-height:100vh
so it starts out filling the viewport height, whatever that may be. Nothing else beyond that is needed. Notice I have added "fallback" properties for min-height, as many browsers post-2010 do not support "vh" units. Its fun to pretend everyone in the web world uses the latest and greatest but the reality is many legacy browsers are still around today in big corporate networks still use antiquated browsers that do not understand those new units.
STYLING HEIGHT FOR LEGACY BROWSERS
One of the things we forget is many very old browsers do not know how to fill the the viewport height correctly. Sadly, those legacy browsers simply had to have height:100%
on both the html
element and the body
. If you did not do that, browser background colors and other weird visuals would flow up under content that did not fill the viewport. We solved that by delivering these 100% values only to older user-agents. If you are testing on a legacy browser, keep that in mind.
html {
height:100%;
}
body {
height:100%;
width:auto;
margin:0;
padding:0;
}