让div占用剩余的空间(高度)?

时间:2013-02-07 12:10:58

标签: html css

HTML

<div class="container">
    <div class="header">
        <span>
            <span>First</span>
        </span>
        <span>
            <span>Second</span>
        </span>
        <span class="active">
           <span>Thrid</span>
        </span>
    </div>
    <div class="body">
        <div class="Control1">
            Content!
        </div>
    </div>
</div>

CSS

.container
{
    height: 300px;
    border: black 1px solid;
}

.container > .header
{
    background: grey;
}
.container > .header > span
{
    display: inline-block;
    padding: 10px;
}
.container > .header > .active
{
    background: black;
    color: white;
}

.container > .body
{
    height: 100%;
    overflow: auto;
    background: lightgrey;
}

http://jsfiddle.net/ytg8S/

我的问题是我的容器div高度为300像素,然后我有未知高度的标题(取决于它上面的项目数和屏幕大小,它可能超过一行。)我如何让.body占用剩下的空间并显示一个滚动条,如果它有更多的内容而不是空间?

我的第一个想法是摆弄绝对定位,但是当我有多行.header项目时这不起作用..

我无法更改html,因为它是由第三方组件生成的(我可以,但这将是很多工作),我宁愿避免使用javascript,如果我可以..

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么我认为这会做你想要的:

container > .body
{
    height: 100%;
    max-height:100%;
    overflow: auto;
    background: lightgrey;
}

答案 1 :(得分:0)

试试这个..希望这有帮助

   .container
{
    height: 300px;
    border: black 1px solid;
    background: #D3D3D3;
}

.container > .header
{
    height:auto;
    background: grey;
    max-height: 200px;
    overflow: auto; 
}
.container > .header > span
{
    display: inline-block;
    padding: 10px;
}
.container > .header > .active
{
    background: black;
    color: white;
}

.container > .body
{
    height: auto;
    max-height: 100px;
    overflow: auto;
    background: lightgrey;
}
相关问题