我有一个主 DIV 容器(橙色)和一些 float 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 解决方案才能使其正常工作。我也可以发布完整的代码,但尊重你的时间,可能没有人疯狂到阅读不相关行的基调。
当然,margin
和 background-color
属性在这里只是为了提供视觉辅助。
所以基本上我的问题是:与所有其他内部 DIV 相比,如何将内部 DIV #1 保持在最大高度,以及如何使顶部和底部 DIV 在父元素内工作。当然,如果顶部和底部 block 很大,我不需要将它们相互混合,因为它们必须增加父 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>
最佳答案
这是您的解决方案(此处示例: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 setup of the same data(也必须将它绑定(bind)到这个 div 中)来计算“最小”高度。但是这个 Div 设置为可见性:隐藏,不干扰你的视觉。它在 Chrome 和 Firefox 中运行良好。
**此外,如果您将行添加到“ block ”以进行测试,则必须同时添加到可见 block 和不可见 block 。同样,当您将数据绑定(bind)到两个字段时,这将自动完成。
一些对我有帮助的阅读:
How to align content of a div to the bottom?
http://css-tricks.com/absolute-positioning-inside-relative-positioning/
关于html - CSS 定位、自动调整大小、上下对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27924358/