I want to make body have 100% of the browser height. Can I do that using CSS?
I tried setting height: 100%
, but it doesn't work.
I want to set a background color for a page to fill the entire browser window, but if the page has little content I get a ugly white bar at the bottom.
As an alternative to setting both the
html
andbody
element's heights to100%
, you could also use viewport-percentage lengths.In this instance, you could use the value
100vh
- which is the height of the viewport.Example Here
This is supported in most modern browsers - support can be found here.
A quick update
A better solution with today's CSS
Try setting the height of the html element to 100% as well.
Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have it's height set as well.
However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.
What I use on the start of literally every CSS file I use is the following:
The margin of 0 ensures that the HTML and BODY elements aren't being auto-positioned by the browser to have some space to the left or right of them.
The padding of 0 ensures that the HTML and BODY elements aren't automatically pushing everything inside them down or right because of browser defaults.
The width and height variants are set to 100% to ensure that the browser doesn't resize them in anticipation of actually having an auto-set margin or padding, with min and max set just in case some weird, unexplainable stuff happens, though you probably dont need them.
This solution also means that, like I did when I first started on HTML and CSS several years ago, you won't have to give your first
<div>
amargin:-8px;
to make it fit in the corner of the browser window.Before I posted, I looked at my other fullscreen CSS project and found that all I used there was just
body{margin:0;}
and nothing else, which has worked fine over the 2 years I've been working on it.Hope this detailed answer helps, and I feel your pain. In my eyes, it is dumb that browsers should set an invisible boundary on the left and sometimes top side of the body/html elements.
After testing various scenarios, I believe this is the best solution:
It is dynamic in that the html and the body element will expand automatically if their contents overflow. I tested this in the latest version of Firefox, Chrome, and IE 11.
See the full fiddle here (for you table haters out there, you can always change it to use a div):
https://jsfiddle.net/71yp4rh1/9/
With that being said, there are several issues with the answers posted here.
Using the above CSS will cause the html and the body element to NOT automatically expand if their contents overflow as shown here:
https://jsfiddle.net/9vyy620m/4/
As you scroll, notice the repeating background? This is happening because the body element's height has NOT increased due to its child table overflowing. Why doesn't it expand like any other block element? I'm not sure. I think browsers handle this incorrectly.
Setting a min-height of 100% on the body as shown above causes other problems. If you do this, you cannot specify that a child div or table take up a percentage height as shown here:
https://jsfiddle.net/aq74v2v7/4/
Hope this helps someone. I think browsers are handling this incorrectly. I would expect the body's height to automatically adjust growing larger if its children overflow. However, that doesn't seem to happen when you use 100% height and 100% width.