Div height percentage based but still scrolling

2019-03-16 19:55发布

问题:

First off, similar but never answered questions:

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

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

I have an issue with scrolling a center part of the web page while its height needs to be auto.

Here is a fiddle

The header needs to be on top at all times, meaning I don't want the body to become larger than 100%.

However the div #messages can become larger, and that div needs to scroll on its own.

The #messages has a margin-bottom to leave room for the fixed bottom div.

I tried making the div #messages with box-sizing: border-box; and making it height:100% and padding to keep it in place but this was a really nasty looking solution and the scroll bar was the full page height instead of only the inner part.

Any help would be greatly appreciated.

回答1:

You could try the following.

You HTML is:

<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>

Apply the following 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;
}

See demo at: http://jsfiddle.net/audetwebdesign/5Y8gq/

First, set the height of 100% to the html and body tags, which allows you to reference the view port height.

You want the #header to be fixed towards the top of the page using position: fixed, similarly for your footer #input.

The key is to use absolute positioning on #content to stretch it between the bottom edge of the header and the top edge of the footer, and then apply overflow-y: scroll to allow it to scroll the content (list of messages).

Comment
The source code for the #input block may be placed outside of the #content block.



回答2:

You want something like This

Or maybe - his big brother..

Pure CSS solution, without fixing any height.

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;
}