【问题标题】:How to make <div> fill <td> height如何使 <div> 填充 <td> 高度
【发布时间】:2011-04-02 07:24:27
【问题描述】:

我浏览了 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>

我需要div 填充td 的高度,这样我就可以将div 的背景(图标)定位在td 的右下角。

你建议我怎么做?

【问题讨论】:

标签: html css html-table


【解决方案1】:

如果你给你的 TD 一个 1px 的高度,那么子 div 将有一个高度的父级来计算它的百分比。因为你的内容会大于 1px,td 会自动增长,div 也会自动增长。有点垃圾黑客,但我敢打赌它会工作。

【讨论】:

  • 虽然它在 webkit 中似乎可以正常工作,但 IE9 完全崩溃了。
  • 这是一个同样适用于 Firefox 的解决方案:stackoverflow.com/questions/36575846/…
  • 为了让它在 Firefox 和 Chrome 中运行,我不得不使用 min-height: 1px; 而不是 height: 1px;
  • ? 我不确定我是喜欢 CSS 还是讨厌它
  • 10 年后仍然是唯一的解决方案:D
【解决方案2】:

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

td {
    height: 200px;
}

td div {
    /* div will now take up full 200px of parent's height */
    height: 100%;
}

由于您的 &lt;td&gt; 似乎将是可变高度,如果您添加右下角的图标和绝对定位的图像,如下所示:

.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;
}

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

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

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

如果您将&lt;div&gt; 保留为&lt;td&gt; 的子级,则jQuery 的这个sn-p 将正确设置其高度:

// 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());
});

【讨论】:

  • 这可行。等我试过了再回复你。我确实阅读了该主题并了解
    需要指定高度才能缩放到 100%。从我读到的内容来看,jQuery 可以做到这一点,因为它可以为我计算。
  • 我的工作并不是那么好,我们决定采用不同的方法。但我会接受你的回答,因为它是最好的。
  • 哈,要是我能有一块钱来支付 CSS 让我改变方法的次数就好了 :)
  • 我是唯一一个认为“CSS 高度:100% 仅在元素的父级具有明确定义的高度时才有效”是一个非常愚蠢的规则的人吗?浏览器无论如何都会确定父元素的高度; you 没有办法显式设置父元素的高度。
  • jquery 解决方案仅在我将其放在文件末尾时才有效,谢谢
【解决方案3】:

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

.thatSetsABackgroundWithAnIcon{

    float:left;
}

或者,使用 inline-block:

.thatSetsABackgroundWithAnIcon{

    display:inline-block;
}

inline-block 方法的工作示例:

table,
th,
td {
  border: 1px solid black;
}
<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>

【讨论】:

  • float:left 建议似乎不起作用。这个答案可以通过删除它来改进,因为提到的第二种解决方案似乎有效。
  • Firefox 没有。它似乎只适用于 Chrome。
  • 那个 sn-p 也不能在 Chromium 上工作,至少现在是这样(我使用的是 63.0.3239.84)。
【解决方案4】:

这个问题已经回答了here。只需将height: 100% 放在div 和容器td 中即可。

【讨论】:

    【解决方案5】:

    真的必须用 JS 来做这件事。这是一个解决方案。我没有使用你的类名,但我在“full-height”的 td 类名中调用了 div :-) 显然使用了 jQuery。请注意,这是从 jQuery(document).ready(function(){ setFullHeights();}); 调用的。 另请注意,如果您有图像,则必须首先使用以下内容遍历它们:

    function loadedFullHeights(){
    var imgCount = jQuery(".full-height").find("img").length;
    if(imgCount===0){
        return setFullHeights();
    }
    var loaded = 0;
    jQuery(".full-height").find("img").load(function(){
        loaded++;
        if(loaded ===imgCount){
            setFullHeights()
        }
    });
    

    }

    您可能想从 docReady 调用 loadedFullHeights() 。这实际上是我最终使用的以防万一。一定要提前考虑!

    function setFullHeights(){
    var par;
    var height;
    var $ = jQuery;
    var heights=[];
    var i = 0;
    $(".full-height").each(function(){
        par =$(this).parent();
        height = $(par).height();
        var tPad = Number($(par).css('padding-top').replace('px',''));
        var bPad = Number($(par).css('padding-bottom').replace('px',''));
        height -= tPad+bPad;
        heights[i]=height;
        i++;
    });
    for(ii in heights){
        $(".full-height").eq(ii).css('height', heights[ii]+'px');
    }
    

    }

    【讨论】:

      【解决方案6】:

      修改

      本身的背景图片。

      或者对 div 应用一些 css:

      .thatSetsABackgroundWithAnIcon{
          height:100%;
      }
      

      【讨论】:

      • 正如我在代码中所写的那样。 TD 已经使用类设置了背景图像。所以这个选项是不可行的。我尝试将 div 的高度设置为 100%,但这不起作用。它只是简单地包装到包含的
        的可变高度。所以需要一些东西来让 div “理解”它应该填充高度。而 height: 100% 不会那样做。 (至少不是一个人)
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签