我有一个简单的页眉,页脚和内容布局,其中每个这样的部分都有未知高度(内容)。目标是将页眉向上,页脚向下和内容置于中间,仅在需要时显示垂直滚动条。我可以通过下面列出的代码来处理页眉,页脚和滚动条,但我找不到如何将内容置于中间位置的方法。
这是一个小提琴: http://jsfiddle.net/mVh44/
<div id="container">
<div id="header">
<div id="headerText">
header header header
</div>
</div>
<div id="content">
center text here
</div>
<div id="footerPadding">
</div>
<div id="footer">
<div id="footerText">
footer footer footer footer footer footer footer footer footer footer
footer footer footer footer footer footer footer footer footer footer
</div>
</div>
</div>
html, body {
border: none;
margin: 0;
padding: 0;
background-color: transparent;
font-family: Verdana;
font-size: .9em;
color: black;
height: 100%; /* needed for container min-height */
}
div {
display: block;
overflow: hidden;
}
div#container {
position: relative; /* needed for footer positioning*/
margin: 0 auto; /* center, not in IE5 */
width: 100%;
height: auto !important; /* real browsers */
height: 100%; /* IE6: treaded as min-height*/
min-height: 100%; /* real browsers */
}
div#header, div#footer {
margin: 0;
width: 100%;
}
div#footer {
position: absolute;
bottom: 0; /* stick to bottom */
}
div#headerText, div#footerText {
padding: 1em 0;
text-align: center;
white-space: nowrap;
}
div#headerText {
border-bottom: .5em double gray;
}
div#footerText {
border-top: .5em double gray;
}
function myResize() {
$('#footerPadding' ).css('height', $('#footer').css('height'));
}
$(window).resize(function(){
myResize()
});
myResize();
答案 0 :(得分:1)
您可以使用CSS并更改HTML标记。
活生生的例子:http://jsfiddle.net/mVh44/12/
<强> CSS 强>
#header{
position:absolute;
top:0;
}
div#footer{
position:relative;
display:table-row;
}
#container{
padding: 48px 0 0 0; //48px from the header
display:table;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#content{
display:table-cell;
vertical-align:middle;
height:100%;
}
<强> HTML 强>
<div id="header">
<div id="headerText">header header header</div>
</div>
<div id="container">
<div id="content">
<div class="box">center text here</div>
<div class="box">center text here</div>
</div>
<div id="footer">
<div id="footerText">footer footer footer footer footer footer footer footer footer footer footer footer footer footer footer footer footer footer footer footer</div>
</div>
</div>
<强>的jQuery 强>
function myResize() {
var headerHeight = $('#header').height();
var windowsWidth = $(window).width();
$('#footerPadding').css('height', $('#footer').css('height'));
$('#container').css('padding-top', headerHeight + 'px');
$('#footerText').css('width', windowsWidth + 'px');
}