【问题标题】:Table inside div, but still higher than parent divdiv 内的表,但仍高于父 div
【发布时间】:2017-01-01 06:47:36
【问题描述】:

为什么 height="100%" 属性在下面的 sn-p 中不起作用?

<table height="100%" width="100%">

编辑: 代码如下:

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

<div style="width:250px;height:90px;">

<table id="experiences-mirror" width="100%" border="1" cellpadding="0" height="100%">
    <caption>Table Name</caption>
    <tbody><tr><th>Company</th><th>Job Title</th><th>Industry</th><th>Job Function</th><th>Start Date</th><th>End Date</th></tr>
    <tr title="hint">
        <td><input name="company1" class="w100" type="text"></td><td><input name="jobTitle1" class="w100" value="" type="text"></td><td><input name="industry1" class="w100" value="" type="text"></td><td><input name="jobFunction1" class="w100" type="text"></td><td><input name="startDate1" class="w100" type="text"></td><td><input name="endDate1" class="w100" type="text"></td>
    </tr>
    <tr title="hint">
        <td><input name="company2" class="w100" type="text"></td><td><input name="jobTitle2" class="w100" type="text"></td><td><input name="industry2" class="w100" type="text"></td><td><input name="jobFunction2" class="w100" type="text"></td><td><input name="startDate2" class="w100" type="text"></td><td><input name="endDate2" class="w100" type="text"></td>
    </tr>
    <tr title="hint">
        <td><input name="company3" class="w100" type="text"></td><td><input name="jobTitle3" class="w100" type="text"></td><td><input name="industry3" class="w100" type="text"></td><td><input name="jobFunction3" class="w100" type="text"></td><td><input name="startDate3" class="w100" type="text"></td><td><input name="endDate3" class="w100" type="text"></td>
    </tr>
    <tr title="hint">
        <td><input name="company4" class="w100" type="text"></td><td><input name="jobTitle4" class="w100" type="text"></td><td><input name="industry4" class="w100" type="text"></td><td><input name="jobFunction4" class="w100" type="text"></td><td><input name="startDate4" class="w100" type="text"></td><td><input name="endDate4" class="w100" type="text"></td>
    </tr>
</tbody></table>

</div>

【问题讨论】:

  • 你能贴出代码吗?
  • 可以通过将许多 放入表中来重现它,每一行都有一个
  • 能否添加更全面的代码示例?从已发布的少量内容很难分辨。
  • 我试图重现这一点,但我不能。您能否发布您拥有的实际代码,最好包括 DOCTYPE,以便我们知道浏览器处于哪种渲染模式。

标签: html html-table


【解决方案1】:

问题是您的表格的计算高度已经大于 90 像素。我把它提高到 290 像素,你的代码就可以工作了。

然而,设置高度的首选方法是通过 CSS,例如:

#experiences-mirror {
 width: 100%;
 height: 100%;
}

【讨论】:

  • 我无法调整高度,如何让表格自行调整大小以适应容器?
  • 它已经可以工作了(在 FF 和 IE 中测试过)。
    的高度为 90px,而 的计算高度超过了该高度。如果您将
    上的 90px 更改为 290px,您会看到它工作正常。
  • 但是我不能把90px改成290px,大小也不能改。我需要表格自动调整大小。
  • 它会自动调整大小。你想让它自动变小还是什么?
  • 是的,在这种情况下,如果表格已经比父容器大,表格应该会自动变小。
【解决方案2】:

可能是因为table-layout CSS 属性。通常,如果表格是自动布局的,那么无论您在 &lt;table&gt; 标记中放置什么内容,它都会根据内容自行调整大小。表格渲染算法充其量是令人费解的。

最好的办法是确保表格的布局是固定的,然后确保 TD 元素的第一行具有固定的宽度。否则你会遇到很多问题,包括许多特定于浏览器的问题。

【讨论】:

    【解决方案3】:

    有一个非常简单的原因:table 元素没有height 属性。

    如果要设置表格的高度,则必须使用 CSS:

    style="height:100%;"
    

    【讨论】:

    • 我用你的代码试过了(当然添加了必要的标签使它成为一个真正的 html 文档),它工作得很好。表格高度是最小值,因此您不能使用它来强制表格变得小于其单元格的高度。为此,您需要设置行或单元格的高度。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签