【问题标题】:CSS: Set div height to fill parent div, without position: abolute;CSS:设置 div 高度以填充父 div,无位置:绝对;
【发布时间】:2013-01-26 11:09:35
【问题描述】:

我(再次)遇到了将子 <div> 标签的大小调整为其父大小的问题。父元素由另一个脚本控制(不想触摸它),并且可以放置在屏幕上具有可变高度/宽度的任何位置。在下面的示例中,这是#container。我想在里面放一些布局,它有一些可变的和一些固定的尺寸:

  • 页脚(此处为:#footer),具有固定高度(例如 100 像素)并填满父项的整个宽度
  • 左侧的导航栏(此处为:#left),具有固定宽度(例如 150 像素)并填满上部的整个高度
  • 导航栏右侧的内容部分,即剩余空间。

我为“页脚”找到了一些解决方案,它确实有效(Make a div fill the height of the remaining screen space -> 'daniels' 的帖子)。但我无法让#left 部分填满整个高度。

下面是我的示例代码(在线版本:http://worldtalk.de/v2013/test.html;不会永远在线!):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Title</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <style type="text/css" media="screen">
    * {  margin: 0; }
    html, body { height: 100%; }
    #container {
        position: absolute; /* have no control over that container element */
        width: 400px;  height: 300px;
        top: 100px;    left: 10px;
        background: red;
    }
    #upper {
        min-height: 100%;
        height:     auto !important;
        height:     100%;
        margin:     0 auto -100px; /* -100px being the size of the footer */
    }
    #footer {
        background: green;
        height: 100px;
    }
    #left {
      background: yellow;
      float: left; width: 150px;

      /* the following CSS doesn't do what I want... */

      min-height: 100%;
      height:     auto !important;
      height: 100%;
    }
    </style>
</head>
<body>
    <div id="container">
      <div id="upper">
        <div id="left">left - shall reach down until footer</div>
        content part...<br> shall be next to it...
      </div>
      <div id="footer">footer</div>
    </div>
</body>
</html>

有什么想法可以在不使用 JavaScript 的情况下实现这一目标?

问候, 斯蒂芬

【问题讨论】:

  • 使用固定的min-height 值,而不是百分比
  • 我不知道所需的高度...在示例中显然是 300px-100px = 200px(parent-height 减去 footer-height)。但这个高度是动态的......它可能会从一个时刻到另一个时刻发生变化。

标签: html css height parent-child


【解决方案1】:

解决方案 1

我认为position:absolute; 的问题是左侧导航将放置在页脚的顶部,但是这里有一个使用绝对左侧导航和页脚的解决方案。它的缺陷是左侧导航继续在页脚下,这可能是也可能不是问题。

#footer {
  position:absolute;
  left:0;
  right:0;
}

#left {
  position:absolute;
  bottom:0;
}

http://jsfiddle.net/LmCLz/1/

解决方案 2

像这样重新排列元素:

<div id="container">
    <div class="inner">
        <div id="left">left - shall reach down until footer</div>
        <div id="right">content part...<br> shall be next to it...</div>
        <div class="clear"></div>
    </div>
    <div id="footer">footer</div>
</div>

然后应用margin-bottom:-100px; 为页脚腾出空间:

.inner {
    height:100%;
    margin-bottom:-100px;
}

http://jsfiddle.net/LmCLz/3/

【讨论】:

  • 解决方案 2 乍一看看起来不错,但与解决方案 1 存在相同的问题(请参阅:jsfiddle.net/LmCLz/18)。当您向其添加滚动条和更多内容时,它就在页脚后面。至少这似乎更接近我想要的。重新排列很好,因为我也想为“内容”部分有一个自己的 div 标签。
  • PS:你有解释为什么“位置:绝对;”有时是相对于父级,有时是相对于整个文档?
  • 我调整了您的解决方案....这似乎有效:jsfiddle.net/LmCLz/21 --- 感谢您的帮助! ...但是:我不明白为什么需要&lt;div class="inner"&gt;。或更详细:为什么我不能只给内部&lt;div&gt;-tag 类“内部”
  • .inner 的要点是创建一个大小正确的框(页脚为-100px),以便左右可以位于高度:100%% 您可能还可以添加边距- #left 和 #right 的底部规则。
  • 绝对位置相对于最近的非静态父级。如果您需要相对于常规定位的元素创建一些东西,通常的技巧是使其 position:relative 没有左、上等。
猜你喜欢
  • 1970-01-01
  • 2013-12-16
  • 2018-05-22
  • 2019-07-08
  • 2011-07-31
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多