Scrollbar without fixed height/Dynamic height with

2019-01-17 20:11发布

I have this HTML structure:

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

I want to have the three parts inside the main part (#body) without overflow. So I need a scroll bar in the middle part.

I tried this CSS:

#content{
    border: red solid 1px;
    overflow-y: auto;
}

And this:

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

But neither of them work.

I made an example at JSFiddle.

Can I do this with only CSS and HTML? I'd prefer to avoid Javascript.

9条回答
疯言疯语
2楼-- · 2019-01-17 20:22

Use this:

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

Tto avoid a fixed heigh

查看更多
成全新的幸福
3楼-- · 2019-01-17 20:29

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
});
查看更多
Evening l夕情丶
4楼-- · 2019-01-17 20:31

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

height: 'calc(100vh - 127px)'
查看更多
Rolldiameter
5楼-- · 2019-01-17 20:32

The JS answer to this question is:

http://jsfiddle.net/6WAnd/20/

or something similar

查看更多
做自己的国王
6楼-- · 2019-01-17 20:39

Use this:

#head {
    border: green solid 1px;
    height:auto;
}    
#content{
            border: red solid 1px;
            overflow-y: scroll;
            height:150px;       
        }
查看更多
一纸荒年 Trace。
7楼-- · 2019-01-17 20:39

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:

http://jsfiddle.net/cvmrvfhm/1/

查看更多
登录 后发表回答