如何指定div的高度? [英] how to specify height for divs?

查看:78
本文介绍了如何指定div的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指定前两个div的固定高度和下两个div的百分比值。

I want to specify fixed height for first two divs and percentage values for next two divs.

最后一个div应占据所有空间浏览器窗口)由前divs留下。如何在css中实现这一点?

The last div should occupy all the space (to the bottom of the browser window) left out by the former divs. how do I achieve this in css..?

这是我的css + html。

Here is my css+html.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>One Hundred Percent height divs</title>
<style type="text/css" media="screen">
html
{
height:100%;
}

body 
{
margin:0;
padding:0;
height:100%;
}
header
{
height:20px;
width:100%;
background-color:red;
}
.container2
{
height:40px;
width:100%;
background-color:#11ccdd;
}
.container3
{
height:35%;
width:100%;
background-color:#bacdef;
}
.subcontainer1
{
float:left;
height:35%;
width:50%;
background-color:#569895;
}
.subcontainer2
{
float:left;
height:35%;
width:50%;
background-color:#dbc462;
}
.container4
{
width:100%;
background-color:#aaaadd;
}
</style>
</head>

<body>

<header>
header
</header>

<div class="ClearAll"></div> 
<div class="container2">
container2
</div>
<div class="ClearAll"></div>

<div class="container3">
<div class="subcontainer1">
subcontainer1
</div>
 <div class="subcontainer2">
subcontainer2
</div>
</div>

<div class="ClearAll"></div>


<div class="container4">
container4


</div>

</body>
</html>

我需要提到前两个div的宽度的像素值,
第三个容器具有width的百分比值。第四个容器应该占据由其他三个div空出的剩余空间(屏幕大小)。

I need to mention the pixels values of width for first two divs as I wrote here. the third container has percentage value of width. the fourth container should occupy the rest of the space(screen size) left out by other three divs. how can I do that ?

推荐答案

您可以使用此JQuery脚本:

You can use this JQuery script:

function resizeContainer() {
    var screenHeight = $(window).height();
    var allElements = $('header').height() + $('.container2').height() + $('.container3').height();
    var container4Height = screenHeight - allElements;
    $('.container4').css({
        'height': container4Height
    });
}

resizeContainer();

$(window).resize(resizeContainer);

演示:

http://jsfiddle.net/Mutant_Tractor/NSh35/2/

这不能在CSS中完成,看起来100%完美

This CANNOT be done in CSS and look 100% perfect

这篇关于如何指定div的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