简体   繁体   English

如何让 div 填充整个表格单元格?

[英]How do I make a div fill an entire table cell?

I'm trying to fill the center cell of a table with a div element.我正在尝试用 div 元素填充表格的中心单元格。 For the purposes of illustrating the problem, the div is styled with a red background.为了说明问题,该 div 的样式为红色背景。 It seems to work in Chrome, but not IE.它似乎适用于 Chrome,但不适用于 IE。 In the fiddle below, IE is setting the height of the div to the minimum height necessary to contain its content.在下面的小提琴中,IE 将 div 的高度设置为包含其内容所需的最小高度。 In tinkering around with this problem with different CSS settings, I managed to get IE to interpret "height: 100%";在用不同的 CSS 设置修补这个问题时,我设法让 IE 解释“高度:100%”; as "the height of the browser window".作为“浏览器窗口的高度”。 However, as the question states, I want IE to interpret it as the height of the td cell.但是,正如问题所述,我希望 IE 将其解释为 td 单元格的高度。 Any ideas?有什么想法吗?

http://jsfiddle.net/UBk79/ http://jsfiddle.net/UBk79/

CSS: CSS:

*{
    padding: 0px;
    margin: 0px;
}
html, body{
    height: 100%;
}
#container{
    height:100%;
    width: 100%;
    border-collapse:collapse;
}
#centerCell{
    border: 1px solid black;
}
#main{
    height: 100%;
    background-color: red;
}

HTML: HTML:

<table id="container">
  <tr id="topRow" height="1px">
    <td id="headerCell" colspan="3">
      TOP
    </td>
  </tr>
  <tr id="middleRow">
    <td id="leftCell" width="1px">
      LEFT
    </td>
    <td id="centerCell">
      <div id="main">CENTER</div>
    </td>
    <td id="rightCell" width="1px">
      RIGHT
    </td>
  </tr>
  <tr id="bottomRow" height="1px">
    <td id="footerCell" colspan="3">
      BOTTOM
    </td>
  </tr>
</table>

I did some more research on this and collected some info that might come in handy to others trying to solve similar problems.我对此进行了更多研究,并收集了一些信息,这些信息可能对其他试图解决类似问题的人有用。 The CSS spec says the following three things that I think are important: CSS 规范说明了以下三件我认为很重要的事情:

First, re: specifying the height (of a div) as a percentage:首先,重新:将高度(div)指定为百分比:

The percentage is calculated with respect to the height of the generated box's containing block.该百分比是根据生成的框的包含块的高度计算的。 If the height of the containing block is not specified explicitly (ie, it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'.如果未明确指定包含块的高度(即,它取决于内容高度),并且该元素不是绝对定位的,则该值计算为 'auto'。

http://www.w3.org/TR/CSS21/visudet.html#the-height-property http://www.w3.org/TR/CSS21/visudet.html#the-height-property

... a height of 'auto' won't fill the cell unless the content is taller than the cell's minimum height. ...除非内容高于单元格的最小高度,否则“自动”的高度不会填充单元格。 But if we try to explicitly set the height of the containing cell or row, then we run into the following problem:但是如果我们尝试显式设置包含单元格或行的高度,那么我们会遇到以下问题:

CSS 2.1 does not define how the height of table cells and table rows is calculated when their height is specified using percentage values.当使用百分比值指定高度时,CSS 2.1 没有定义表格单元格和表格行的高度是如何计算的。

http://www.w3.org/TR/CSS21/tables.html#height-layout http://www.w3.org/TR/CSS21/tables.html#height-layout

Since the spec doesn't define it, I guess it's not too surprising that Chrome and IE choose to calculate it differently.由于规范没有定义它,我想 Chrome 和 IE 选择不同的计算方式也就不足为奇了。

Alternatively, (as xec indirectly pointed out) trying to use relative positioning has the following spec problem:或者,(正如 xec 间接指出的)尝试使用相对定位有以下规范问题:

The effect of 'position:relative' on table-row-group, table-header-group, table-footer-group, table-row, table-column-group, table-column, table-cell, and table-caption elements is undefined. 'position:relative' 对 table-row-group、table-header-group、table-footer-group、table-row、table-column-group、table-column、table-cell 和 table-caption 元素的影响未定义。

www.w3.org/TR/CSS21/visuren.html#propdef-position www.w3.org/TR/CSS21/visuren.html#propdef-position

