问题
Given a sticky footer such as that on Ryan Fait's site with a fixed pixel height, is it possible to center, both horizontally and vertically, variable-size content in the space above this footer?
回答1:
I would suggest looking at Bobby van der Sluis's article on Footers at A List Apart.
Example #7 at the end of his article shows a vertically centered block. It does rely on scripting, but it is truly minimal.
edit
You can also use a single-cell table to accomplish vertical centering. Incorporating it with Ryan Fait's sticky footer would give you something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<style type="text/css">
/* Original Sticky Footer: http://ryanfait.com/sticky-footer/ */
html, body {
margin: 0;
height: 100%;
width: 100%;
}
#footer {
margin-top: -150px;
height: 150px;
}
#footer {
background: #bbd;
}
.block {
width: 300px;
padding: 20px;
background: yellow;
margin: 0 auto 150px; /* height of #footer */
}
</style>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td>
<div class="block">
<h1>Vertically Centered!</h1>
<p>This block will remain centered. Just needs that one table cell wrapping.</p>
</div>
</td></tr>
</table>
<div id="footer">Footer Content here</div>
</body>
</html>
回答2:
well, then you could set this for the vertical align of content:
.verticalalign{
width:270px;
height:150px;
position:absolute;
left:50%;
top:50%;
margin:-75px 0 0 -135px;
}
来源:https://stackoverflow.com/questions/7909587/horizontal-and-vertical-centering-above-a-sticky-footer-in-css