如何使<div>填充<td>高度

Cod*_*key 89 html css html-table

这个问题的答案需要更新,因为浏览器已经改变


原始问题

我已经浏览了StackOverflow上的几篇帖子,但未能找到这个相当简单的问题的答案.

我有一个像这样的HTML结构:

<table>
  <tr>
    <td class="thatSetsABackground">
      <div class="thatSetsABackgroundWithAnIcon">
        <dl>
          <dt>yada
          </dt>
          <dd>yada
          </dd>
        </dl>
      <div>
    </td>
    <td class="thatSetsABackground">
      <div class="thatSetsABackgroundWithAnIcon">
        <dl>
          <dt>yada
          </dt>
          <dd>yada
          </dd>
        </dl>
      <div>
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我需要的是div填充的高度td,所以我可以将div的背景(图标)定位在右下角td.

你怎么建议我去那?

psa*_*e23 156

如果你给你的TD高度为1px,那么子div将有一个高亮的父级来计算它的%.因为你的内容会比1px大,所以td会自动增长,就像div一样.有点垃圾黑客,但我打赌它会起作用.

  • 10年后仍然是唯一的解决方案:D (11认同)
  • 虽然它似乎在webkit中运行良好,但IE9完全破解了. (7认同)
  • 为了使其在 Firefox 和 Chrome 中都能工作,我必须使用 `min-height: 1px;` 而不是 `height: 1px;` (4认同)
  • 我不确定我是喜欢 CSS 还是讨厌它 (4认同)
  • 这是在Firefox中也可以使用的解决方案:http://stackoverflow.com/questions/36575846/how-to-make-div-fill-td-height-in-firefox/36576083#36576083 (3认同)
  • 这似乎在Firefox 45中不起作用 (2认同)
  • 12年后这对我很有帮助 (2认同)

Pat*_*Pat 31

CSS高度:100%仅在元素的父级具有明确定义的高度时才有效.例如,这将按预期工作:

td {
    height: 200px;
}

td div {
    /* div will now take up full 200px of parent's height */
    height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

由于您的<td>高度似乎是可变的,如果您添加了带有绝对定位图像的右下角图标,如下所示:

.thatSetsABackgroundWithAnIcon {
    /* Makes the <div> a coordinate map for the icon */
    position: relative;

    /* Takes the full height of its parent <td>.  For this to work, the <td>
       must have an explicit height set. */
    height: 100%;
}

.thatSetsABackgroundWithAnIcon .theIcon {        
    position: absolute;
    bottom: 0;
    right: 0;
}
Run Code Online (Sandbox Code Playgroud)

使用表格单元格标记如下:

<td class="thatSetsABackground">  
  <div class="thatSetsABackgroundWithAnIcon">    
    <dl>
      <dt>yada
      </dt>
      <dd>yada
      </dd>
    </dl>
    <img class="theIcon" src="foo-icon.png" alt="foo!"/>
  </div>
</td>
Run Code Online (Sandbox Code Playgroud)

编辑:使用jQuery设置di​​v的高度

如果你保持<div>作为孩子<td>,这个jQuery片段将正确设置其高度:

// Loop through all the div.thatSetsABackgroundWithAnIcon on your page
$('div.thatSetsABackgroundWithAnIcon').each(function(){
    var $div = $(this);

    // Set the div's height to its parent td's height
    $div.height($div.closest('td').height());
});
Run Code Online (Sandbox Code Playgroud)

  • 我是唯一一个认为"CSS高度:100%仅在元素的父级具有明确定义的高度时才起作用"的人是一个非常愚蠢的规则吗?浏览器无论如何都要确定父元素的高度; 没有办法*你*必须明确设置父元素的高度. (5认同)
  • 哈,如果我只有一美元的CSS让我改变了我的方法的次数:) (3认同)
  • 我并没有那么好地工作,我们决定采用不同的方法.但我会接受你的答案,因为它是最好的答案. (2认同)

小智 5

你可以尝试让你的div浮动:

.thatSetsABackgroundWithAnIcon{

    float:left;
}
Run Code Online (Sandbox Code Playgroud)

或者,使用内联块:

.thatSetsABackgroundWithAnIcon{

    display:inline-block;
}
Run Code Online (Sandbox Code Playgroud)

内联块方法的工作示例:

table,
th,
td {
  border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)
<table>
  <tr>
    <td>
      <div style="border:1px solid red; height:100%; display:inline-block;">
        I want cell to be the full height
      </div>
    </td>
    <td>
      This cell
      <br/>is higher
      <br/>than the
      <br/>first one
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

  • 至少现在这个片段在Chromium上也不起作用(我使用的是63.0.3239.84). (7认同)
  • 显示:内联块; 效果很好!感谢提示! (3认同)