让DIV填充整个表格单元格

我已经看到这个问题谷歌一下,但到目前为止还没有工作。我想现在已经是2010年了(那些问题/答案已经过时了,而且没有答案),我们有了CSS3!有没有办法让一个div填充整个表格单元格的宽度和高度使用CSS?

我不知道单元格的宽度和/或高度将提前,设置div的宽度和高度为100%是行不通的。

另外,我需要div的原因是因为我需要在单元格之外绝对定位一些元素,而position: relative不适用于tds,所以我需要一个包装器div。

266560 次浏览

我认为最好的解决方案是使用JavaScript。

但我不确定你需要这样做来解决你在单元格外的<td>中定位元素的问题。你可以这样做:

<div style="position:relative">
<table>
<tr>
<td>
<div style="position:absolute;bottom:-100px">hello world</div>
</td>
</tr>
</table>
</div>

当然不是内联的,而是类和id。

以下代码适用于IE 8, IE 8的IE 7兼容模式和Chrome(未在其他地方测试):

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

你在最初的问题中说,将widthheight设置为100%不起作用,不过,这让我怀疑有其他一些规则覆盖了它。你有没有检查Chrome或Firebug中的计算样式,看看宽度/高度规则是否真的被应用?

编辑

我多么愚蠢!div的大小是针对文本的,而不是针对td的。你可以在Chrome上通过使div display:inline-block来修复这个问题,但它不能在IE上工作。事实证明这更棘手……

在表格单元格中Div height=100%将只在表格本身具有height属性时起作用。

<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

这是我在html <td>中扩展100%高度和100%宽度的解决方案

如果你删除代码中的第一行,你可以修复它…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

因为这个文档类型不允许在某些文件中使用<td>内部的100%,但如果你删除这一行,正文无法将背景扩展到100%高和100%宽,所以我发现这个DOCTYPE解决了这个问题

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">

这个DOCTYPE可以让你在你的body中扩展背景为100%的高度和100%的宽度,并让你把所有的100%高度和100%宽度带入<td>

由于所有其他浏览器(包括ie7、ie8和ie9)都正确地处理表单元格上的position:relative,而只有 Firefox会出错,所以最好的办法是使用JavaScript shim。您不应该为了一个失败的浏览器而更改DOM。当IE出错而其他浏览器都没问题时,人们总是使用shims。

下面是注释了所有代码的代码片段。在我的例子中,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();
}
});

如果你想在表格单元格中使用100% div的原因是能够将背景色扩展到完整的高度,但单元格之间仍然能够有间距,你可以将背景色赋予<td>本身,并使用CSS border-spacing属性创建单元格之间的空白。

然而,如果你真的需要一个100%高度的div,那么就像其他人提到的那样,你需要为<table>分配一个固定的高度,或者使用Javascript。

经过几天的搜索,我想出了一个可能的解决这个问题。

 <!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>
< p >西班牙语: El truco es establecer la Tabla, El TR y El TD a height:100%。要有兼容FireFox和Chrome。Internet Explorer ignora eso, por lo que ponemos la etiquette TD como block。De esta forma Explorer sí toma la altura máxima.

英文解释:在代码注释中

我提出了一个解决方案,使用实验 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>

为了使height:100%适用于内部div,你必须为父div设置一个高度。对于我的特殊情况,它使用高度:100%。这使得内部div的高度有所延伸,而表的其他元素不允许td变得太大。当然,您可以使用100%以外的其他值

如果你还想让表格单元格有一个固定的高度,这样它就不会根据内容而变大(让内部div滚动或隐藏溢出),那就是当你别无选择,只能做一些js的技巧。父td需要以非相对单位(不是%)指定高度。你很可能别无选择,只能用js来计算这个高度。这也需要为table元素设置table-layout:fixed样式

我经常遇到类似的问题,总是在table元素上使用table-layout: fixed;,在内部div上使用height: 100%;

我不知道你想怎么做,但你可以试试这个:

<td width="661" valign="top"><div>Content for New Div Tag Goes Here</div></td>

以下应该工作。你必须为父单元格设置一个高度。 https://jsfiddle.net/nrvd3vgd/ < / p >

<table style="width:200px; border: 1px solid #000;">
<tr>
<td style="height:100px;"></td>
</tr>
<tr>
<td style="height:200px;">
<div style="height:100%; background: #f00;"></div>
</td>
</tr>
</table>
我最终发现除了使用jQuery之外,没有任何东西可以在我所有的浏览器和所有DocTypes /浏览器渲染模式中工作。这就是我想到的。

