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

当前回答

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

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%;
}

其他回答

如果您需要支持IE6,请使用JavaScript来管理包装器div的大小(读取窗口大小后设置元素的位置(以像素为单位)。如果您不想使用JavaScript,那么就不能这样做。有一些变通办法,但预计需要一到两周时间才能在每种情况下和每种浏览器中工作。

对于其他现代浏览器,使用以下css:

position: absolute;
top: 60px;
bottom: 0px;

在CSS3中你可以使用

height: calc(100% - 60px);

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

我还没有看到这样的帖子,但我想我应该把它放在那里。

<div class="main">
<header>Header</header>
<div class="content">Content</div>

CSS:

body, html {
    height: 100%;
    margin: 0;
    padding: 0;
}

.main {
    height: 100%;
    padding-top: 50px;
    box-sizing: border-box;
}

header {
    height: 50px;
    margin-top: -50px;
    width: 100%;
    background-color: #5078a5;
}

.content {
    height: 100%;
    background-color: #999999;
}

这是一个工作的jsfiddle

注意:我不知道浏览器的兼容性是什么。我只是在玩其他的解决方案,这似乎很有效。

这并没有完全回答所提出的问题,但它确实创建了您试图实现的相同的视觉效果。

<style>

body {
  border:0;
  padding:0;
  margin:0;
  padding-top:60px;
}

#header {
  position:absolute;
  top:0;
  height:60px;
  width:100%;
}

#wrapper {
  height:100%;
  width:100%;
}
</style>