Div height percentage based but still scrolling

后端 未结 2 1634
小蘑菇
小蘑菇 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:

    example
    ...
    example

    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.

提交回复
热议问题