CSS:除了内容修复以外的任何东西

时间:2014-10-04 19:15:54

标签: css layout fixed

我试图制作一个横幅,导航和页脚始终保持固定的布局,同时可以滚动内容。我在这里看到了一些类似的布局,但实际的页面内容不限于此。我现在想要的是将任何内容集中在一起,但你最好还是需要一些视觉效果 - 到目前为止我所得到的:

HTML

<body>
<div id="container">
    <div id="banner"></div>
    <div id="main">
        <div id="nav1"></div>
        <div id="nav2"></div>
        <div id="content"></div>
    </div>
    <div id="footer"></div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    width: 100%;
    background-color: #222;
}

#container {
    margin: 0 auto;
    height: 100%;
    width: 800px;
    margin-top: 20px;
    background-color: black;
}

#banner {
    width: 100%;
    height: 100px;
    background-color: red;
}

#main {
    height: 100%;
    width: 100%;
}

#nav1 {
    height: 100%;
    width: 150px;
    float: left;
    background-color: yellow;
}

#nav2 {
    height: 100%;
    width: 100px;
    float: right;
    background-color: yellow;
}

#content {
    height: 100%;
    width: 100%;
    background-color: white;
}

#footer {
    width: 100%;
    height: 30px;
    background-color: lime;
}

jsfiddle:http://jsfiddle.net/gLhd6sno/1/

滚动时我只想移动白色区域中的内容,我也无法弄清楚如何在不破坏布局的情况下禁用溢出。也许你有个主意? 谢谢。

1 个答案:

答案 0 :(得分:1)

这是一种依赖于绝对定位的方法。

* {
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    width: 100%;
    background-color: #222;
    margin: 0;
}

#container {
    width: 800px;
    left: 50%;
    margin-left: -400px;
    background-color: black;
    position: absolute;
    top: 20px;
    bottom: 0;
}

#banner {
    width: 100%;
    height: 100px;
    background-color: red;
    position: absolute;
    top: 0;
}

#main {
    width: 100%;
    position: absolute;
    top: 100px;
    bottom: 30px;
}

#nav1 {
    width: 150px;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    background-color: yellow;
    border: 2px dotted blue;
}

#nav2 {
    width: 100px;
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: yellow;
    border: 2px dotted blue;
}

#content {
    position: absolute;
    top: 0;
    bottom: 0px;
    left: 150px;
    right: 100px;
    background-color: tan;
    border: 2px dotted blue;
    overflow: auto;
}

#footer {
    width: 100%;
    position: absolute;
    bottom: 0;
    height: 30px;
    background-color: lime;
}

请参阅演示:http://jsfiddle.net/audetwebdesign/k9nsvt3t/

如果缩小高度,您会看到内容区域周围出现滚动条, 这可能会成功。其余的页面元素都是静态的,无论如何 主要区域的内容量。

相关问题