Div height percentage based but still scrolling

后端 未结 2 1633
小蘑菇
小蘑菇 2021-02-09 15:33

First off, similar but never answered questions:

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

scroll-bar-on-div-with-overflowau

相关标签:
2条回答
  • 2021-02-09 15:49

    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.

    0 讨论(0)
  • 2021-02-09 15:56

    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;
    }
    
    0 讨论(0)
提交回复
热议问题