简体   繁体   English

如何设置最后一个内部div以使用CSS填充其余包装高度?

[英]How to set the last inner div to fill the rest of the wrapper height with CSS?


I would like to create a wrapper div that contains 2 inner divs so that the wrapper and the first inner divs will have a fixed height and the second inner div will fill the rest of the wrapper height. 我想创建一个包含2个内部div的包装div,以便该包装和第一个内部div具有固定的高度,第二个内部div将填充其余的包装高度。
I have done something like this: 我做了这样的事情:

<div class="frameArea">
   <div class="header"></div>
   <div class="frame"></div>
</div>

My CSS file looks like this: 我的CSS文件如下所示:

.frameArea {
   width: 200px;
   height: 300px;
   border: 2px solid black;
}
.frameArea .header {
   background-color: green;
   width: 100%;
   height: 25px;
   font-size: 18px;
   line-height: 25px;
}
.frameArea .frame {
   background-color: white;
   width: 100%;
   height: 100%;
}

The thing is, that using this CSS making the .frame div to be 100% of the wrapper's height , which means he will be 300px instead of what i'm looking for - 275px (in this case when the wrapper is 300px and the .header is 25px). 事实是,使用此CSS使.frame div为包装器高度的 100%,这意味着他将为300px而不是我要的值-275px(在这种情况下,包装器为300px且。标头为25像素)。
One way I can do this by setting the height property of .frame using JS code that will calculate and set it dynamically when the document is ready, however I would like to ask if there is any CSS property that does the same in more elegant way? 我可以通过使用JS代码设置.frame的height属性来实现此目的的一种方法,该JS代码将在文档准备就绪时动态地计算和设置它,但是我想问一问是否有CSS属性以更优雅的方式执行相同的操作?

Thanks! 谢谢!

Here's a jsFiddle with the solution. 这是带有解决方案的jsFiddle。 The answer is a simple CSS solution. 答案是一个简单的CSS解决方案。 Just change the .frame height: 100%; 只需更改.frame height: 100%; to height: auto; 达到height: auto; .

EDIT: Sorry, wrong fiddle. 编辑:对不起,错误的小提琴。 Here you go: http://jsfiddle.net/shaansingh/UX6XQ/1/embedded/result/ 在这里,您可以: http : //jsfiddle.net/shaansingh/UX6XQ/1/embedded/result/

Using CSS calc() 使用CSS calc()

.frameArea .frame {
    ...
    height: calc(100% - 25px);
}

DEMO 演示

calc() is not compatible with older browsers and thus might give you compatibility problems, to overcome this I suggest you use JavaScript solution. calc()与较旧的浏览器不兼容,因此可能会给您带来兼容性问题,为解决这个问题,我建议您使用JavaScript解决方案。

A jQuery Solution jQuery解决方案

var h = $('div.frameArea').height() - 25;
$('div.frame').height(h);

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM