Html 内容可滚动时,页眉和页脚位置固定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18215780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:06:58  来源:igfitidea点击:

Header and footer with fixed position while content is scrollable?

htmlcsscss-position

提问by Michal Artazov

I'm trying to make a website where header and footer have fixed position while content scrolls in the middle.

我正在尝试制作一个网站,其中页眉和页脚具有固定位置,而内容在中间滚动。

<header style="position:fixed"></header>
<div id="content">some content</div>
<footer style="position:fixed"></footer>

I created what I thought would work but it doesn't. Here's jsFiddlewith the whole thing. As you can see, part of content is hidden under the footer and beyond (I can't see 'HELLOWEEN' in the end). What must I change to fix it? Thanx

我创造了我认为会起作用的东西,但它没有。这是jsFiddle的全部内容。如您所见,部分内容隐藏在页脚下方(最后我看不到“HELLOWEEN”)。我必须更改什么才能修复它?谢谢

回答by Nick Tomlin

The easiest fix for this is to add padding equivalent to the height of your static header and footer:

最简单的解决方法是添加与静态页眉和页脚高度相等的填充:

#content {
    width: 80%;
    margin: 0 auto;
    padding: 60px 0;
}

JSfiddle

JSfiddle

回答by peterchon

http://jsfiddle.net/yASFU/

http://jsfiddle.net/yASFU/

<header>header</header>
<section>
    <div class="push">push</div>
</section>
<footer>footer</footer>

html, body {height:100%; margin:0; overflow:hidden;}
header, footer {display:block; background-color:black; height:10%;}
section {height:80%; background-color:lightblue; display:block; overflow:auto;}
section .push {height:4000px;}

回答by Stephen Thomas

  1. remove the position styles on the header and footer
  2. set a height on the content
  3. add a style overflow-y:auto to the content
  1. 删除页眉和页脚的位置样式
  2. 在内容上设置高度
  3. 向内容添加样式 overflow-y:auto

If you want the content height to match the browser window (less the header and footer), use javascript to set it (and adjust on window resize events)

如果您希望内容高度与浏览器窗口相匹配(减少页眉和页脚),请使用 javascript 进行设置(并在窗口调整大小事件上进行调整)