0

下面的代码有一个 DIV 需要定位在容器的顶部,另一个在底部,然后内容需要从中间通过。

<div style="position:absolute; top:0; width:100%; height:40px"></div>

<div class="howto"></div>

<div style="position:absolute; bottom:0; width:100%; height:40px"></div>

所以我们不知道包含 DIV 的高度。如果没有 JS,带类的 div 怎么能让howto容器 DIV 的高度减去顶部和底部绝对定位的 div 的高度,从而包含这 2 个 DIV 之间的内容。

4

2 回答 2

1

对于您希望完成的任务,这是一种可能的解决方案:

@tinkerbin:http ://tinkerbin.com/QsaCPgR6

HTML:

<div class="container">
    <div class="header"></div>

    <div class="howto">
      Has height set to auto. You may change that if you want to.
    </div>

    <div class="footer"></div>
</div>

CSS:

.container {
  position: relative;
  padding: 40px 0; /* top and bottom padding = .header and .footer padding*/
}

.header,
.footer {
  position: absolute;
  height: 40px;
  width: 100%;
}

.header {
  top: 0;
}

.footer {
  bottom: 0;
}

.howto {
  height: /*specifiy one if you wish to*/;
}
于 2012-07-30T13:18:35.093 回答
0

据我所知,没有一种纯粹的 CSS 方式可以在没有 JS 的情况下完成您想做的事情。

请参阅之前关于 SA 的帖子:

使一个 div 填充剩余屏幕空间的高度

于 2012-07-30T11:41:07.230 回答