CSS定位,自动调整大小,顶级&底部对齐

时间:2015-01-13 14:25:56

标签: html css position css-position

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

enter image description here

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

为了保持我的代码清晰,我发布了简单的结构,需要CSS解决方案才能使其正常工作。我也可以发布完整的代码,但要尊重你的时间,并且可能没有人能够阅读不相关行的音调。

课程,marginbackground-color属性仅用于提供视觉辅助。

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

当然,我可以使用JavaScript解决方法轻松完成,处理元素的offsetHeight属性,但这不是解决方案(仅限CSS)。此外,我不要求整个跨浏览器解决方案,任何适用于IE8 +和更新的Chrome,Firefox和Opera浏览器的东西都是我完全可以接受的。

我正在搜索SO和其他相关资源好几天但找不到任何可用的东西。现在还不确定它是否可行。因此,如果有任何我能做的事情让我知道,欢迎任何评论,建议,提示或技巧。

我的目标示例:

enter image description here

最后,我想用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>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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;
}

答案 2 :(得分: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>

位置:绝对是关键,但当灰色列“更长”时,则重叠。为了解决这个问题,我提出了一个立场:静态设置相同的数据(也必须将它绑定在这个div中)来计算“最小”高度。但是此Div设置为可见性:隐藏,不会干扰您的视觉效果。它适用于Chrome和Firefox。

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

一些帮助我的阅读:

How to align content of a div to the bottom?

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

相关问题