固定标题&有自动高度,滚动内容的页脚?

时间:2011-11-22 18:41:38

标签: jquery html css

我想布置一个网格,其中包含一个始终可见的固定位置页眉和页脚以及一个内容元素,该内容元素会在内部滚动条的情况下展开以适应容器高度的其余部分。

<div id="container">
  <div id="header">Header Text</div>
  <div id="content">
    <div id="row1">Content</div>
    <div id="row2">Content</div>
    <div id="row3">Content</div>
    <div id="row4">Content</div>
    <div id="row5">Content</div>
    <div id="row6">Content</div>
    <div id="row7">Content</div>
  </div>
  <div id="footer">Footer Text</div>
</div>

如果我在#content上设置固定高度但是在更大的分辨率下,我可以正常工作,我希望#content填充空白区域。

另一个警告; #container,#header和#footer的高度未知。

jQuery是一种可能性。

编辑:这一点对我有用,改编自Senad的回答;

function resizeGrid() {
    $("div.items").innerHeight(0);
    $("div.items").innerHeight($(window).height() - $("body").innerHeight() - 22)
}

3 个答案:

答案 0 :(得分:5)

<强> CSS

#header { position: fixed; top: 0; left: 0; height: 100px; }
#footer { position: fixed; bottom: 0; left: 0; height: 100px; }
#content { margin-top: 100px;

<强> JS

$(document).ready(function(){  
   resizeContent();
  //attach on resize event
   $(window).resize(function() {
       resizeContent();
    });
});
function resizeContent()
{
   $('#content').attr('height', $(window).height() - $('#header').height() - $('#footer').height();
}

我希望这会对你有所帮助:

答案 1 :(得分:1)

#header,
#footer {
    width:100%;
    height:20px;
    background:#ccc;
    position:fixed
}

#header {top:0}
#footer {bottom:0}

html, body {height:100%}

Pure css,no js:)

答案 2 :(得分:0)

取决于你的意思。如果你的意思总是在页面上,那么:

#header, #footer { position: fixed; height 150px; }
#content { margin: 150px 0px; }

任何背景都应该放在body元素

如果您的意思是在内容的顶部,那么您只需要一个sticky footer

如果您的意思是页眉和页脚是自动高度,并且您希望它们始终位于页面上的固定点,那么您将需要jQuery,因为没有简单的跨浏览器解决方案。

$('#content').css({marginTop: $('#header').outerHeight(), marginBottom: $('#footer').outerHeight() });