How can I make the contents of a fixed element scrollable only when it exceeds the height of the viewport?

后端 未结 9 537
别跟我提以往
别跟我提以往 2020-12-01 00:24

I have a div positioned fixed on the left side of a web page, containing menu and navigation links. It has no height set from css, the content dete

相关标签:
9条回答
  • 2020-12-01 00:58

    Try this on your position:fixed element.

    overflow-y: scroll;
    max-height: 100%;
    
    0 讨论(0)
  • 2020-12-01 00:59

    You probably need an inner div. With css is:

    .fixed {
       position: fixed;
       top: 0;
       left: 0;
       bottom: 0;
       overflow-y: auto;
       width: 200px; // your value
    }
    .inner {
       min-height: 100%;
    }
    
    0 讨论(0)
  • 2020-12-01 01:03

    For your purpose, you can just use

    position: absolute;
    top: 0%;
    

    and it still be resizable, scrollable and responsive.

    0 讨论(0)
  • 2020-12-01 01:04

    Add this to your code for fixed height and add one scroll.

    .fixedbox {
        max-height: auto;
        overflow-y: scroll;
    }
    
    0 讨论(0)
  • 2020-12-01 01:08

    I'm presenting this as a workaround rather than a solution. This may not work all the time. I did it this way as I'm doing a very basic HTML page, for internal use, in a very bizarre environment. I know there are libraries like MaterializeCSS that can do really nice nav bars. (I was going to use them, but it didn't work with my environment.)

    <div id="nav" style="position:fixed;float:left;overflow-y:hidden;width:10%;"></div>
    <div style="margin-left:10%;float:left;overflow-y:auto;width:60%;word-break:break-all;word-wrap:break-word;" id="content"></div>
    
    0 讨论(0)
  • 2020-12-01 01:13

    You probably can't. Here's something that comes close. You won't get content to flow around it if there's space below.

    http://jsfiddle.net/ThnLk/1289

    .stuck {
        position: fixed;
        top: 10px;
        left: 10px;
        bottom: 10px;
        width: 180px;
        overflow-y: scroll;
    }
    

    You can do a percentage height as well:

    http://jsfiddle.net/ThnLk/1287/

    .stuck {
        max-height: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题