CSS 定位、自适应大小、顶部和底部对齐

4
我有一个主DIV容器(橙色)和一些浮动的内部DIV容器(灰色和红色)。这些内部DIV具有某些内容,主DIV必须适应高度较高的DIV的高度。我面临的问题与第一个DIV(灰色的DIV)有关。根据该DIV内的内容,我必须将其保持在最大高度,因此,如果它比任何其他DIV短,则需要调整大小以达到最大高度,如果它更大,则主DIV适合其大小。
该DIV还包含两个DIV,我正在尝试将其中一个定位在顶部,另一个定位在DIV底部。到目前为止,我尝试过的任何组合都失败了。我确信CSS属性position和bottom(或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>

为了保持代码清晰,我只发布简单的结构,并需要CSS方案来使其正常工作。我也可以发布完整的代码,但尊重您的时间,可能没有人会疯狂地阅读无关的行数。
当然,marginbackground-color属性仅用于提供视觉辅助。
所以,基本上我的问题是:如何保持内部DIV#1在高度上与所有其他内部DIV相比最大,并且如何使顶部和底部DIV在父元素内工作。 当然,如果它们很大,我不需要顶部和底部块混合在一起,那么它们必须增加父DIV(和主容器)的大小。
当然,我可以通过JavaScript解决方案轻松实现它,处理元素的offsetHeight属性,但这不可能是解决方案(仅限CSS)。 此外,我不要求整个跨浏览器解决方案,任何适用于IE8+以及更新的Chrome,Firefox和Opera浏览器的解决方案都非常可接受。
我搜索SO和其他相关资源数天,但找不到任何可用的东西。现在甚至不确定是否可能。因此,如果有任何我可以做的事情使其正常工作,请告诉我,任何评论,建议,提示或技巧都非常受欢迎。
我的目标示例:
最后,我想仅使用DIV和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>

你能在 JSFiddle 上发布一个帖子并将其链接到你的问题中吗? - Gerwin
你考虑过使用表格吗? - Paulie
我有你需要的答案,我现在会发布它。 - Paulie
@Gerwin,抱歉,没有理解。http://jsfiddle.net/6hwLaeqy/ - Wh1T3h4Ck5
@artuc,说实话,我从来没有听说过同位素。我肯定会去了解一下。现在,我正在寻找仅基于CSS的解决方案,但如果没有帮助,总是很好有额外的选择。 - Wh1T3h4Ck5
显示剩余6条评论
3个回答

1

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

<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>

请注意一下那些无关紧要的东西,现在应该可以工作了。(Blah是为了表示它已经可以工作了) - Paulie
如果这个答案符合您的实际需求,请标记为正确 :) - Paulie
请将其标记为正确,因为它回答了您的问题和评论问题。 - Paulie
无论如何,感谢您的时间和努力(点赞)。现在你知道我过去8天的感受了 ;) - Wh1T3h4Ck5
创建一个类来指定您希望的区域大小,然后将该类指向您希望自动扩展的内容。如果您向其中一个添加内容,则另一个将自动增加。 - Paulie
显示剩余2条评论

1

你觉得这怎么样?

更新 使用边框作为边距区域。 因此不需要进行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;
}

1
不幸的是,当红色块比灰色块所需的高度短时,它无法工作。在这个例子中,顶部块会消失。http://jsfiddle.net/amo4w5aj/16/ - Wh1T3h4Ck5

1
这里是你的解决方案(示例在此: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是关键,但当灰色列“更长”时会重叠。为了解决这个问题,我使用了相同数据的position: static设置来计算“最小”高度(也必须在此div中绑定它)。但是,这个Div被设置为visibility: hidden,以不干扰您的视觉。它在Chrome和Firefox中运行良好。

**另外,如果您添加行来测试一个“块”,则必须同时添加到可见和不可见的块中。当您将数据绑定到两个字段时,这将自动完成。

一些帮助我的阅读材料:

如何将div的内容对齐到底部?

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


答案已更新 - 这是我所能想到的最好的解决方案,而且没有使用JavaScript。据我所知,它完全符合您的期望。 - deebs
没错,迫不及待地想测试一下。终于达到了预期的效果。我已经测试了很多不同的内容,并且它们在大多数主流浏览器上都是视觉上正确的。谢谢Dave!让音乐响起来,woo-hoo:D - Wh1T3h4Ck5
太好了听到这个消息!这是一个非常有趣的问题,似乎是一个相当普遍的问题。很棒的问题,很高兴它最终为您工作了。 - deebs
很抱歉浪费你的时间,而且,就在8天前,这对我来说似乎真的是一件简单的事情,但今天我终于放弃了并在这里发布了。有时候事情并不像看起来那么简单。 - Wh1T3h4Ck5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接