我有一个包装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>

当前回答

你可以这样做:height: calc(100% - nPx);例如height: calc(100% - 70px);

其他回答

这是一个工作的css,在Firefox / IE7 / Safari / Chrome / Opera下测试。

* {margin:0px;padding:0px;overflow:hidden}
div {position:absolute}
div#header {top:0px;left:0px;right:0px;height:60px}
div#wrapper {top:60px;left:0px;right:0px;bottom:0px;}
div#left {top:0px;bottom:0px;left:0px;width:50%;overflow-y:auto}
div#right {top:0px;bottom:0px;right:0px;width:50%;overflow-y:auto}

"overflow-y"不是w3c批准的,但是每个主要浏览器都支持它。你的两个div #left和#right如果它们的内容太高,将显示一个垂直滚动条。

为了在IE7下工作,你必须通过添加DOCTYPE来触发标准兼容模式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <style type="text/css"> *{margin:0px;padding:0px;overflow:hidden} div{position:absolute} div#header{top:0px;left:0px;right:0px;height:60px} div#wrapper{top:60px;left:0px;right:0px;bottom:0px;} div#left{top:0px;bottom:0px;left:0px;width:50%;overflow-y:auto} div#right{top:0px;bottom:0px;right:0px;width:50%;overflow-y:auto} </style> </head> <body> <div id="header"></div> <div id="wrapper"> <div id="left"><div style="height:1000px">high content</div></div> <div id="right"></div> </div> </body>

如果你不想使用绝对定位和所有的爵士,这里有一个我喜欢使用的修复:

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 {
    height: 100%;
    height: -webkit-calc(100% - 60px);
    height: -moz-calc(100% - 60px);
    height: calc(100% - 60px);
}

在少用的同时确保

height: ~calc(100% - 60px);

否则less是无法正确编译的

使用一个外部包装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>

在CSS3中你可以使用

height: calc(100% - 60px);