I am currently working on a web application, where I want the content to fill the height of the entire screen.
The page has a header, which contains a logo, and account information. This could be an arbitrary height. I want the content div to fill the rest of the page to the bottom.
I have a header div
and a content div
. At the moment I am using a table for the layout like so:
CSS and HTML
#page {
height: 100%; width: 100%
}
#tdcontent {
height: 100%;
}
#content {
overflow: auto; /* or overflow: hidden; */
}
<table id="page">
<tr>
<td id="tdheader">
<div id="header">...</div>
</td>
</tr>
<tr>
<td id="tdcontent">
<div id="content">...</div>
</td>
</tr>
</table>
The entire height of the page is filled, and no scrolling is required.
For anything inside the content div, setting top: 0;
will put it right underneath the header. Sometimes the content will be a real table, with it's height set to 100%. Putting header
inside content
will not allow this to work.
Is there a way to achieve the same effect without using the table
?
Update:
Elements inside the content div
will have heights set to percentages as well. So something at 100% inside the div
will fill it to the bottom. As will two elements at 50%.
Update 2:
For instance, if the header takes up 20% of the screen's height, a table specified at 50% inside #content
would take up 40% of the screen space. So far, wrapping the entire thing in a table is the only thing that works.
It's dynamic calc the remining screen space, better using Javascript.
You can use CSS-IN-JS technology, like below lib:
https://github.com/cssobj/cssobj
DEMO: https://cssobj.github.io/cssobj-demo/
CSS only Approach (If height is known/fixed)
When you want the middle element to span across entire page vertically, you can use
calc()
which is introduced in CSS3.Assuming we have a fixed height
header
andfooter
elements and we want thesection
tag to take entire available vertical height...Demo
Assumed markup
So your CSS should be
So here, what am doing is, adding up the height of elements and than deducting from
100%
usingcalc()
function.Just make sure that you use
height: 100%;
for the parent elements.There really isn't a sound, cross-browser way to do this in CSS. Assuming your layout has complexities, you need to use JavaScript to set the element's height. The essence of what you need to do is:
Once you can get this value and set the element's height, you need to attach event handlers to both the window onload and onresize so that you can fire your resize function.
Also, assuming your content could be larger than the viewport, you will need to set overflow-y to scroll.
HTML 5
CSS
Solution above uses viewport units and flexbox, and is therefore IE10+, providing you use the old syntax for IE10.
Codepen to play with: link to codepen
Or this one, for those needing the main container to be scrollable in case of overflowing content: link to codepen
2015 update: the flexbox approach
There are two other answers briefly mentioning flexbox; however, that was more than two years ago, and they don't provide any examples. The specification for flexbox has definitely settled now.
All major browsers and IE11+ support Flexbox. For IE 10 or older, you can use the FlexieJS shim.
To check current support you can also see here: http://caniuse.com/#feat=flexbox
Working example
With flexbox you can easily switch between any of your rows or columns either having fixed dimensions, content-sized dimensions or remaining-space dimensions. In my example I have set the header to snap to its content (as per the OPs question), I've added a footer to show how to add a fixed-height region and then set the content area to fill up the remaining space.
In the CSS above, the flex property shorthands the flex-grow, flex-shrink, and flex-basis properties to establish the flexibility of the flex items. Mozilla has a good introduction to the flexible boxes model.
it never worked for me in other way then with use of the JavaScript as NICCAI suggested in the very first answer. I am using that approach to rescale the
<div>
with the Google Maps.Here is the full example how to do that (works in Safari/FireFox/IE/iPhone/Andorid (works with rotation)):
CSS
JS
HTML