Without knowing how high the inside div
's height will be, and that the height will be variable, it's very difficult to create a one-size-fits-all solution, especially considering that different devices and computers have different screen sizes and resolutions. Added to this problem is that your header and footer may also be of variable height, so that also complicates things.
To get around these quirks, and have better control of your varying div
height and where it sits on the page, I'd consider using JavaScript to control its position. This way, you can monitor when its height changes (depending on what you're planning on doing), and move it dynamically depending on its current height.
In the following example, I'm using jQuery to get the JavaScript done easily. You can download from their site here, and just include it in your code.
Here's a basic example (HTML first):
<div id="header"></div>
<div id="container"></div>
<div id="footer"></div>
Then some CSS:
html, body {
height: 100%;
min-height: 400px; /* to cater for no element 'collision'... */
margin: 0;
padding: 0;
position: relative; /* to keep header and footer positioned correctly... */
}
#header {
background-color: green;
height: 65px;
position: absolute;
top: 0;
left: 0;
width: 100%;
}
#container {
background-color: red;
width: 600px;
height: 40%;
position: absolute;
left: 50%;
margin-left: -300px; /* 50% over, then move back 300px to center... */
}
#footer {
background-color: orange;
height: 65px;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
}
And finally, your JavaScript. Put this in the <head>...</head>
section of your HTML. I've tried to comment as much as possible, but please let me know if anything's unclear:
<script src="./path/to/jquery.js"></script>
<script>
// this will run this function as soon as the page is loaded...
$(function(){
// bind the resize event to the window...
// this will call the 'centerMyContent' function whenever the
// window is resized...
jQuery(window).bind('resize', centerMyContent);
// call the 'centerMyContent' function on the first page load...
centerMyContent();
});
// the actual centring function...
function centerMyContent(){
// get the body and container elements...
var container = jQuery('#container');
var body = jQuery('body');
// work out the top position for the container...
var top = (body.height() / 2) - (container.height() / 2);
// set the container's top position...
container.css('top', top + 'px');
}
</script>
I hope this helps get you on the right track! Successfully tested in Chrome, Firefox, and Internet Explorer 7, 8, 9 and 10.