CSS定位,auto-resize,上下对齐

CSS positioning, auto-resize, top & bottom alignment

我有一个主要的 DIV 容器(橙色)和一些浮动的 DIV 容器(灰色和红色)。这些内部 DIV 有一些内容,主要 DIV 必须适合更高高度的高度。我面临的问题与第一个 DIV(灰色)有关。根据里面的内容 DIV 我必须将它保持在最大高度,所以如果它比任何其他 DIV 短它需要调整到最大高度,如果它更大主要 DIV 适合它尺寸。

DIV 还包含两个 DIV,我正在尝试定位它们,一个在顶部,另一个在 DIV 的底部。到目前为止,无论我尝试过什么,都失败了。

我确定 CSS 属性 positionbottom(或 top)与解决此问题相关,但到目前为止我尝试过的任何组合都没有帮不上忙。

<!-- MAIN CONTAINER: Orange -->
<div style="overflow:hidden; width:550px; background-color:#ffcc00">

  <!-- INNER CONTAINER #1: Gray -->
  <div style="overflow:hidden; float:left; width:180px; margin:5px 5px; background-color:#eeeeee">

    <!-- TOP BLOCK -->
    <div style="">This block goes top!</div>

    <!-- BOTTOM BLOCK -->
    <div style="">This block goes bottom!</div>

  </div>

  <!-- INNER CONTAINER #2: Red -->
  <div style="overflow:hidden; float:left; width:250px; margin:5px 5px; background-color:#ff0000">Not that important block<br />with some content<br />...</div>

</div>

为了保持我的代码清晰,我 post 只是简单的结构并且需要 CSS 解决方案来让它工作。我也可以 post 完成代码,但请尊重您的时间,可能没有人疯狂到阅读不相关行的基调。

当然,marginbackground-color 属性在这里只是为了提供视觉帮助。

所以基本上我的问题是:与所有其他内部 DIV 相比,如何将内部 DIV #1 保持在最大高度以及如何使顶部和底部 DIV 工作在父元素内。当然,如果顶部和底部块很大,我不需要将它们相互混合,因为它们必须增加父级 DIV(和主容器)的大小。

当然,我可以使用 JavaScript 解决方法轻松做到这一点,处理元素的 offsetHeight 属性 但这不是解决方案(仅限 CSS)。此外,我不要求完整的 cross-browser 解决方案,任何适用于 IE8+ 和更新版本 Chrome、Firefox 和 Opera 浏览器的东西对我来说都是完全可以接受的。

我搜索了 SO 和其他相关资源好几天了,但找不到任何有用的东西。甚至不确定现在是否可能。因此,如果我能做些什么来让它发挥作用,请告诉我,热烈欢迎任何评论、建议、提示或技巧。

我的目标示例:

最后,我想用 DIVs 和 CSS 模拟这种效果(以某种方式)。

<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="200" valign="top">Top</td>
    <td width="350" rowspan="2" valign="top">Side</td>
  </tr>
  <tr>
    <td valign="bottom">Bottom</td>
  </tr>
</table>

具有相对定位的页面元素使您可以控制绝对定位其中的子元素。

<html>
<body>
<!-- MAIN CONTAINER: Orange -->
<div style="position:relative; overflow:hidden; width:550px; background-color:#ffcc00">

<!-- INNER CONTAINER #1: Gray -->
<div style="overflow:hidden; float:left; width:180px; margin:5px 5px; background-    color:#eeeeee">

<!-- TOP BLOCK -->
<div style="position: auto; top: 0; left: 0; margin:5px 5px; background-color:#eeeeee">This block goes top! BLAH BLAH BLAH</div>

<!-- BOTTOM BLOCK -->
<div style="position: auto; bottom: 0; left: 0; margin:5px 5px; background-color:#eeeeee">This block goes bottom! BLAH BLAH BLAHBLAH BLAH BLAHBLAH BLAH BLAHBLAH BLAH BLAHBLAH BLAH BLAH</div>



</div>

<!-- INNER CONTAINER #2: Red -->
<div style="position:relative; overflow:hidden; float:left; width:250px; margin:10px 5px; background-color:#ff0000">Not that important block<br />with some content<br />...</div>

</div>

</body>
</html>

这个怎么样?

已更新 使用边框作为边缘区域。 因此不再需要 html 结构更改。

http://jsfiddle.net/amo4w5aj/15/

html

<!-- MAIN CONTAINER: Orange -->
<div class="container" style="position: relative; overflow:hidden; width:550px; background-color:#ffcc00;">

  <!-- INNER CONTAINER #1: Gray -->
  <div class="grey" style="overflow:hidden; float:left; width:180px; background-color:#eeeeee; ">

    <!-- TOP BLOCK -->
      <div style="">This block goes top</div>

    <!-- BOTTOM BLOCK -->
    <div style="position:absolute;bottom:0;background-color:pink;">This block goes bottom!</div>

  </div>

  <!-- INNER CONTAINER #2: Red -->
  <div class="red" style="overflow:hidden; float:left; width:250px; background-color:#ff0000">Not that important block<br />with some content<br />...</div>

</div>

css

.grey{
    padding-bottom :32767px;
    margin-bottom:-32767px;
}
.container{
    border: 5px solid #ffcc00;
}
.red{
    margin-left : 5px;
}

这是您的解决方案(此处示例:http://jsfiddle.net/jtnx7gw8/):

<!-- MAIN CONTAINER: Orange -->
<div style="height: 100%; width:550px;position: relative; background-color:#ffcc00; padding: 5px 0;">

  <!-- INNER CONTAINER #1: Gray -->
  <div style="display: inline-block; width:180px; margin:0 5px; vertical-align: top; height: 100%;">

    <!-- TOP BLOCK -->
    <div style="position: absolute; top: 5px; background: pink; max-width: 180px;">This block goes top!<br/>line 2<br/>line 3</div>
    <!-- INVISIBLE TOP BLOCK TO CALCULATE HEIGHT-->
    <div style="visibility: hidden; background: none;">This block goes top!<br/>line 2<br/>line 3</div>

    <!-- BOTTOM BLOCK -->
    <div style="position: absolute; bottom: 5px;background: gray; max-width: 180px;">This block goes bottom!</div>
    <!-- INVISIBLE BOTTOM BLOCK TO CALCULATE HEIGHT-->
    <div style="visibility: hidden;">This block goes bottom!</div>

  </div>

  <!-- INNER CONTAINER #2: Red -->
  <div style="height: 100%; display: inline-block; width:250px; margin:0 5px; background-color:#ff0000">Not that important block<br />with some content.<br/>...</div>

</div>

position: absolute 是关键,但当灰色列为 "longer" 时重叠。为了解决这个问题,我做了一个 position: static setup of the same data (will have to bind it in this div as well) to calculate the "minimum" height.但是这个 Div 设置为 visibility: hidden,不干扰你的视觉。它在 Chrome 和 Firefox 中工作得很好。

**此外,如果您向 'block' 添加行以进行测试,则必须同时添加到可见块和不可见块。同样,当您将数据绑定到两个字段时,这将自动完成。

一些对我有帮助的读物:

How to align content of a div to the bottom?

http://css-tricks.com/absolute-positioning-inside-relative-positioning/