【发布时间】:2013-02-11 21:57:02
【问题描述】:
我在一个表格单元格中有 2 个 div,希望一个与单元格顶部对齐,另一个与单元格底部对齐。
我创建了这个 jsFiddle http://jsfiddle.net/davew9999/3Mwz5/11/,它显示了我拥有的 HTML 结构。代码如下:
HTML
<table>
<tbody>
<tr>
<td>
<div class="cell">
<div class="top">This text is dynamic so can't set fixed height on this div. For example
this text could be very long while the others are quite short.</div>
<div class="bottom">This should always be at the bottom of the table cell and also is dynamic
and can't be a fixed height.</div>
</div>
</td>
<td>
<div class="cell">
<div class="top">Not much text.</div>
<div class="bottom">This should always be at the bottom of the table cell and also is dynamic
and can't be a fixed height</div>
</div>
</td>
</tr>
</tbody>
</table>
CSS
tr {
vertical-align: baseline;
}
td {
padding:10px
}
.cell {
position: relative;
}
.bottom {
position: absolute;
bottom: 0;
}
我找到的所有解决方案都涉及在包装器 div 上设置高度(在我的示例中为“cell”类的 div)。但是,这对我不起作用,因为内容是动态的。
我尝试了一个解决方案,我计算了加载时 td 元素的高度,并将类“cell”的 div 设置为相同的高度。这一直有效,直到我调整浏览器窗口的大小,从而导致表格单元格的高度发生变化。我尝试处理浏览器窗口调整大小事件,但是在单击和拖动调整大小期间会触发很多。它必须在 IE8 及其缓慢的 JavaScript 引擎中工作,并且有超过 200 个 td 元素来检查这不是一个合理的解决方案。
给我所需输出的解决方案是将 div 的内容与“top”类和 div 与“bottom”类拆分到 2 个表格行中,但是我想知道是否有纯 CSS 解决方案或更好的解决方案我尝试过的 CSS 和 JavaScript 解决方案。
【问题讨论】:
-
你已经搞定了..只需给单元格添加一个高度..
-
"这对我不起作用,因为内容是动态的。" :)
-
没看到那个..好吧基本上我认为你需要再次使用你的计算脚本..然后如果jQuery
$(window).resize(function() { // Do the same function again });写这个命令后记 -
或正常的javascript:
window.onresize = function() { // function here } -
我对此进行了调查,发现在拖动以调整大小操作期间,调整大小事件被调用了很多。该表很大(超过 200 个 td 元素),它需要在 IE8 中运行,这在执行 JavaScript 时非常慢。我将更新提到这一点的问题。问题还询问是否有纯 CSS 解决方案
标签: html css css-position html-table