为什么 Firefox 不使用空 tbody 渲染 table 的边框?

Why does Firefox not render border of table with empty tbody?

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

但是如果你使用伪选择器 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>

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

旁注:虽然空tbody在HTML5中有效,但每个行组中的单元格数量应该在一个中匹配(使用colspan除外) table.

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

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