【问题标题】:Why does Firefox not render border of table with empty tbody?为什么 Firefox 不使用空 tbody 渲染表格边框?
【发布时间】:2015-10-17 03:06:51
【问题描述】:

当表格的 tbody 为空时,Firefox 无法正确呈现表格单元格边框。

但是如果你使用伪选择器tbody:empty {display:none;}来隐藏tbody元素,一切都会按预期呈现。

jsfiddle

table {
    border-collapse: collapse;
}
th,
td {
    border: 1px solid #000;
}

.fixed tbody:empty {
    display: none;
}
<table class="broken">
    <thead>
        <tr>
            <th>1</th>
            <td>2</td>
            <td>3</td>
        </tr>
    </thead>
    <tbody></tbody>
    <tfoot>
        <tr>
            <th>1</th>
            <td>2</td>
            <td>3</td>
        </tr>
    </tfoot>
</table>

<hr />

<table class="fixed">
    <thead>
        <tr>
            <th>1</th>
            <td>2</td>
            <td>3</td>
        </tr>
    </thead>
    <tbody></tbody>
    <tfoot>
        <tr>
            <th>1</th>
            <td>2</td>
            <td>3</td>
        </tr>
    </tfoot>
</table>

【问题讨论】:

  • 提醒像我这样的人:确保您的空 &lt;tbody&gt; 标签在同一行打开和关闭。否则,其中的空白似乎会违反:empty 选择器并阻止此技巧起作用。我使用的解决方法是在&lt;tbody&gt; 中有一个空的&lt;tr&gt; 标记。

标签: css firefox border


【解决方案1】:

它很可能属于 Firefox 上的 Bug 409254Bug 217769

旁注:虽然空的tbody 在 HTML 5 中有效,但每个行组中的单元格数应在一个表中匹配(使用 colspan 除外)。

解决方法是在表格和单元格元素上分别绘制边框。

table {
    border-collapse: separate; /*changed from collapse*/
    border-spacing: 0;
    border: 1px solid;
    border-width: 0 0 1px 1px; /*draw bottom and left borders*/
}
th,
td {
    border: 1px solid;
    border-width: 1px 1px 0 0; /*draw top and right borders*/
}

jsfiddle

【讨论】:

  • 好的,谢谢您的提示。对我来说,有一个空的 tbody 似乎是合法的(它仍然是一个标签)。因为,是的,它可能会发生(如果您在一些用户交互后用 javascript 填充 tbody)。
  • 不客气。我编辑了答案,提出了解决方法。
猜你喜欢
  • 2011-12-24
  • 2011-02-19
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
相关资源
最近更新 更多