不能得到一个div为100%的高度

时间:2012-11-29 16:39:16

标签: css html

我在线尝试了几个建议,使我的html和body标签的高度为100%,最小高度为100%。我已将div标签设置为相同..它只是没有扩展到屏幕的底部。

此外,我无法获得第二个浮动div来填充100%未被其旁边的另一个div填充的空间。它只会填充100%的屏幕(在另一个较小的div下)或足以允许文本。

我想应该注意的是我尝试在IE9和Firefox 16中查看。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <style type="text/css">
    body {
      margin: 0px;
      padding: 0px;
      height: 100%;
      min-height: 100%;
      overflow:hidden;
    }  
    div#topbar {
      width: 100%;
      height:133px;
      background-image :url(bkgnd_header_tile.jpg);
    }
    div#logo {
      width: 187px;
      height: 133px;
      background-image:url('headerlogo_home.jpg');
      float: left;
    }
    div#text {
      width: 1;
      height: 133px;
      float: right;
    }
    div#campuses {
      height: 68px;
      padding-top: 10px;
      color: White;
      text-align: right;
    }
    div#title {
      height: 41px;
      color: White;
      text-align: right;
      padding-top: 14px;
    }
    div#sidebar {
      height: 100%;
      width: 250px;
      float: right;
      background-color: Black;
    }
    div#body {
      height: 100%;
      width: 100%
      float: right;
      font-family: Segoe UI;
    }
    span.text {
      padding-left: 15px;
      padding-right: 15px;
      font-family: Sans-Serif;
      font-size: small;
    }
    span.name {
      padding-left: 15px;
      padding-right: 15px;
      font-family: Sans-Serif;
      font-size: x-large;
    }  
  </style>
</head>
<body>
  <div id="topbar">
    <div id="logo"></div>
    <div id="text">
      <div id="campuses">
        <span class="text">St. John's Campus</span>
        <span class="text">Grenfell Campus</span>
        <span class="text">Marine Institute</span>
        <span class="text">Harlow Campus</span>
        <span class="text">Distance Education</span>
      </div>
      <div id="title"><span class="name">Memorial Self Service</span></div>
    </div>
  </div>
  <div id="sidebar">

  </div>
  <div id="body">
    asdf</div>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

position:relative设置为正文将考虑身体的高度并完全沿着身体拉伸绝对定位的内容 请注意,以百分比指定的高度属性是根据包含块

计算的

或者您也可以在正文标记

中尝试overflow:hidden;

答案 1 :(得分:0)

我会尝试将位置设置为绝对,因为我总是有这种奇怪的行为,绝对来救援,最重要的是我还要设置左边和顶部为0

答案 2 :(得分:0)

基本上,根据Ravindra的建议,并稍微修改一下,我得到了这个。我使用以下样式设置

设置正文
body {
  position: absolute;
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}

并将它们用于我的两个DIV部分:

div#body { 
  position: relative;
  height: 100%;
  overflow:scroll;
}

div#sidebar {
  position: fixed;
  background-color : #EFEFEF;
  height: 100%;
  width: 300px;
  overflow: hidden;
}

div#content {
  height: 100%;
  float: left;
  margin-left: 300px;

<div id="body"></div>是另外两个div的父容器,id =“sidebar”和id =“content”。我之所以这样做,是因为我在自己的div 133px高处还有一个顶部栏位于“body”div之上。