I am trying to position a div
at the center of viewport.
Here's what I did so far.
HTML
<html>
<head>
<title>TEST</title>
</head>
<body>
<div id="header">Some header stuff here.</div>
<div id="wrapper">
<div class="spacer"><!-- avoid overlapping of content by header --></div>
<div id="content">Stuff to be centered horizontally and vertically.</div>
<div footer="spacer"><!-- avoid overlapping of content by footer --></div>
</div>
<div id="footer">Some footer.</div>
</body>
</html>
CSS
html, body{
height:100%; /*Setting height to 100% in order to make min-height:100% to work on wrapper.*/
}
#header{
height:70px;
width:100%;
position:fixed;
top:0;
left:0;
}
#wrapper{
min-height:100%;
width:100%;
margin-bottom:-70px; /*Equal to footer's height*/
}
#content{
/* Exact height is not known.*/
width:250px;
position:absolute;
top:50%;
left:50%;
margin:0px 0px 0px -125px; /* Top margin?? */
}
#footer{
height:70px;
width:100%;
position:relative;
bottom:0;
}
div.spacer{
height:70px;
}
Now I have two problems.
Since height is unknown, how to position the
div#content
to exact center because I can't set a negativemargin-top
.Even if the height of
#content
was known and settingmargin-top
could have worked, the method will fail as soon as the viewport's height is less than that ofdiv#content
's since header and footer will overlap#content
.
How to fix this?