html - 当存在静态页眉/页脚时,将滚动条限制在内容区域

标签 html css scrollbar box

有一个带有静态页眉和页脚的网页示例: Site Link (查看此网页时,请单击右上角的红色“X”删除透明的 archive.org header )

我的问题是,是否可以(轻松)将滚动条限制在实际(可滚动内容)区域——换句话说,滚动条不应延伸到静态页眉和页脚(就像现在一样)。

Here is a jsfiddle显示相同的内容:带有静态页眉和页脚的可滚动内容,其中滚动条越过页眉和页脚,但应限于内容框:

#content {
width: 1024px;
margin: 0 auto;
padding: 60px 0;
height:calc(100% - 120px);
}

这是当前实现的示例:

html, body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    background-color:#ff00ff;
    margin:0px auto;
}
.wrapper {
    width:100%;
    height:100%;
}
.container {
    width:1024px;
    margin:0px auto;
    height:100%;
}
header {
    width: 1024px;
    height: 60px;
    background: red;
    position: fixed;
    top: 0;
}
#content {
    width: 1024px;
    margin: 0 auto;
    padding: 60px 0;
    height:calc(100% - 120px);
    background-color:#fff;
}
footer {
    width: 1024px;
    height: 60px;
    background: red;
    position: fixed;
    bottom: 0;
}
<div class="wrapper">
    <div class="container">
        <header></header>
        <div id="content">
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLOWEEN</p>
        </div>
        <footer></footer>
    </div>
</div>

最佳答案

您可以拥有一个可滚动的内容 Pane ,方法是在 #content 上使用:overflow: auto;。 这是您的 css 的更改版本,可帮助您解决当前的问题:

html, body {
    height: 100%;
}
body {
    margin: 0;
    padding: 0;
    background-color:#ff00ff;
}
.wrapper {
    width:1024px;
    height: inherit;
}
.container {
    margin:0 auto;
    height: inherit;
}
header {
    height: 60px;
    background: red;
}
#content {
    margin: 0 auto;
    padding: 0;
    height:calc(100% - 120px);
    background-color:#fff;
    overflow: auto; 
}
footer {
    height: 60px;
    background: red;
}
<div class="wrapper">
    <div class="container">
        <header></header>
        <div id="content">
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLO</p>
            <p>HELLOWEEN</p>
        </div>
        <footer></footer>
    </div>
</div>

这里还有 jsFiddle:http://jsfiddle.net/7S4Xx/271/

关于html - 当存在静态页眉/页脚时,将滚动条限制在内容区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29745663/

相关文章:

html - 水平滚动条未显示

jquery - CSS 或 JQUERY 垂直滚动条但不设置高度

html - 如何在不使用 CSS 移动文本的情况下相对于文本定位图像?

用于内容对齐的 CSS

html - box-shadow 移除自身

html - 使用 CSS 在第三行之后放置一个元素

c# - 检查滚动条可见性

javascript - 在动态生成的 DOMElement 上应用 jQuery

php - 使用 jquery 时,表单字段未发布到 php 文件

jquery - 聚焦时如何创建位于输入表单下的div