So I've concluded there's probably not a pure CSS way to solve the problem that one can reasonably expect to work on most browsers.所以我得出的结论是,可能没有一种纯粹的 CSS 方法来解决人们可以合理地期望在大多数浏览器上工作的问题。

At first, I thought, "Wow, the CSS spec is pretty shoddy and incomplete for leaving all this stuff undefined."起初,我想,“哇,CSS 规范非常粗制滥造,因为没有定义所有这些东西。” As I thought about it more, though, I realized that defining the spec for these issues would a lot more complicated than it appears at first.然而,当我考虑得更多时,我意识到为这些问题定义规范会比最初看起来复杂得多。 After all, row/cell heights are calculated as a function of the heights of their content, and I want to make the height of my content a function of the row/cell height.毕竟,行/单元格高度是作为其内容高度的函数计算的,我想让我的内容的高度成为行/单元格高度的函数。 Even though I have a well-defined, terminating algorithm for how I want it to work in my specific case, it's not clear that the algorithm would easily generalize to all the other cases that the spec would need to cover without getting into infinite loops.即使我有一个明确定义的终止算法,我希望它在我的特定情况下如何工作,但不清楚该算法是否可以轻松地推广到规范需要涵盖的所有其他情况,而不会陷入无限循环。

Just set the table cell to: position:relative and the div to:只需将表格单元格设置为: position:relative并将 div 设置为:

position:absolute;
top:0;
left:0;
width:100%;
height:100%;

Edit 2017: DEMO BELOW:编辑 2017:下面的演示:

Note how you cannot see the red td because the yellow div covers it entirely...请注意您是如何看不到红色 td 的,因为黄色 div 完全覆盖了它...

 #expandingDiv { position: absolute; height: 100%; width: 100%; top: 0; left: 0; background: yellow; }
 <table style="width: 120px"> <tr> <td style="background: blue">blue&nbsp;td</td> <td style="background: green">green&nbsp;td</td> </tr> <tr> <td style="background: red; position: relative"> <div id='expandingDiv'> yellow div </div> </td> <td style="background: orange"> Some longer text which makes the bottom two tds expand dynamically. </td> </tr> </table>

Although I liked Craig's answer and will not use the approach in this answer myself, I did get quite far with this jsFiddle .虽然我喜欢 Craig 的回答并且我自己不会在这个答案中使用这种方法,但我确实在这个jsFiddle 上做得很远。

It relies on a hack, however: Setting height: 1px on the table.然而,它依赖于 hack height: 1px在桌子上设置height: 1px It works in Chrome, FF, IE11 and Edge (all that I tested), but Chrome starts misbehaving in edge cases.它适用于 Chrome、FF、IE11 和 Edge(所有我测试过的),但 Chrome 在边缘情况下开始行为不端。 See the fiddle.见小提琴。 Here are the interesting bits:以下是有趣的部分:

table {
    width: 100%;
    /* Whý does this make it work? */
    height: 1px;
}

td {
    border: 10px solid blue;
    height: 100%;
}

#container {
    width: calc(100% - 20px);
    height: calc(100% - 20px);
    border: 10px solid black;
}

Too much of a hack-smell to me.对我来说太多的黑客气味。

have you tried changing css to:您是否尝试将 css 更改为:

#centerCell{
    border: 1px solid black;
    height:100%;
}

seems to work for me on edge, firefox and chrome似乎在 Edge、Firefox 和 chrome 上对我有用

Simply set the line height of the div;只需设置div的行高; as long as its display is still a block level element.只要它的显示仍然是块级元素。 There is no need for relative or absolute positioning or hard coding of the height at the div level or any of its parents.不需要相对或绝对定位或在 div 级别或其任何父级对高度进行硬编码。 Works in IE 8+, Firefox, and Chrome.适用于 IE 8+、Firefox 和 Chrome。

Example:示例:

line-height: 50px;
// or
line-height: 2em;

Here's a jsfiddle: https://jsfiddle.net/55cc077/pvu5cmta/这是一个 jsfiddle: https ://jsfiddle.net/55cc077/pvu5cmta/

CSS height: 100% only works if the element's parent has an explicitly defined height. CSS height: 100% 仅当元素的父元素具有明确定义的高度时才有效。 This jQuery sets the table cell height in the first column.这个 jQuery 在第一列中设置表格单元格的高度。

<script type="text/javascript">
    $(function(){
    $('.myTable2 tr').each(function(){
    var H1 = $(this).height(); // Get the row height
    $(this).find('td:first').css({'height': H1 + 'px', 'line-height': H1 + 'px'}); //Set td height to row height
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM