Div高度百分比基于但仍然滚动

时间:2013-10-01 14:02:36

标签: html css css3 scroll

首先,类似但从未回答的问题:

vertically-scrolling-percentage-based-heights-vertical-margins-codepen-exampl

scroll-bar-on-div-with-overflowauto-and-percentage-height

我在滚动网页中心部分时遇到问题,而其高度需要自动。

这是fiddle

标题必须始终位于顶部,这意味着我不希望身体变得大于100%。

然而div #messages可以变大,而div需要自己滚动。

#messages有一个margin-bottom,为固定的底部div留出空间。

我尝试使用#messages创建div box-sizing: border-box;并将其设为height:100%并填充以保持其位置但这是一个非常讨厌的解决方案,滚动条是整页高度而不仅仅是内部。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:6)

您需要This

之类的内容

或者 - 他的big brother ..

纯CSS解决方案,无需修复任何高度。

<强> HTML:

<div class="Container">
    <div class="First">
    </div>
    <div class="Second">
        <div class="Content">
        </div>
    </div>
</div>

<强> CSS:

*
{
    margin: 0;
    padding: 0;
}

html, body, .Container
{
    height: 100%;
}

    .Container:before
    {
        content: '';
        height: 100%;
        float: left;
    }

.First
{
    /*for demonstration only*/
    background-color: #bf5b5b;
}

.Second
{
    position: relative;
    z-index: 1;
    /*for demonstration only*/
    background-color: #6ea364;
}

    .Second:after
    {
        content: '';
        clear: both;
        display: block;
    }

.Content
{
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: auto;
}

答案 1 :(得分:5)

您可以尝试以下操作。

你的HTML是:

<div id="container">
    <div id="header">The header...</div>
    <div id="content">
        <div id="messages">
            <div class="message">example</div>
             ...
            <div class="message">example</div>
        </div>
        <div id="input">
            <div class="spacer">
                <input type="text" />
            </div>
        </div>
    </div>
</div>

应用以下CSS:

html, body {
    height: 100%;
}
body {
    margin:0;
}
#header {
    background:#333;
    height: 50px;
    position: fixed;
    top: 0;
    width: 100%;
}
#content {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    bottom: 45px;
    overflow-y: scroll;
}
#messages {
    overflow: auto;
}
#messages .message {
    height: 79px;
    background: #999;
    border-bottom: 1px solid #000;
}
#input {
    position:fixed;
    bottom:0;
    left:0;
    width:100%;
    height: 45px;
}
#input .spacer {
    padding: 5px;
}
#input input {
    width: 100%;
    height: 33px;
    font-size: 20px;
    line-height: 33px;
    border: 1px solid #333;
    text-indent: 5px;
    color: #222;
    margin: 0;
    padding: 0;
}

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

首先,将100%的高度设置为htmlbody标记,以便您引用视图端口高度。

您希望使用#headerposition: fixed固定在页面顶部,类似于您的页脚#input

关键是在#content上使用绝对定位在页眉的下边缘和页脚的上边缘之间拉伸,然后应用overflow-y: scroll以允许它滚动内容(消息列表)。

<强>注释
#input块的源代码可以放在#content块之外。

相关问题