Scrollbar without fixed height/Dynamic height with scrollbar

后端 未结 10 1485
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 22:24

I have this HTML structure:

Dynamic height without scrollbar

相关标签:
10条回答
  • 2020-12-02 22:35

    I have Similar issue with PrimeNG p_Dialog content and i fixed by below style for the contentStyle

    height: 'calc(100vh - 127px)'
    
    0 讨论(0)
  • 2020-12-02 22:36

    Use this:

    #head {
        border: green solid 1px;
        height:auto;
    }    
    #content{
                border: red solid 1px;
                overflow-y: scroll;
                height:150px;       
            }
    
    0 讨论(0)
  • 2020-12-02 22:39

    A quick, clean approach using very little JS and CSS padding: http://jsfiddle.net/benjamincharity/ZcTsT/14/

    var headerHeight = $('#header').height(),
        footerHeight = $('#footer').height();
    
    $('#content').css({
      'padding-top': headerHeight,
      'padding-bottom': footerHeight
    });
    
    0 讨论(0)
  • 2020-12-02 22:40

    Flexbox is a modern alternative that lets you do this without fixed heights or JavaScript.

    Setting display: flex; flex-direction: column; on the container and flex-shrink: 0; on the header and footer divs does the trick:

    HTML:

    <div id="body">
        <div id="head">
            <p>Dynamic size without scrollbar</p>
            <p>Dynamic size without scrollbar</p>
            <p>Dynamic size without scrollbar</p>  
        </div>
        <div id="content">
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
            <p>Dynamic size with scrollbar</p>
        </div>
        <div id="foot">
            <p>Fixed size without scrollbar</p>
            <p>Fixed size without scrollbar</p>
        </div>  
    </div>
    

    CSS:

    #body {
        position: absolute;
        top: 150px;
        left: 150px;
        height: 300px;
        width: 500px;
        border: black dashed 2px;
        display: flex;
        flex-direction: column;
    }
    
    #head {
        border: green solid 1px;
        flex-shrink: 0;
    }
    
    #content{
        border: red solid 1px;
        overflow-y: auto;
        /*height: 100%;*/
    }
    
    #foot {
        border: blue solid 1px;
        height: 50px;
        flex-shrink: 0;
    }
    
    0 讨论(0)
  • 2020-12-02 22:43

    I don't know if I got it right, but does this solve your problem?

    I just changed the overflow-y: scroll;

    #content{
        border: red solid 1px;
        overflow-y: scroll;
        height: 100px;
    }
    

    Edited

    Then try to use percentage values like this: http://jsfiddle.net/6WAnd/19/

    CSS markup:

    #body {
        position: absolute;
        top; 150px;
        left: 150px;
        height: 98%;
        width: 500px;
        border: black dashed 2px;
    }    
    #head {
        border: green solid 1px;
        height: 15%;
    }
    #content{
        border: red solid 1px;
        overflow-y: scroll;
        height: 70%;
    }
    #foot {
        border: blue solid 1px;
        height: 15%;
    }
    
    0 讨论(0)
  • 2020-12-02 22:45

    Use this:

    style="max-height: 300px; overflow: auto;" 
    

    Try to avoid fixed height

    0 讨论(0)
提交回复
热议问题