function InitDivHeights() {
var spacing = 10; // <-- Tune this value to match your tr/td spacing and padding.
var rows = $('#MyTable tr');
var heights = [];
for (var i = 0; i < rows.length; i++)
heights[i] = $(rows[i]).height();
for (var i = 0; i < rows.length; i++) {
var row = $(rows[i]);
var cells = $('td', row);
for (var j = 0; j < cells.length; j++) {
var cell = $(cells[j]);
var rowspan = cell.attr('rowspan') || 1;
var newHeight = 0;
for (var k = 0; (k < rowspan && i + k < heights.length); k++)
newHeight += heights[i + k];
$('div', cell).height(newHeight - spacing);
}
}
}
$(document).ready(InitDivHeights);

在IE11 (Edge模式),FF42, Chrome44+测试。没有使用嵌套表进行测试。

我不得不fake height设置为__ABC0和height: inherit for <td>s

tr有height: 1px(无论如何都会被忽略)

然后设置td height: inherit .

然后将div设置为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 style="position:relative;">
<div style="position:absolute;top:0;bottom:0;width:100%;"></div>
</td>

好吧,没有人提到这个,所以我想我要发布这个技巧:

.tablecell {
display:table-cell;
}
.tablecell div {
width:100%;
height:100%;
overflow:auto;
}

在单元格内的容器div上的溢出:汽车为我做了这个技巧。如果没有它,div就不会使用整个高度。

如果<table> <tr> <td> <div>都设置了height: 100%;,则div将在所有浏览器中填充动态单元格高度。

我不看这里一个老css技巧<div>内部<道明>。因此我提醒:简单地为宽度设置一些最小值,而为min-width设置你需要的东西。例如:

<div style="width: 3px; min-width: 99%;">

在这种情况下,道明的宽度由你决定。

如果表格单元格是你想要的大小,只需添加这个css类并将它分配给你的div:

.block {
height: -webkit-calc(100vh);
height: -moz-calc(100vh);
height: calc(100vh);
width: 100%;
}

如果您希望表格单元格也填充父单元格,则将类也分配给表格单元格。

你可以这样做:

<td>
<div style="width: 100%; position: relative; float: left;">inner div</div>
</td>

有点晚了,但这是我的解决方案:

<td style="padding: 0; position: relative;">
<div style="width: 100%; height: 100%; position: absolute; overflow: auto;">
AaaaaaaaaaaaaaaaaaBBBBBBBbbbbbbbCCCCCCCCCccccc<br>
DDDDDDDddddEEEEEEEEdddfffffffffgggggggggggggggggggg
</div>
</td>

所以,因为每个人都在发布他们的解决方案,没有一个适合我,这里是我的(在Chrome &Firefox)。

table { height: 1px; } /* Will be ignored, don't worry. */
tr { height: 100%; }
td { height: 100%; }
td > div { height: 100%; }

小提琴:https://jsfiddle.net/nh6g5fzv/

--

编辑:你可能要注意的一件事是,如果你想在td中对div应用填充,你必须添加box-sizing: border-box;,因为height: 100%

如果被定位的元素及其父元素没有宽度和高度,则设置

padding: 0;

在其父元素中,

因为我没有足够的声誉发表评论,我想添加一个完整的跨浏览器解决方案,结合@Madeorsk和@Saadat的方法,并进行了一些轻微的修改!(截至2020年2月10日,在Chrome、Firefox、Safari、IE和Edge上测试)

table { height: 1px; }
tr { height: 100%; }
td { height: 100%; }
td > div {
height: -webkit-calc(100vh);
height: -moz-calc(100vh);
height: calc(100%);
width: 100%;
background: pink;  // This will show that it works!
}

然而,如果你像我一样,你也想控制垂直对齐,在这种情况下,我喜欢使用flexbox:

td > div {
width: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
}

不建议这样做。

我想出了一个工作,如果你的div不包含文本和有一个统一的背景。

display: 'list-item'

为div提供所需的高度和宽度,但有一个很大的缺点,即拥有列表项项目符号。因为div有一个相同的背景色,我用这些样式去掉了子弹:

list-style-position: 'inside',
color: *background-color*,

由于这里的大多数答案都是不正确的,上面的答案甚至不能解决问题,选择的答案也不起作用。

为了以最短的方式解决问题,将table height设置为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>

这个hack对我来说是有用的:只要在div中放一个空格(然后它会占用td的所有高度)

<td>
<div>
&nbsp;
<!-- some optional content filled by vuejs, angular or what you want -->
</div>
</td>