我有一个包装div,其中包含2个div相邻。在这个容器上面,我有一个包含我的标题的div。包装器div必须是100%减去标题的高度。头部大约是60像素。这是固定的。我的问题是,如何设置包装器div的高度为100% - 60px ?
<div id="header"></div>
<div id="wrapper">
<div id="left"></div>
<div id="right"></div>
</div>
我有一个包装div,其中包含2个div相邻。在这个容器上面,我有一个包含我的标题的div。包装器div必须是100%减去标题的高度。头部大约是60像素。这是固定的。我的问题是,如何设置包装器div的高度为100% - 60px ?
<div id="header"></div>
<div id="wrapper">
<div id="left"></div>
<div id="right"></div>
</div>
当前回答
在CSS3中你可以使用
height: calc(100% - 60px);
其他回答
如果您需要支持IE6,请使用JavaScript来管理包装器div的大小(读取窗口大小后设置元素的位置(以像素为单位)。如果您不想使用JavaScript,那么就不能这样做。有一些变通办法,但预计需要一到两周时间才能在每种情况下和每种浏览器中工作。
对于其他现代浏览器,使用以下css:
position: absolute;
top: 60px;
bottom: 0px;
如果你不想使用绝对定位和所有的爵士,这里有一个我喜欢使用的修复:
html:
<body>
<div id="header"></div>
<div id="wrapper"></div>
</body>
css:
body {
height:100%;
padding-top:60px;
}
#header {
margin-top:60px;
height:60px;
}
#wrapper {
height:100%;
}
使用一个外部包装div设置为100%,然后你的内部包装div 100%现在应该是相对的。
我以为这对我很管用,但显然不是:
<html>
<body>
<div id="outerwrapper" style="border : 1px solid red ; height : 100%">
<div id="header" style="border : 1px solid blue ; height : 60px"></div>
<div id="wrapper" style="border : 1px solid green ; height : 100% ; overflow : scroll ;">
<div id="left" style="height : 100% ; width : 50% ; overflow : scroll; float : left ; clear : left ;">Some text
on the left</div>
<div id="right" style="height : 100% ; width 50% ; overflow : scroll; float : left ;">Some Text on the
right</div>
</div>
</div>
</body>
</html>
你可以这样做:height: calc(100% - nPx);例如height: calc(100% - 70px);
在这个例子中,你可以识别不同的区域:
<html>
<style>
#divContainer {
width: 100%;
height: 100%;
}
#divHeader {
position: absolute;
left: 0px;
top: 0px;
right: 0px;
height: 28px;
background-color:blue;
}
#divContentArea {
position: absolute;
left: 0px;
top: 30px;
right: 0px;
bottom: 30px;
}
#divContentLeft {
position: absolute;
top: 0px;
left: 0px;
width: 200px;
bottom: 0px;
background-color:red;
}
#divContentCenter {
position: absolute;
top: 0px;
left: 200px;
bottom: 0px;
right:200px;
background-color:yellow;
}
#divContentRight {
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
width:200px;
background-color:red;
}
#divFooter {
position: absolute;
height: 28px;
left: 0px;
bottom: 0px;
right: 0px;
background-color:blue;
}
</style>
<body >
<div id="divContainer">
<div id="divHeader"> top
</div>
<div id="divContentArea">
<div id="divContentLeft">left
</div>
<div id="divContentCenter">center
</div>
<div id="divContentRight">right
</div>
</div>
<div id="divFooter">bottom
</div>
</div>
</body>
</html>