我必须为 <tr>
设置一个虚假的高度,而为 <td>
设置 height: inherit
<tr>
有 height: 1px
(无论如何它都会被忽略)
然后将 <td>
的高度设置为 height: inherit
然后将该
height: 100%
这对我在IE Edge和Chrome中有效:
<table style="width:200px;">
<tr style="height: 1px;">
<td style="height: inherit; border: 1px solid #000; width: 100px;">
<div>
Something big with multi lines and makes table bigger
</div>
</td>
<td style="height: inherit; border: 1px solid #000; width: 100px;">
<div style="background-color: red; height: 100%;">
full-height div
</div>
</td>
</tr>
</table>
td { height: 1px }
。 - Chris Happyth/td
单元格上的inherit
改为height: 100%
。但这样在Chrome / Safari等浏览器中就行不通了。:-)。我使用了 @supports (height: -moz-available)
来实际针对Firefox并在查询中设置高度。 - Tomáš Pustelník@supports (height: -moz-available)
。 - andzep当且仅当table元素本身具有height属性时,在表格单元格中使用div height=100%才有效。
<table border="1" style="height:300px; width: 100px;">
<tr><td>cell1</td><td>cell2</td></tr>
<tr>
<td style="height: 100%">
<div style="height: 100%; width: 100%; background-color:pink;"></div>
</td>
<td>long text long text long text long text long text long text</td>
</tr>
</table>
更新:在FireFox中,您还应该将height=100%
值设置为父级TD
元素。
td
高度也设置为 100%
,则在 FF 中应该可以正常工作。这是正确的答案。 - HartleySantable { height: 1px; } /* Will be ignored, don't worry. */
tr { height: 100%; }
td { height: 100%; }
td > div { height: 100%; }
Fiddle: https://jsfiddle.net/nh6g5fzv/
--
编辑:如果您想要在td中的div上应用padding,您必须添加box-sizing: border-box;
,因为有height: 100%
。
display: table;
吗?或者我们需要在 Chrome 的开发版本上尝试一下? - Madeorsk<table style="width:100px"> <!-- Not actually necessary; just makes the example text shorter -->
<tr><td>test</td><td>test</td></tr>
<tr>
<td style="padding:0;">
<div style="height:100%; width:100%; background-color:#abc; position:relative;">
<img style="left:90px; position:absolute;" src="../Content/Images/attachment.png"/>
test of really long content that causes the height of the cell to increase dynamically
</div>
</td>
<td>test</td>
</tr>
</table>
根据你在原问题中的描述,将width
和height
设置为100%
没有起作用,这表明可能存在其他规则覆盖了它。你是否在Chrome或Firebug中检查计算后样式以查看宽度/高度规则是否真正生效了?
我是多么愚蠢啊!div
的大小是根据文本而不是td
来确定的。你可以通过将div
设置为display:inline-block
来在Chrome上修复它,但在IE上不起作用。这证明更加棘手...
由于此处大多数解决方案都是错误的,顶部答案甚至没有解决问题,而选择的答案也不起作用。
为了以最短的方式解决问题,请将table
高度设置为fit-content
,将table-row
高度设置为100%
,然后您就完成了。
适用于Firefox,Chrome和Edge。
示例:
<table style="height:fit-content">
<tr style="height:100%">
<td>
<div style="height:100%"></div>
</td>
</tr>
</table>
height:fit-content
应用于 <table>
,无需对 <tr>
应用任何样式即可正常工作。 - Jay Dadhaniaheight:100%
才能在FireFox中工作。 - henk<td>
元素,并使用 CSS 的 border-spacing 属性来创建单元格之间的外边距。<table>
分配一个固定高度或使用 Javascript。border-spacing:5px
来设置单元格之间的间距,但我同意,在单元格内部使用DIV更加优雅和更好。 - vsyncborder-collapse: separate
应用于表格,否则它将没有任何效果。+1 - katzenhutborder-collapse: separate
是默认值,但是没错,这是一个好的提示。 - Matt Browne<table> <tr> <td> <div>
的height: 100%;
,那么这个由于其他浏览器(包括IE 7、8和9)正确处理表格单元格上的position:relative
而仅有Firefox出错,所以您最好使用JavaScript shim。 您不应该为一个失败的浏览器改变DOM。 当IE出现问题而所有其他浏览器都没有问题时,人们经常使用shim。
这里是带有注释的所有代码片段。我的示例中使用了响应式Web设计实践的JavaScript、HTML和CSS,但如果您不想使用,则不必使用。(响应式意味着它会自适应您的浏览器宽度。)
http://jsfiddle.net/mrbinky3000/MfWuV/33/
这里是代码本身,但如果没有上下文,它就没有意义,请访问上面的jsfiddle URL。(完整片段还有大量的CSS和Javascript注释。)
$(function() {
// FireFox Shim
if ($.browser.mozilla) {
$('#test').wrapInner('<div class="ffpad"></div>');
function ffpad() {
var $ffpad = $('.ffpad'),
$parent = $('.ffpad').parent(),
w, h;
$ffpad.height(0);
if ($parent.css('display') == 'table-cell') {
h = $parent.outerHeight();
$ffpad.height(h);
}
}
$(window).on('resize', function() {
ffpad();
});
ffpad();
}
});
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin título</title>
</head>
<body style="height:100%">
<!-- for Firefox and Chrome compatibility set height:100% in the containing TABLE, the TR parent and the TD itself. -->
<table width="400" border="1" cellspacing="0" cellpadding="0" style="height:100%;">
<tr>
<td>whatever</td>
<td>whatever</td>
<td>whatever</td>
</tr>
<tr style="height:100%;">
<td>whatever dynamic height<br /><br /><br />more content
</td>
<td>whatever</td>
<!-- display,background-color and radius properties in TD BELOW could be placed in an <!--[if IE]> commentary If desired.
This way TD would remain as display:table-cell; in FF and Chrome and would keep working correctly.
If you don't place the properties in IE commentary it will still work in FF and Chorme with a TD display:block;
The Trick for IE is setting the cell to display:block; Setting radius is only an example of whay you may want a DIV 100%height inside a Cell.
-->
<td style="height:100%; width:100%; display:block; background-color:#3C3;border-radius: 0px 0px 1em 0px;">
<div style="width:100%;height:100%;background-color:#3C3;-webkit-border-radius: 0px 0px 0.6em 0px;border-radius: 0px 0px 0.6em 0px;">
Content inside DIV TAG
</div>
</td>
</tr>
</table>
</body>
</html>
我提出了一种解决方案,使用Flexbox(实验性的)来模拟表格布局,这将允许单元格的内容元素在垂直方向上填充其父单元格:
.table{ display:flex; border:2px solid red; }
.table > *{ flex: 1; border:2px solid blue; position:relative; }
.fill{ background:lightgreen; height:100%; position:absolute; left:0; right:0; }
/* Reset */
*{ padding:0; margin:0; }
body{ padding:10px; }
<div class='table'>
<aside><div class='fill'>Green should be 100% height</div></aside>
<aside></aside>
<aside>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus conguet.</p>
</aside>
</div>