Scroll part of content in fixed position container

后端 未结 5 623
逝去的感伤
逝去的感伤 2020-12-08 00:27

I have a position: fixed div in a layout, as a sidebar. I\'ve been asked to have part of it\'s content stay fixed to the top of it (internally), and the rest to

相关标签:
5条回答
  • 2020-12-08 00:27

    What worked for me :

    div#scrollable {
        overflow-y: scroll;
        max-height: 100vh;
    }
    
    0 讨论(0)
  • 2020-12-08 00:34

    Set the scrollable div to have a max-size and add overflow-y: scroll; to it's properties.

    Edit: trying to get the jsfiddle to work, but it's not scrolling properly. This will take some time to figure out.

    0 讨论(0)
  • 2020-12-08 00:37

    It seems to work if you use

    div#scrollable {
        overflow-y: scroll;
        height: 100%;
    }
    

    and add padding-bottom: 60px to div.sidebar.

    For example: http://jsfiddle.net/AKL35/6/

    However, I am unsure why it must be 60px.

    Also, you missed the f from overflow-y: scroll;

    0 讨论(0)
  • 2020-12-08 00:43

    I changed scrollable div to be with absolute position, and everything works for me

    div.sidebar {
        overflow: hidden;
        background-color: green;
        padding: 5px;
        position: fixed;
        right: 20px;
        width: 40%;
        top: 30px;
        padding: 20px;
        bottom: 30%;
    }
    div#fixed {
        background: #76a7dc;
        color: #fff;
        height: 30px;
    }
    
    div#scrollable {
        overflow-y: scroll;
        background: lightblue;
    
        position: absolute;
        top:55px; 
        left:20px;
        right:20px;
        bottom:10px;
    }
    

    DEMO with two scrollable divs

    0 讨论(0)
  • 2020-12-08 00:44

    Actually this is better way to do that. If height: 100% is used, the content goes off the border, but when it is 95% everything is in order:

    div#scrollable {
        overflow-y: scroll;
        height: 95%;
    }
    
    0 讨论(0)
提交回复
热议问题